#!/bin/sh
#
# A script to perform R14.0.0 XDG compliance updates.

SCRIPT_NAME="`basename \`readlink -f $0\``"
SCRIPT_VERSION=201403020

# This script should be needed to run only once, but corner cases
# and file/directory permissions could cause incomplete updates.

# TODO: How to handle environments where files/directories are locked
#       administratively or are owned by root and can't be updated.
#       The nominal validation checks in this script provide some notice
#       but no direct remedy.

# TODO: How to update profile directories not named $HOME/.trinity and $TDEHOME
#       is not yet declared when running this script.

Wait_For_Response () {
unset response
# -r Backslash does not act as an escape character.
# -p Display "PROMPT" without a trailing newline, before attempting to read any input.
while true; do
  read -r -p "$1 (y/n): " yn
  case $yn in
    [Yy]* ) response=y; break;;
    [Nn]* ) response=n; break;;
    * ) echo "Please answer yes (y/Y) or no (n/N).";;
  esac
done
}

Proceed_From_Response () {
if [ "$response" = "n" -o "$response" = "N" ]; then
  echo "Exiting."
  echo
  exit 0
else
  echo "Continuing."
  echo
fi
}

Message_Prefix () {
if [ "$USER_DIR" = "$HOME" ] && [ "$DISPLAY" != "" ]; then
  printf "%s" "[r14-xdg-update] "
fi
}

Validation_Failure () {
if [ "$USER_DIR" = "$HOME" ] && [ "$DISPLAY" != "" ]; then
  printf "%b" "$MESSAGE" | xmessage -center -file - > /dev/null 2>/dev/null
else
  printf "%b" "$MESSAGE"
fi
if [ "$KDEGLOBALS_KEY_VALUE" = "" ]; then
  KDEGLOBALS_KEY_VALUE="$TEST_NUM"
else
  KDEGLOBALS_KEY_VALUE="$KDEGLOBALS_KEY_VALUE;$TEST_NUM"
fi
}

Log () {
  echo "$@" | \
  while read l; do
    Message_Prefix
    echo "$l"
  done
}

# Main script:

# Allow forced execution of this script regardless of the kdeglobals setting
# and allow passing a user home directory as a positional parameter.
if [ "$#" -eq "2" ]; then
  if [ "$1" = "force" ] || [ "$2" = "force" ]; then
    FORCE="true"
  fi
  if [ "$1" != "force" ]; then
    USER_DIR="$1"
  elif [ "$2" != "force" ]; then
    USER_DIR="$2"
  fi
elif [ "$#" -eq "1" ]; then
  if [ "$1" = "force" ]; then
    FORCE="true"
  else
    USER_DIR="$1"
  fi
fi

unset KDEGLOBALS_KEY_VALUE

WARNING_MESSAGE="Trinity R14 XDG compliance updates will not be performed.\n\nWithout R14 XDG compliance updates, some Trinity apps will fail to\nfunction properly.\n\nFailures include the following:\n\n* Many left-side icon lists will not populate,\n  such as the Panel and Konqueror configuration dialogs.\n\n* User-defined keyboard shortcuts fail (khotkeysrc).\n  System defined shortcuts remain functional.\n\n* User-defined app preferences fail (profilerc).\n\n* Konqueror navigation/sidebar panel won't open.\n\n* User-defined konqueror service menus, kicker customization,\n* konqueror sidebar, Recent Documents list fail.\n\nPlease exercise appropriate action.\n"

# As the user should not be logged into a Trinity session when running
# this script, or an administrator might run this script remotely, the
# $TDEHOME variable might not be set or knowable from within this script.
# We presume $USER_DIR/.trinity and provide a way to pass an environment
# variable to change that location.
USER_DIR=${USER_DIR:-"$HOME"}
if [ "$USER_DIR" != "$HOME" ]; then
  PROFILE_DIR=${PROFILE_DIR:-"$USER_DIR/.trinity"}
elif [ "$TDEHOME" = "" ]; then
  PROFILE_DIR=${PROFILE_DIR:-"$USER_DIR/.trinity"}
else
  PROFILE_DIR="$TDEHOME"
fi
if [ ! -d "$PROFILE_DIR" ]; then
  MESSAGE="Warning! Unable to find the user profile directory $PROFILE_DIR.\n\nCheck permissions, paths, and typing.\n\n${WARNING_MESSAGE}"
  # Are we in X? Display an X dialog explaining breakage.
  if [ "$USER_DIR" = "$HOME" ] && [ "$DISPLAY" != "" ]; then
    printf "%b" "$MESSAGE" | xmessage -center -file - -buttons Continue,Quit > /dev/null 2>/dev/null
    if [ "$?" = "102" ]; then
      # User selected the Quit button: quit this script.
      unset PROFILE_DIR
    fi
  else
    Message_Prefix
    printf "%b" "$MESSAGE"
  fi
  exit 1
fi

# The binaries for TDE are located in the same place as this script.
# To determine that location use the following method rather than presuming
# the existence of $TDEDIR. That environment variable might not be
# defined or defined to point to KDE4 binaries.
BIN_DIR="`dirname \`readlink -f $0\``"
if [ -x $BIN_DIR/tde-config ]; then
  TDEDIR=${BIN_DIR%/bin}
else
  MESSAGE="Unable to determine the TDE bin directory, where this script should be installed."
  # Are we in X? Display an X dialog explaining breakage.
  if [ "$USER_DIR" = "$HOME" ] && [ "$DISPLAY" != "" ]; then
    printf "%b" "$MESSAGE" | xmessage -center -file - -buttons Continue,Quit > /dev/null 2>/dev/null
    if [ "$?" = "102" ]; then
      # User selected the Quit button: quit this script.
      unset PROFILE_DIR
      exit 1
    fi
  else
    Message_Prefix
    printf "%b" "$MESSAGE\n\n${WARNING_MESSAGE}"
  fi
  exit 1
fi
unset BIN_DIR

Log "Performing a profile update for Trinity release R14 XDG compliance."
Log ""
Log "To run this script against a different user directory, or automated"
Log "from within another script, pass the directory path as a parameter."
Log "For example: r14-xdg-update /home/user_dir"
Log "Use the user home directory and not the profile directory."
Log "User directory: $USER_DIR"
Log "Profile directory: $PROFILE_DIR"
Log ""
if [ "$USER_DIR" != "$HOME" ]; then
  Log "Root (admin) privileges might be required to run this script"
  Log "against other user directories."
  Log "This script is being run against $USER_DIR."
  Log "Your normal user directory is $HOME."
fi

# Do not update when $TDEHOME is a sym link to another profile directory. Trinity should have
# full reign within its own profile directory (limited to administrative locking), but the error
# check is a conservative approach.
TDEHOME_LINK="`readlink \"$PROFILE_DIR\"`"
if [ "$TDEHOME_LINK" != "" ]; then
  # Force this entry to ensure the updates eventually are performed should the user copy the
  # original kdeglobals file into a new Trinity profile.
  # $TDEDIR/bin/kwriteconfig --file "$PROFILE_DIR/share/config/kdeglobals" --group "R14 XDG Updates" --key Updated --type bool 'false'
  # Are we in X? Display an X dialog explaining breakage.
  if [ "$USER_DIR" = "$HOME" ] && [ "$DISPLAY" != "" ]; then
    echo "[r14-xdg-update] Warning! The profile directory $PROFILE_DIR is a" 1>&2
    echo "                 sym link to $TDEHOME_LINK!" 1>&2
    echo "                 R14 updates will not be performed because Trinity needs its own" 1>&2
    echo "                 separate profile directory." 1>&2
    echo "                 Without R14 updates some Trinity apps will fail to function correctly." 1>&2
  fi
  MESSAGE="Oops! The profile directory $PROFILE_DIR is a sym link to $TDEHOME_LINK.\n\n${WARNING_MESSAGE}\nPossible remedies:\n\n* Contact your system administrator.\n\n* Break the sym link to force creating a fresh Trinity profile.\n\n* Use the migratekde3 script to migrate a KDE3 profile to Trinity."
  # Are we in X? Display an X dialog explaining breakage.
  if [ "$USER_DIR" = "$HOME" ] && [ "$DISPLAY" != "" ]; then
    MESSAGE="${MESSAGE}\n\nSelecting the Continue button means retaining the KDE3 profile and\nbreaking the sym link. With the sym link broken, run the migratekde3\nscript before restarting Trinity to migrate a KDE3 profile or\nallow Trinity to create a fresh profile."
    printf "%b" "$MESSAGE" | xmessage -center -file - -buttons Continue,Quit > /dev/null 2>/dev/null
    EXIT_CODE="$?"
    unset TDEHOME_LINK
    if [ "$EXIT_CODE" = "102" ]; then
      # User selected the Quit button: quit this script and exit X.
      unset PROFILE_DIR
      Log "The user chose to quit."
      exit 1
    else
      # User selected the Continue button: continue this script and start TDE.
      Log "The user chose to continue, which will break the sym link."
      BREAK_SYMLINK="true"
    fi
  else
    echo
    printf "%b" "$MESSAGE"
    echo
    Wait_For_Response "Break the sym link now?"
    Proceed_From_Response
    BREAK_SYMLINK="true"
  fi
  if [ "$BREAK_SYMLINK" = "true" ]; then
    unlink "$USER_DIR/.trinity" 2>/dev/null
    if [ "`readlink \"$USER_DIR/.trinity\"`" = "" ]; then
      MESSAGE="Sym link broken. With the sym link broken, run the migratekde3\nscript before restarting Trinity to migrate a KDE3 profile or\nallow Trinity to create a fresh profile."
      Message_Prefix
      printf "%b" "$MESSAGE"
      echo
    else
      MESSAGE="Unable to break the sym link. Check file and directory privileges. Quitting."
      Message_Prefix
      echo "$MESSAGE"
      echo
    fi
  fi
  unset PROFILE_DIR
  unset TDEHOME_LINK
  exit 1
fi
unset TDEHOME_LINK

R14_UPDATED="`$TDEDIR/bin/kreadconfig --file "$PROFILE_DIR/share/config/kdeglobals" --group "R14 XDG Updates" --key Updated`"
R14_VERSION="`$TDEDIR/bin/kreadconfig --file "$PROFILE_DIR/share/config/kdeglobals" --group "R14 XDG Updates" --key Version --default 0`"
if [ "$R14_VERSION" -lt "$SCRIPT_VERSION" ] || [ "$R14_UPDATED" != "true" ] || [ "$FORCE" = "true" ]; then
  if [ "$R14_UPDATED" != "true" ] && [ "$R14_UPDATED" != "false" ] && [ "$R14_UPDATED" != "" ]; then
    Log "The r14-xdg-update script has been run at least once."
    Log "The error code is $R14_UPDATED."
    echo
    if [ "$FORCE" != "true" ]; then
      # There is an error code.
      # As a previous attempt failed, try again to perform a full update.
      FORCE="true"
    fi
    MESSAGE="The r14-xdg-update script has been run at least once.\n\nThe script is not successfully updating.\n\nThe script will run with each login until corrected.\n\nPlease contact an administrator or take appropriate\nadmininstrative action to correct the problem.\n\nThe error code is $R14_UPDATED."
    # Are we in X? Display an X dialog explaining breakage.
    if [ "$USER_DIR" = "$HOME" ] && [ "$DISPLAY" != "" ]; then
      printf "%b" "$MESSAGE" | xmessage -center -file - -buttons Continue,Quit > /dev/null 2>/dev/null
      if [ "$?" = "102" ]; then
        # User select the Quit button: quit this script.
        unset PROFILE_DIR
        unset R14_UPDATED
        exit 1
      fi
    fi
  fi
else
  echo "This script has been run at least once previously. To run manually pass the 'force' parameter."
  exit 0
fi
if [ "$FORCE" = "true" ]; then
  R14_VERSION=0
fi

# Trap when the user runs this script while in a Trinity session.
# Most files can be updated "live" but some can't, such as kdeglobals.
if [ "$USER_DIR" = "$HOME" ]; then
  if [ "$TDE_FULL_SESSION" != "" ] || [ "$TDE_SESSION_UID" != "" ]; then
    MESSAGE="You are running this script from while a Trinity session is active.\n\nMost files can be updated \"live\" but some cannot, such as kdeglobals.\n\nThis script might complete successfully and might not."
    Message_Prefix
    printf "%b" "$MESSAGE\n\n${WARNING_MESSAGE}"
    Wait_For_Response "Continue?"
    Proceed_From_Response
  fi
fi

if [ "$R14_VERSION" -lt "201309150" ]; then
  Log "Updating temp file locations."
  # All three directories are for temporary files. The cache directory is
  # intended for persistent temporary data (is expected to remain across reboots
  # and shutdowns). The other two directories are for non-persistent data and
  # can be deleted across reboots and shutdowns.
  CACHE_DIR="`readlink $PROFILE_DIR/cache-\`uname -n\``"
  SOCKET_DIR="`readlink $PROFILE_DIR/socket-\`uname -n\``"
  TMP_DIR="`readlink $PROFILE_DIR/tmp-\`uname -n\``"
  # Delete the non-persistent temporary directories. This is safe at any time.
  unlink $PROFILE_DIR/socket-`uname -n` 2>/dev/null
  unlink $PROFILE_DIR/tmp-`uname -n` 2>/dev/null
  if [ "$SOCKET_DIR" != "" ]; then
    rm -fr $SOCKET_DIR 2>/dev/null
  fi
  if [ "$TMP_DIR" != "" ]; then
    rm -fr $TMP_DIR 2>/dev/null
  fi
  # Remember that this script may be run more than once. The new directory
  # might already exist.
  if [ "$CACHE_DIR" != "" ] && [ -d "$CACHE_DIR" ]; then
    # Flush the obsolete ksycoca cache files.
    rm -f ${CACHE_DIR}/ksycoca* 2>/dev/null
    # Flush the tdesycoca cache files. This is safe at any time.
    rm -f ${CACHE_DIR}/tdesycoca* 2>/dev/null
    # Old cache directory: /var/tmp/kde*cache-$USER
    # New cache directory: /var/tmp/tdecache-$USER
    # Rename/move the directory name but only when the new name does not exist.
    if [ "`echo $CACHEDIR | grep tdecache`" = "" ]; then
      Log "Renaming the temporary cache directory."
      unlink $PROFILE_DIR/cache-`uname -n` 2>/dev/null
      mv -f $CACHE_DIR `dirname $CACHE_DIR`/tdecache-$USER 2>/dev/null
      Log "Creating a sym link for the temporary cache directory."
      ln -s `dirname $CACHE_DIR`/tdecache-$USER $PROFILE_DIR/cache-`uname -n`
    fi
  fi
  # Housekeeping: the old locations are no longer needed.
  if [ "$CACHE_DIR" != "" ]; then
    rm -fr `dirname 2>/dev/null $CACHE_DIR`/kde*cache-$USER
  fi
  if [ "$SOCKET_DIR" != "" ]; then
    rm -fr `dirname 2>/dev/null $SOCKET_DIR`/ksocket-$USER
  fi
  if [ "$TMP_DIR" != "" ]; then
    rm -fr `dirname 2>/dev/null $TMP_DIR`/kde-$USER
  fi
fi

if [ "$R14_VERSION" -lt "201309150" ]; then
  Log "Updating references of $TDEDIR/share/applications/kde to share/applications/tde."
  # Exclude KMail mail files --- we don't want to touch those files.
  find "$PROFILE_DIR" \
       -path $PROFILE_DIR/share/apps/amarok/albumcovers -prune -o \
       -path $PROFILE_DIR/share/apps/basket/baskets -prune -o \
       -path $PROFILE_DIR/share/apps/juk/covers -prune -o \
       -path $PROFILE_DIR/share/apps/kget/logs -prune -o \
       -path $PROFILE_DIR/share/apps/kmail/autosave -prune -o \
       -path $PROFILE_DIR/share/apps/kmail/dimap -prune -o \
       -path $PROFILE_DIR/share/apps/kmail/imap -prune -o \
       -path $PROFILE_DIR/share/apps/kmail/mail -prune -o \
       -path $PROFILE_DIR/share/apps/kmail/search -prune -o \
       -path $PROFILE_DIR/share/apps/knotes -prune -o \
       -path $PROFILE_DIR/share/apps/kopete/logs -prune -o \
       -type f -print0 2>/dev/null | \
    xargs -r0 grep -ZIFl "$TDEDIR/share/applications/kde" | \
    xargs -r0 sed -i "s|$TDEDIR/share/applications/kde|$TDEDIR/share/applications/tde|g"
fi
if [ "$R14_VERSION" -lt "201312160" ]; then
  Log "Updating references of kconf to tdeconf."
  find "$PROFILE_DIR"/share/config -type f -print0 2>/dev/null | \
    xargs -r0 grep -ZIEl "([^a-zA-Z0-9]|^)kconf" | \
    xargs -r0 sed -ri "s|([^a-zA-Z0-9]\|^)kconf|\1tdeconf|g"
fi

# Preserve keyboard shortcuts and input actions.
if [ "$R14_VERSION" -lt "201309150" ]; then
  if [ -r "$PROFILE_DIR/share/config/khotkeysrc" ]; then
    Log "Updating user-defined keyboard shortcuts in khotkeysrc."
    sed -i 's|CommandURL=kde-|CommandURL=tde-|g' "$PROFILE_DIR/share/config/khotkeysrc" 2>/dev/null
    sed -i 's|K Menu - kde-|TDE Menu - tde-|g' "$PROFILE_DIR/share/config/khotkeysrc" 2>/dev/null
    sed -i 's|Name=K Menu|Name=TDE Menu|g' "$PROFILE_DIR/share/config/khotkeysrc" 2>/dev/null
    sed -i 's|in KDE stands|in TDE stands|g' "$PROFILE_DIR/share/config/khotkeysrc" 2>/dev/null
    sed -i 's| use KDE| use TDE|g' "$PROFILE_DIR/share/config/khotkeysrc" 2>/dev/null
    Log "Updating some text strings in khotkeysrc."
    sed -i 's|Go to KDE Website|Go to TDE Website|g' "$PROFILE_DIR/share/config/khotkeysrc" 2>/dev/null
    sed -i 's|www\.kde\.org|www\.trinitydesktop\.org|g' "$PROFILE_DIR/share/config/khotkeysrc" 2>/dev/null
    sed -i 's|KDE3\.1|TDE|g' "$PROFILE_DIR/share/config/khotkeysrc" 2>/dev/null
    sed -i 's|kde32b1|trinity2b1|g' "$PROFILE_DIR/share/config/khotkeysrc" 2>/dev/null
    sed -i 's|kde321|trinity21|g' "$PROFILE_DIR/share/config/khotkeysrc" 2>/dev/null
  fi
fi
# Fix the some of the same text strings in kglobalshortcutsrc.
if [ "$R14_VERSION" -lt "201309150" ]; then
  Log "Updating some text strings in kglobalshortcutsrc."
  sed -i 's|Go to KDE Website|Go to TDE Website|g' "$PROFILE_DIR/share/config/kglobalshortcutsrc" 2>/dev/null
  sed -i 's|www\.kde\.org|www\.trinitydesktop\.org|g' "$PROFILE_DIR/share/config/kglobalshortcutsrc" 2>/dev/null
  sed -i 's|KDE3\.1|TDE|g' "$PROFILE_DIR/share/config/kglobalshortcutsrc" 2>/dev/null
fi

# Preserve app preferences.
if [ "$R14_VERSION" -lt "201309150" ]; then
  if [ -r $PROFILE_DIR/share/config/profilerc ]; then
    Log "Updating user-defined app preferences in profilerc."
    sed -i 's|Application=kde-|Application=tde-|g' "$PROFILE_DIR/share/config/profilerc" 2>/dev/null
    sed -i 's|khtml|tdehtml|' "$PROFILE_DIR/share/config/profilerc" 2>/dev/null
    sed -i 's|kfile_|tdefile_|' "$PROFILE_DIR/share/config/profilerc" 2>/dev/null
    sed -i 's|kfilereplace|tdefilereplace|' "$PROFILE_DIR/share/config/profilerc" 2>/dev/null
  fi
fi

# Preserve kicker/panel icons.
if [ "$R14_VERSION" -lt "201309150" ]; then
  if [ -r $PROFILE_DIR/share/config/kickerrc ]; then
    Log "Updating kicker/panel customizations in kickerrc."
    if [ -r "$PROFILE_DIR/share/config/kickerrc" ]; then
      sed -i 's|StorageId\[\$e\]=kde-|StorageId\[\$e\]=tde-|g' "$PROFILE_DIR/share/config/kickerrc" 2>/dev/null
    else
      Log "kickerrc does not exist."
    fi
  fi
fi
if [ "$R14_VERSION" -lt "201309150" ]; then
  if [ -r $PROFILE_DIR/share/config/systemtray_panelappletrc ]; then
    Log "Updating system tray icons in systemtray_panelappletrc."
    sed -i -e "s|Krandr|Tderandr|g" \
           -e "s|Kwallet|Tdewallet|g" \
           -e "s|Knetworkmanager|Tdenetworkmanager|g" \
           -e "s|Kradio|Tderadio|g" \
        $PROFILE_DIR/share/config/systemtray_panelappletrc
  fi
fi

if [ "$R14_VERSION" -lt "201309150" ]; then
  if [ -r $PROFILE_DIR/share/config/katerc ]; then
    Log "Updating katerc."
    sed -i 's|ktexteditor_|tdetexteditor_|g' "$PROFILE_DIR/share/config/katerc" 2>/dev/null
  else
    Log "katerc does not exist."
  fi
fi

if [ "$R14_VERSION" -lt "201309150" ]; then
  if [ -r $PROFILE_DIR/share/config/kwriterc ]; then
    Log "Updating kwriterc."
    sed -i 's|ktexteditor_|tdetexteditor_|g' "$PROFILE_DIR/share/config/kwriterc" 2>/dev/null
  else
    Log "kwriterc does not exist."
  fi
fi

if [ "$R14_VERSION" -lt "201401070" ]; then
  if [ -r $PROFILE_DIR/share/config/keditrc ]; then
    Log "Updating keditrc."
    sed -i 's|ktexteditor_|tdetexteditor_|g' "$PROFILE_DIR/share/config/keditrc" 2>/dev/null
  else
    Log "keditrc does not exist."
  fi
fi

if [ "$R14_VERSION" -lt "201401070" ]; then
  if [ -r $PROFILE_DIR/share/config/quantarc ]; then
    Log "Updating quantarc."
    sed -i 's|ktexteditor_|tdetexteditor_|g' "$PROFILE_DIR/share/config/quantarc" 2>/dev/null
  else
    Log "quantarc does not exist."
  fi
fi

if [ "$R14_VERSION" -lt "201401070" ]; then
  # kdeveloprc is renamed to tdeveloprc later in this script. As we don't know whether
  # this script is being run the first time or any subsequent time, we look for both rc files.
  if [ -r $PROFILE_DIR/share/config/tdeveloprc ]; then
    DEVELOP_RC_FILE="$PROFILE_DIR/share/config/tdeveloprc"
  elif [ -r $PROFILE_DIR/share/config/kdeveloprc ]; then
    DEVELOP_RC_FILE="$PROFILE_DIR/share/config/kdeveloprc"
  fi
  if [ "$DEVELOP_RC_FILE" != "" ]; then
    Log "Updating `basename $DEVELOP_RC_FILE`."
    sed -i 's|ktexteditor_|tdetexteditor_|g' "$DEVELOP_RC_FILE" 2>/dev/null
    unset DEVELOP_RC_FILE
  else
    Log "[tk]developrc does not exist."
  fi
fi

if [ "$R14_VERSION" -lt "201401131" ]; then
  if [ -r $PROFILE_DIR/share/config/khelpcenterrc ]; then
    Log "Updating khelpcenterrc."
    sed -i 's|kde_application_manuals|tde_application_manuals|g' "$PROFILE_DIR/share/config/khelpcenterrc" 2>/dev/null
  else
    Log "khelpcenterrc does not exist."
  fi
fi

if [ "$R14_VERSION" -lt "201401070" ]; then
  if [ -r $PROFILE_DIR/share/apps/kate/kateui.rc ]; then
    Log "Updating kateui.rc."
    sed -i 's|ktexteditor_|tdetexteditor_|g' "$PROFILE_DIR/share/apps/kate/kateui.rc" 2>/dev/null
  else
    Log "kateui.rc does not exist."
  fi
fi

if [ "$R14_VERSION" -lt "201401070" ]; then
  if [ -r $PROFILE_DIR/share/apps/katepart/katepartui.rc ]; then
    Log "Updating katepartui.rc."
    sed -i 's|ktexteditor_|tdetexteditor_|g' "$PROFILE_DIR/share/apps/katepart/katepartui.rc" 2>/dev/null
  else
    Log "katepartui.rc does not exist."
  fi
fi

if [ "$R14_VERSION" -lt "201401070" ]; then
  if [ -r $PROFILE_DIR/share/apps/kwrite/kwriteui.rc ]; then
    Log "Updating kwriteui.rc."
    sed -i 's|ktexteditor_|tdetexteditor_|g' "$PROFILE_DIR/share/apps/kwrite/kwriteui.rc" 2>/dev/null
  else
    Log "kwriteui.rc does not exist."
  fi
fi

if [ "$R14_VERSION" -lt "201401070" ]; then
  if [ -d $PROFILE_DIR/share/apps/ktexteditor_kdatatool ]; then
    Log "Updating ktexteditor_kdatatool."
    # First rename any existing file inside the directory.
    if [ -r $PROFILE_DIR/share/apps/ktexteditor_kdatatool/ktexteditor_kdatatoolui.rc ]; then
      mv $PROFILE_DIR/share/apps/ktexteditor_kdatatool/ktexteditor_kdatatoolui.rc $PROFILE_DIR/share/apps/ktexteditor_kdatatool/tdetexteditor_kdatatoolui.rc
      sed -i 's|ktexteditor_|tdetexteditor_|g' "$PROFILE_DIR/share/apps/ktexteditor_kdatatool/tdetexteditor_kdatatoolui.rc" 2>/dev/null
    fi
    # Now rename the directory.
    mv $PROFILE_DIR/share/apps/ktexteditor_kdatatool $PROFILE_DIR/share/apps/tdetexteditor_kdatatool
  else
    Log "ktexteditor_kdatatool does not exist."
  fi
fi

if [ "$R14_VERSION" -lt "201309150" ]; then
  if [ -r $PROFILE_DIR/share/apps/ark/ark_part.rc ]; then
    Log "Updating ark_part.rc."
    sed -i 's|ark_kparts|ark_tdeparts|g' "$PROFILE_DIR/share/apps/ark/ark_part.rc" 2>/dev/null
  else
    Log "ark_part.rc does not exist."
  fi
fi

# Preserve Quick Launch icons.
# There should only be one configuration file, but old KDE3 remnant files might exist from users who
# migrated from KDE3.
if [ "$R14_VERSION" -lt "201309150" ]; then
  Log "Updating Quick Launch applet."
  if [ -r "$PROFILE_DIR/share/config/kickerrc" ]; then
    QUICK_LAUNCH_CONFIG="`grep launcher_panelapplet $PROFILE_DIR/share/config/kickerrc | awk -F = '{print $2}'`"
    if [ "$QUICK_LAUNCH_CONFIG" != "" ]; then
      sed -i 's|,kde-|,tde-|g' "$PROFILE_DIR/share/config/$QUICK_LAUNCH_CONFIG" 2>/dev/null
    else
      Log "Quick Launch is not installed."
    fi
  else
    Log "Quick Launch is not installed."
  fi
fi

if [ "$R14_VERSION" -lt "201402020" ]; then
  Log "Updating Quanta Plus plugins.rc."
  if [ -r $PROFILE_DIR/share/apps/quanta/plugins.rc ]; then
    sed -i 's|FileName=kde3|FileName=trinity|g' "$PROFILE_DIR/share/apps/quanta/plugins.rc" 2>/dev/null
    sed -i 's|FileName=kdemod3|FileName=trinity|g' "$PROFILE_DIR/share/apps/quanta/plugins.rc" 2>/dev/null
    sed -i 's|KFileReplace|TDEFileReplace|g' "$PROFILE_DIR/share/apps/quanta/plugins.rc" 2>/dev/null
    sed -i 's|kfilereplace|tdefilereplace|g' "$PROFILE_DIR/share/apps/quanta/plugins.rc" 2>/dev/null
  else
    Log "Quanta Plus plugins.rc does not exist."
  fi
fi

# Update sym link files in $USER_DIR/.trinity/Autostart.
if [ "$R14_VERSION" -lt "201309150" ]; then
  if [ -d "$PROFILE_DIR/Autostart" ]; then
    ( cd "$PROFILE_DIR/Autostart"
      Log "Updating Autostart files."
      for i in `find . -type l`; do
        LINK="`readlink $i`"
        LINK_PATH="`dirname $LINK`"
        LINK_NAME="`basename $LINK`"
        if [ "`echo $LINK_PATH | grep \"$TDEDIR/share/applications/kde\"`" != "" ]; then
          NEW_LINK_PATH="`echo \"$LINK_PATH\" | sed 's|/share/applications/kde|/share/applications/tde|'`"
        fi
        if [ "$NEW_LINK_PATH" != "" ]; then
          unlink $i
          ln -sf "$NEW_LINK_PATH/$LINK_NAME" "$LINK_NAME"
        fi
        if [ "$?" != "0" ]; then
          Log "There was an error with creating a new sym link for $LINK." 1>&2
          KDEGLOBALS_KEY_VALUE="autostart"
        fi
      done
    )
  else
    Log "Autostart directory not found."
  fi
fi

# Ensure all KDED services are accounted for in the user's profile. Any that are missing
# are defaulted to not auto-loading (false). Refer to bug report 1210. This same test is
# performed in the migratekde3 script, but notice the migratekde3 script uses the key of
# X-KDE-Kded-autoload whereas X-TDE-Kded-autoload is used here.
if [ "$R14_VERSION" -lt "201309150" ]; then
  if [ -d $PROFILE_DIR/share/services/kded ]; then
    if [ "`find $PROFILE_DIR/share/services/kded -name \"*.desktop\"`" != "" ]; then
      Log "Validating KDED services."
      for i in `/bin/ls -1 $PROFILE_DIR/share/services/kded/*.desktop`; do
        SERVICE_NAME=`basename $i`
        if [ ! -f $TDEDIR/share/services/kded/$SERVICE_NAME ]; then
          $TDEDIR/bin/kwriteconfig --file $i --group "Desktop Entry" --key "X-TDE-Kded-autoload" --type bool "false"
        fi
      done
    fi
  fi
fi

# Try to update sessions.
if [ "$R14_VERSION" -lt "201310050" ]; then
  Log "Update sessions."
  if [ -f $PROFILE_DIR/share/config/ksmserverrc ]; then
    sed -i -e 's|kwin|twin|g' \
           -e 's|krandr|tderandr|g' \
           -e 's|kwallet|tdewallet|g' \
           -e 's|kradio|tderadio|g' \
        $PROFILE_DIR/share/config/ksmserverrc
  fi
  if [ -d $PROFILE_DIR/share/config/session ]; then
    find $PROFILE_DIR/share/config/session -name "kwin_*" -o -name "kwallet*" -o -name "kradio*" | \
    while read i; do
      ir=$PROFILE_DIR/share/config/session/$(basename $i | \
         sed -e "s|^kwin_|twin_|" \
             -e "s|^kwallet|tdewallet|" \
             -e "s|^kradio|tderadio|")
      mv $i $ir
    done
  fi
fi

Log "Renaming some configuration files and directories."
# Note: The only rebranding that occured before starting the R14 branch was krita. All other
# rebranding updates belong in this script.
# Don't force renaming in case this script is used to update an existing Trinity profile. That is,
# always check whether the new config file already exists.
if [ "$R14_VERSION" -lt "201309150" ]; then
  if [ ! -f $PROFILE_DIR/share/config/tdeprintrc ] && [ -f $PROFILE_DIR/share/config/kdeprintrc ] || [ -d $PROFILE_DIR/share/apps/kdeprint ]; then
    Log "  kdeprint->tdeprint"
    mv $PROFILE_DIR/share/config/kdeprintrc $PROFILE_DIR/share/config/tdeprintrc 2>/dev/null
    mv $PROFILE_DIR/share/apps/kdeprint $PROFILE_DIR/share/apps/tdeprint 2>/dev/null
  fi
  if [ ! -f $PROFILE_DIR/share/config/tdesurc ] && [ -f $PROFILE_DIR/share/config/kdesurc ]; then
    Log "  kdesu->tdesu"
    mv $PROFILE_DIR/share/config/kdesurc $PROFILE_DIR/share/config/tdesurc 2>/dev/null
  fi
  if [ ! -f $PROFILE_DIR/share/config/tdeveloprc ] && [ -f $PROFILE_DIR/share/config/kdeveloprc ]; then
    Log "  kdevelop->tdevelop"
    mv $PROFILE_DIR/share/config/kdeveloprc $PROFILE_DIR/share/config/tdeveloprc 2>/dev/null
  fi
fi
if [ "$R14_VERSION" -lt "201403020" ]; then
  if [ ! -f $PROFILE_DIR/share/config/doctdevtocpluginrc ] && [ -f $PROFILE_DIR/share/config/dockdevtocpluginrc ]; then
    Log "  dockdevtocpluginrc->doctdevtocpluginrc"
    mv $PROFILE_DIR/share/config/dockdevtocpluginrc $PROFILE_DIR/share/config/doctdevtocpluginrc 2>/dev/null
  fi
  if [ ! -f $PROFILE_DIR/share/config/tdevgrepviewrc ] && [ -f $PROFILE_DIR/share/config/kdevgrepviewrc ]; then
    Log "  kdevgrepviewrc->tdevgrepviewrc"
    mv $PROFILE_DIR/share/config/kdevgrepviewrc $PROFILE_DIR/share/config/tdevgrepviewrc 2>/dev/null
  fi
  if [ ! -f $PROFILE_DIR/share/config/tdevsnippetrc ] && [ -f $PROFILE_DIR/share/config/kdevsnippetrc ]; then
    Log "  kdevsnippetrc->tdevsnippetrc"
    mv $PROFILE_DIR/share/config/kdevsnippetrc $PROFILE_DIR/share/config/tdevsnippetrc 2>/dev/null
  fi
  if [ ! -f $PROFILE_DIR/share/config/tdevassistantrc ] && [ -f $PROFILE_DIR/share/config/kdevassistantrc ]; then
    Log "  kdevassistantrc->tdevassistantrc"
    mv $PROFILE_DIR/share/config/kdevassistantrc $PROFILE_DIR/share/config/tdevassistantrc 2>/dev/null
  fi
  if [ ! -f $PROFILE_DIR/share/config/tdevfileselectorrc ] && [ -f $PROFILE_DIR/share/config/kdevfileselectorrc ]; then
    Log "  kdevfileselectorrc->tdevfileselectorrc"
    mv $PROFILE_DIR/share/config/kdevfileselectorrc $PROFILE_DIR/share/config/tdevfileselectorrc 2>/dev/null
  fi
  if [ ! -f $PROFILE_DIR/share/config/tdevdesignerrc ] && [ -f $PROFILE_DIR/share/config/kdevdesignerrc ]; then
    Log "  kdevdesignerrc->tdevdesignerrc"
    mv $PROFILE_DIR/share/config/kdevdesignerrc $PROFILE_DIR/share/config/tdevdesignerrc 2>/dev/null
  fi
  if [ ! -d $PROFILE_DIR/share/apps/tdevdocumentation ] && [ -d $PROFILE_DIR/share/apps/kdevdocumentation ]; then
    Log "  kdevdocumentation->tdevdocumentation"
    mv $PROFILE_DIR/share/apps/kdevdocumentation $PROFILE_DIR/share/apps/tdevdocumentation 2>/dev/null
  fi
  if [ ! -d $PROFILE_DIR/share/apps/tdevfileselector ] && [ -d $PROFILE_DIR/share/apps/kdevfileselector ]; then
    Log "  kdevfileselector->tdevfileselector"
    mv $PROFILE_DIR/share/apps/kdevfileselector $PROFILE_DIR/share/apps/tdevfileselector 2>/dev/null
  fi
  if [ ! -d $PROFILE_DIR/share/apps/tdevabbrev ] && [ -d $PROFILE_DIR/share/apps/kdevabbrev ]; then
    Log "  kdevabbrev->tdevabbrev"
    mv $PROFILE_DIR/share/apps/kdevabbrev $PROFILE_DIR/share/apps/tdevabbrev 2>/dev/null
  fi
fi
# kwin/twin is the Trinity window manager. kwin4/twin4 is a game.
if [ "$R14_VERSION" -lt "201310190" ]; then
  if [ ! -f $PROFILE_DIR/share/config/twinrc ] && [ -f $PROFILE_DIR/share/config/kwinrc ] || [ -f $PROFILE_DIR/share/config/kwinrulesrc ]; then
    # Do not include kwinrules_update because that is an auto-generated file.
    Log "  kwin->twin"
    mv $PROFILE_DIR/share/config/kwinrc $PROFILE_DIR/share/config/twinrc 2>/dev/null
    mv $PROFILE_DIR/share/config/kwinrc.eventsrc $PROFILE_DIR/share/config/twinrc.eventsrc 2>/dev/null
    mv $PROFILE_DIR/share/config/kwinrc $PROFILE_DIR/share/config/twinrc 2>/dev/null
    mv $PROFILE_DIR/share/config/kwinrulesrc $PROFILE_DIR/share/config/twinrulesrc 2>/dev/null
    mv $PROFILE_DIR/share/config/kwin_rules_dialogrc $PROFILE_DIR/share/config/twin_rules_dialogrc 2>/dev/null
    sed -i 's|PluginLib=kwin_|PluginLib=twin_|' $PROFILE_DIR/share/config/twinrc
    sed -i 's|PluginLib=kwin3_|PluginLib=twin3_|' $PROFILE_DIR/share/config/twinrc
  fi
fi
if [ "$R14_VERSION" -lt "201309150" ]; then
  if [ ! -f $PROFILE_DIR/share/config/twin4rc ] && [ -f $PROFILE_DIR/share/config/kwin4rc ]; then
    Log "  kwin4->twin4"
    mv $PROFILE_DIR/share/config/kwin4rc $PROFILE_DIR/share/config/twin4rc 2>/dev/null
  fi
  if [ ! -d $PROFILE_DIR/share/config/tderesources ] && [ -d $PROFILE_DIR/share/config/kresources ]; then
    Log "  kresources->tderesources"
    mv $PROFILE_DIR/share/config/kresources $PROFILE_DIR/share/config/tderesources 2>/dev/null
  fi
  if [ ! -f $PROFILE_DIR/share/config/tdeab2tdeabcrc ] && [ -f $PROFILE_DIR/share/config/kab2kabcrc ]; then
    Log "  kab2kabc->tdeab2tdeabc"
    mv $PROFILE_DIR/share/config/kab2kabcrc $PROFILE_DIR/share/config/tdeab2tdeabcrc 2>/dev/null
  fi
  if [ ! -f $PROFILE_DIR/share/config/tdeconf_updaterc ] && [ -f $PROFILE_DIR/share/config/kconf_updaterc ]; then
    Log "  kconf_update->tdeconf_update"
    mv $PROFILE_DIR/share/config/kconf_updaterc $PROFILE_DIR/share/config/tdeconf_updaterc 2>/dev/null
    mv $PROFILE_DIR/share/apps/kconf_update $PROFILE_DIR/share/apps/tdeconf_update 2>/dev/null
  fi
  if [ ! -f $PROFILE_DIR/share/config/tdeio_httprc ] && [ -f $PROFILE_DIR/share/config/kio_httprc ]; then
    Log "  kio_http->tdeio_http"
    mv $PROFILE_DIR/share/config/kio_httprc $PROFILE_DIR/share/config/tdeio_httprc 2>/dev/null
  fi
  if [ ! -f $PROFILE_DIR/share/config/tdeio_ftprc ] && [ -f $PROFILE_DIR/share/config/kio_ftprc ]; then
    Log "  kio_ftp->tdeio_ftp"
    mv $PROFILE_DIR/share/config/kio_ftprc $PROFILE_DIR/share/config/tdeio_ftprc 2>/dev/null
    mv $PROFILE_DIR/share/apps/kio_ftp $PROFILE_DIR/share/apps/tdeio_ftp 2>/dev/null
  fi
  if [ ! -f $PROFILE_DIR/share/config/tdeioslaverc ] && [ -f $PROFILE_DIR/share/config/kioslaverc ]; then
    Log "  kioslave->tdeioslave"
    mv $PROFILE_DIR/share/config/kioslaverc $PROFILE_DIR/share/config/tdeioslaverc 2>/dev/null
  fi
  if [ ! -f $PROFILE_DIR/share/config/tdelaunchrc ] && [ -f $PROFILE_DIR/share/config/klaunchrc ]; then
    Log "  klaunch->tdelaunch"
    mv $PROFILE_DIR/share/config/klaunchrc $PROFILE_DIR/share/config/tdelaunchrc 2>/dev/null
  fi
  if [ ! -f $PROFILE_DIR/share/config/tderandrtrayrc ] && [ -f $PROFILE_DIR/share/config/krandrtrayrc ]; then
    Log "  krandrtray->tderandrtray"
    mv $PROFILE_DIR/share/config/krandrtrayrc $PROFILE_DIR/share/config/tderandrtrayrc 2>/dev/null
  fi
  if [ ! -f $PROFILE_DIR/share/config/tdewalletrc ] && [ -f $PROFILE_DIR/share/config/kwalletrc ]; then
    Log "  kwallet->tdewallet"
    mv $PROFILE_DIR/share/config/kwalletrc $PROFILE_DIR/share/config/tdewalletrc 2>/dev/null
    mv $PROFILE_DIR/share/apps/kwallet $PROFILE_DIR/share/apps/tdewallet 2>/dev/null
  fi
  if [ ! -d $PROFILE_DIR/share/apps/tdefile ] && [ -d $PROFILE_DIR/share/apps/kfile ]; then
    Log "  kfile->tdefile"
    mv $PROFILE_DIR/share/apps/kfile $PROFILE_DIR/share/apps/tdefile 2>/dev/null
  fi
  if [ ! -f $PROFILE_DIR/share/config/tdehtmlrc ] && [ -f $PROFILE_DIR/share/config/khtmlrc ]; then
    Log "  khtml->tdehtml"
    mv $PROFILE_DIR/share/config/khtmlrc $PROFILE_DIR/share/config/tdehtmlrc 2>/dev/null
    mv $PROFILE_DIR/share/apps/khtml $PROFILE_DIR/share/apps/tdehtml 2>/dev/null
  fi
  if [ ! -f $PROFILE_DIR/share/config/tdeio_camerarc ] && [ -f $PROFILE_DIR/share/config/kio_camerarc ]; then
    Log "  kio_camera->tdeio_camera"
    mv $PROFILE_DIR/share/config/kio_camerarc $PROFILE_DIR/share/config/tdeio_camerarc 2>/dev/null
  fi
  if [ ! -f $PROFILE_DIR/share/config/tdeio_thumbnailrc ] && [ -f $PROFILE_DIR/share/config/kio_thumbnailrc ]; then
    Log "  kio_thumbnail->tdeio_thumbnail"
    mv $PROFILE_DIR/share/config/kio_thumbnailrc $PROFILE_DIR/share/config/tdeio_thumbnailrc 2>/dev/null
  fi
  if [ ! -f $PROFILE_DIR/share/config/tdeio_locaterc ] && [ -f $PROFILE_DIR/share/config/kio_locaterc ]; then
    Log "  kio_locate->tdeio_locate"
    mv $PROFILE_DIR/share/config/kio_locaterc $PROFILE_DIR/share/config/tdeio_locaterc 2>/dev/null
  fi
  if [ ! -f $PROFILE_DIR/share/config/tdeio_aptrc ] && [ -f $PROFILE_DIR/share/config/kio_aptrc ]; then
    Log "  kio_apt->tdeio_apt"
    mv $PROFILE_DIR/share/config/kio_aptrc $PROFILE_DIR/share/config/tdeio_aptrc 2>/dev/null
  fi
  if [ ! -f $PROFILE_DIR/share/config/tderadiorc ] && [ -f $PROFILE_DIR/share/config/kradiorc ]; then
    Log "  kradio->tderadio"
    mv $PROFILE_DIR/share/config/kradiorc $PROFILE_DIR/share/config/tderadiorc 2>/dev/null
    mv $PROFILE_DIR/share/apps/kradio $PROFILE_DIR/share/apps/tderadio 2>/dev/null
  fi
  if [ ! -f $HOME/.tderc ] && [ -f $HOME/.kderc ]; then
    # Do not blindly move $HOME/.kderc because that file might be from KDE4.
    # The first test is from a Trinity .kderc and the second test is from a KDE4 .kderc.
    if [ "`grep selectBackground $HOME/.kderc`" != "" ] && [ "`grep smallestReadableFont $HOME/.kderc`" = "" ]; then
      # Both tests pass. Probably a Trinity .kderc.
      Log "  .kderc->.tderc"
      mv $HOME/.kderc $HOME/.tderc 2>/dev/null
    fi
  fi
fi
if [ "$R14_VERSION" -lt "201401070" ]; then
  if [ ! -d $PROFILE_DIR/share/apps/tdestyle ] && [ -d $PROFILE_DIR/share/apps/kstyle ]; then
    Log "  kstyle->tdestyle"
    mv $PROFILE_DIR/share/apps/kstyle $PROFILE_DIR/share/apps/tdestyle 2>/dev/null
  fi
fi
if [ "$R14_VERSION" -lt "201309150" ]; then
  # Copy the following two rc files rather than move because the older versions are needed for HAL systems.
  if [ ! -f $PROFILE_DIR/share/config/tdenetworkmanagerrc ] && [ -f $PROFILE_DIR/share/config/knetworkmanagerrc ]; then
    Log "  knetworkmanager->tdenetworkmanager"
    cp -a $PROFILE_DIR/share/config/knetworkmanagerrc $PROFILE_DIR/share/config/tdenetworkmanagerrc 2>/dev/null
  fi
  if [ ! -f $PROFILE_DIR/share/config/tdepowersaverc ] && [ -f $PROFILE_DIR/share/config/kpowersaverc ]; then
    Log "  kpowersave->tdepowersave"
    cp -a $PROFILE_DIR/share/config/kpowersaverc $PROFILE_DIR/share/config/tdepowersaverc 2>/dev/null
  fi
fi
if [ "$R14_VERSION" -lt "201401050" ]; then
  if [ ! -f $PROFILE_DIR/share/config/tdedebugdialogrc ] && [ -f $PROFILE_DIR/share/config/kdebugdialogrc ]; then
    Log "  kdebugdialog->tdedebugdialog"
    mv $PROFILE_DIR/share/config/kdebugdialogrc $PROFILE_DIR/share/config/tdedebugdialogrc 2>/dev/null
  fi
fi
if [ "$R14_VERSION" -lt "201401150" ]; then
  if [ ! -f $PROFILE_DIR/share/config/tdefilereplacerc ] && [ -f $PROFILE_DIR/share/config/kfilereplacerc ]; then
    Log "  kfilereplace->tdefilereplace"
    mv $PROFILE_DIR/share/config/kfilereplacerc $PROFILE_DIR/share/config/tdefilereplacerc 2>/dev/null
  fi
fi

if [ "$R14_VERSION" -lt "201401052" ]; then
  # Rename the user's custom launcher menu.
  # Before the release of R14.0.0, custom user menus had the same file name as KDE4:
  # applications-kmenuedit.menu. Under that condition, there is a possibility the user
  # was using the same custom menu for both Trinity and KDE4. To distinguish whether a
  # custom menu is TDE or KDE4, search the menu for TDE-specific and KDE4-specific menu
  # items. There is a possibility the user has customized a common menu to include or
  # exclude all of these items. Later we will copy any existing custom menu.
  # There are three environments to inherit:
  # KDE4
  # KDE/TDE 3.5.x
  # TDE pre R14
  if [ -f $USER_DIR/.config/menus/applications-tdemenuedit.menu ]; then
    CUSTOM_MENU="TDE"
  elif [ -f $USER_DIR/.config/menus/applications-kmenuedit.menu ]; then
    if [ "`grep \"kde4-kfind.desktop\" $USER_DIR/.config/menus/applications-kmenuedit.menu`" ] || \
       [ "`grep \"kde4-Help.desktop\" $USER_DIR/.config/menus/applications-kmenuedit.menu`" ]; then
       CUSTOM_MENU="KDE4"
    elif [ "`grep \"kde-kfind.desktop\" $USER_DIR/.config/menus/applications-kmenuedit.menu`" ] || \
       [ "`grep \"kde-Help.desktop\" $USER_DIR/.config/menus/applications-kmenuedit.menu`" ]; then
       # KDE/TDE 3.5.x
       CUSTOM_MENU="KDE"
    elif [ "`grep \"tde-Kfind.desktop\" $USER_DIR/.config/menus/applications-kmenuedit.menu`" ] || \
       [ "`grep \"tde-Help.desktop\" $USER_DIR/.config/menus/applications-kmenuedit.menu`" ] || \
       [ "`grep \"tde-Home.desktop\" $USER_DIR/.config/menus/applications-kmenuedit.menu`" ]; then
       CUSTOM_MENU="TDE"
    fi

    # Now we know which type of environment. Copy the existing menu.
    Log "  kmenuedit.menu($CUSTOM_MENU)->tdemenuedit.menu"
    # Note: at this point the layout of a renamed KDE4 custom menu will not be the same as Trinity.
    # We'll fix that later.
    cp $USER_DIR/.config/menus/applications-kmenuedit.menu $USER_DIR/.config/menus/applications-tdemenuedit.menu 2>/dev/null
  fi
fi

if [ "$R14_VERSION" -lt "201401052" ]; then
  # Update the user's custom menu. Any custom menu should have been renamed a few lines above.
  if [ -r $USER_DIR/.config/menus/applications-tdemenuedit.menu ] && [ "$CUSTOM_MENU" != "TDE" ]; then
    # KDE/TDE 3.5.x or converted KDE4. No need to update a pre R14 menu.
    sed -i 's|<Filename>kde-|<Filename>tde-|g' $USER_DIR/.config/menus/applications-tdemenuedit.menu
  fi
fi

if [ "$R14_VERSION" -lt "201401130" ]; then
  Log "Updating profile *.desktop files."
  # First update *.desktop files in the Trinity profile folder. Updating these files is safe.
  find "$PROFILE_DIR" -name "*.desktop" -print0 2>/dev/null | \
    xargs -r0 grep -ZIl "\(X-KDE-\|KDE;\|kdesu\)" | \
    xargs -r0 sed -i -e "s|X-KDE-|X-TDE-|g" -e "s|KDE;|TDE;|g" -e "s|kdesu|tdesu|g"
  # Next update *.desktop files in $USER_DIR/.local.
  # Any existing applications-kmenuedit.menu has been copied to applications-tdemenuedit.menu.
  if [ -f "$USER_DIR/.config/menus/applications-tdemenuedit.menu" ] && [ "$CUSTOM_MENU" != "TDE" ]; then
    # KDE/TDE 3.5.x or converted KDE4. No need to update a pre R14 menu.
    Log "Updating ~/.local *.desktop files."
    find "$USER_DIR/.local" -name "*.desktop" -print0 2>/dev/null | \
      xargs -r0 grep -ZIl "\(X-KDE-\|KDE;\|kdesu\)" | \
      xargs -r0 sed -i -e "s|X-KDE-|X-TDE-|g" -e "s|KDE;|TDE;|g" -e "s|kdesu|tdesu|g"
    # When the custom menu is from KDE/TDE 3.5.x or pre R14, then we are done.
    # When the custom menu is from KDE4, we have additional work to perform because the KDE4
    # layout structure is different from the Trinity layout.
    # In limited tests the following menu items are out of place when migrating a KDE4 menu:
    # Edutainment (because KDE4 uses 'Education' rather than 'Edutainment'
    # Find Files/Folders (because of kde4- prefix rather than tde- prefix)
    # Help (because of kde4- prefix rather than tde- prefix)
    # Home - Personal Files (because of kde4- prefix rather than tde- prefix)
    # All four items are placed at the top of the migrated menu.
    # The following should fix the layout to conform to Trinity.
    if [ "$CUSTOM_MENU" = "KDE4" ]; then
      Log "Repairing menu layout structure."
      sed -i -e "s|Education|Edutainment|" -e "s|kde4-|tde-|g" $USER_DIR/.config/menus/applications-tdemenuedit.menu
      # The following must be performed in this order.
      sed -i "s|tde-Help.desktop|tde-Home.desktop|" $USER_DIR/.config/menus/applications-tdemenuedit.menu
      sed -i "s|tde-kfind.desktop|tde-Help.desktop|" $USER_DIR/.config/menus/applications-tdemenuedit.menu
      sed -i "s|<Menuname>Applications</Menuname>|<Filename>tde-Kfind.desktop</Filename>|" $USER_DIR/.config/menus/applications-tdemenuedit.menu
      # Also need to fix Lost+Found? Lost+Found is not in alphabetical order
      # when opening the menu editor against the converted menu.
    fi
  fi
fi

# Disable some features new to R14, otherwise users will see an unfamiliar desktop.
if [ "$R14_VERSION" -lt "201309150" ]; then
  if [ $($TDEDIR/bin/kreadconfig --file kickerrc --group "General" --key "ShowDeepButtons" --default false) = "false" ]; then
    $TDEDIR/bin/kwriteconfig --file kickerrc --group "General" --key "ShowDeepButtons" --type bool "false"
  fi
  if [ $($TDEDIR/bin/kreadconfig --file kickerrc --group "General" --key "UseResizeHandle" --default false) = "false" ]; then
    $TDEDIR/bin/kwriteconfig --file kickerrc --group "General" --key "UseResizeHandle" --type bool "false"
  fi
  if [ $($TDEDIR/bin/kreadconfig --file kickerrc --group "General" --key "MenubarPanelBlurred" --default false) = "false" ]; then
    $TDEDIR/bin/kwriteconfig --file kickerrc --group "General" --key "MenubarPanelBlurred" --type bool "false"
  fi
fi

# Perform some nominal update validations.
# First clean house from any previous failures.
if [ "$CACHE_DIR" = "" ]; then
  CACHE_DIR=$PROFILE_DIR
fi
if [ -d "$CACHE_DIR" ]; then
  rm -f ${CACHE_DIR}/${SCRIPT_NAME}-*test*.txt 2>/dev/null
else
  # Create this directory in case the migratekde3 script was run immediately
  # before this script, which means the cache directory will not yet exist.
  mkdir "$CACHE_DIR"
fi
# This first test includes *.desktop files in the profile Autostart directory.
TEST_NUM="1"
R14_UPDATE_TEST1=""
find "$PROFILE_DIR" -name "*.desktop" -print0 2>/dev/null | \
  xargs -r0 grep -IFl "X-KDE-" >${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt
if [ -s ${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt ]; then
  R14_UPDATE_TEST1="failed"
  MESSAGE="Some Trinity profile R14 XDG compliance updates failed.\nCheck *.desktop files for 'X-KDE' in\n${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt.\n\n"
  Validation_Failure
else
  rm -f ${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt 2>/dev/null
fi
TEST_NUM="2"
R14_UPDATE_TEST2=""
find "$PROFILE_DIR" -name "*.desktop" -print0 2>/dev/null | \
  xargs -r0 grep -IFl "KDE;" >${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt
if [ -s ${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt ]; then
  R14_UPDATE_TEST2="failed"
  MESSAGE="Some Trinity profile R14 XDG compliance updates failed.\nCheck *.desktop files for 'KDE;' in\n${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt.\n\n"
  Validation_Failure
else
  rm -f ${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt 2>/dev/null
fi
TEST_NUM="3"
R14_UPDATE_TEST3=""
find "$PROFILE_DIR" \
     -path $PROFILE_DIR/share/apps/amarok/albumcovers -prune -o \
     -path $PROFILE_DIR/share/apps/basket/baskets -prune -o \
     -path $PROFILE_DIR/share/apps/juk/covers -prune -o \
     -path $PROFILE_DIR/share/apps/kget/logs -prune -o \
     -path $PROFILE_DIR/share/apps/kmail/autosave -prune -o \
     -path $PROFILE_DIR/share/apps/kmail/dimap -prune -o \
     -path $PROFILE_DIR/share/apps/kmail/imap -prune -o \
     -path $PROFILE_DIR/share/apps/kmail/mail -prune -o \
     -path $PROFILE_DIR/share/apps/kmail/search -prune -o \
     -path $PROFILE_DIR/share/apps/knotes -prune -o \
     -path $PROFILE_DIR/share/apps/kopete/logs -prune -o \
     -type f -print0 2>/dev/null | \
  xargs -r0 grep -IFl "$TDEDIR/share/applications/kde" >${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt
if [ -s ${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt ]; then
  R14_UPDATE_TEST3="failed"
  MESSAGE="Some Trinity profile R14 XDG compliance updates failed.\nCheck files for '$TDEDIR/share/applications/kde' in\n${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt. "
  Validation_Failure
else
  rm -f ${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt 2>/dev/null
fi
TEST_NUM="4"
R14_UPDATE_TEST4=""
if [ -r "$PROFILE_DIR/share/config/khotkeysrc" ]; then
  grep "CommandURL=kde-" "$PROFILE_DIR/share/config/khotkeysrc" &>${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt
  if [ -s ${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt ]; then
    R14_UPDATE_TEST4="failed"
    MESSAGE="Some Trinity profile R14 XDG compliance updates failed.\nCheck khotkeysrc for 'CommandURL=kde-' in\n${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt. "
  else
    rm -f ${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt 2>/dev/null
  fi
fi
TEST_NUM="5"
R14_UPDATE_TEST5=""
if [ -r "$PROFILE_DIR/share/config/khotkeysrc" ]; then
  grep "K Menu - kde-" "$PROFILE_DIR/share/config/khotkeysrc" &>${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt
  if [ -s ${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt ]; then
    R14_UPDATE_TEST5="failed"
    MESSAGE="Some Trinity profile R14 XDG compliance updates failed.\nCheck khotkeysrc for 'K Menu - kde-' in\n${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt. "
    Validation_Failure
  else
    rm -f ${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt 2>/dev/null
  fi
fi
TEST_NUM="6"
R14_UPDATE_TEST6=""
if [ -r $PROFILE_DIR/share/config/profilerc ]; then
  grep "Application=kde-" "$PROFILE_DIR/share/config/profilerc" &>${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt
  if [ -s ${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt ]; then
    R14_UPDATE_TEST6="failed"
    MESSAGE="Some Trinity profile R14 XDG compliance updates failed.\nCheck profilerc for 'Application=kde-' in\n${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt. "
    Validation_Failure
  else
    rm -f ${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt 2>/dev/null
  fi
fi
TEST_NUM="7"
R14_UPDATE_TEST7=""
if [ -r $PROFILE_DIR/share/config/kickerrc ]; then
  grep "StorageId\[\$e\]=kde-" "$PROFILE_DIR/share/config/kickerrc" &>${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt
  if [ -s ${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt ]; then
    R14_UPDATE_TEST7="failed"
    MESSAGE="Some Trinity profile R14 XDG compliance updates failed.\nCheck kickerrc for 'StorageId[$e]=kde-' in\n${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt. "
    Validation_Failure
  else
    rm -f ${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt 2>/dev/null
  fi
fi
TEST_NUM="8"
R14_UPDATE_TEST8=""
if [ "$QUICK_LAUNCH_CONFIG" != "" ]; then
  grep "kde-" "$PROFILE_DIR/share/config/$QUICK_LAUNCH_CONFIG" &>${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt
  if [ -s ${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt ]; then
    R14_UPDATE_TEST8="failed"
    MESSAGE="Some Trinity profile R14 XDG compliance updates failed.\nCheck $QUICK_LAUNCH_CONFIG for 'kde-' in\n${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt. "
    Validation_Failure
  else
    rm -f ${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt 2>/dev/null
  fi
fi
TEST_NUM="9"
R14_UPDATE_TEST9=""
if [ -r $USER_DIR/.config/menus/applications-tdemenuedit.menu ]; then
  grep "<Filename>kde-" "$USER_DIR/.config/menus/applications-tdemenuedit.menu" &>${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt
  if [ -s ${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt ]; then
    R14_UPDATE_TEST9="failed"
    MESSAGE="Some Trinity profile R14 XDG compliance updates failed.\nCheck applications-tdemenuedit.menu for '<Filename>kde-' in\n${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt. "
    Validation_Failure
  else
    rm -f ${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt 2>/dev/null
  fi
fi
TEST_NUM="10"
R14_UPDATE_TEST10=""
find "$PROFILE_DIR" -name "*.desktop" -print0 2>/dev/null | \
  xargs -r0 grep -IFl "kdesu" >${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt
if [ -s ${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt ]; then
  R14_UPDATE_TEST10="failed"
  MESSAGE="Some Trinity profile R14 XDG compliance updates failed.\nCheck *.desktop files for 'kdesu' in\n${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt. "
  Validation_Failure
else
  rm -f ${CACHE_DIR}/${SCRIPT_NAME}-validation-test${TEST_NUM}.txt 2>/dev/null
fi
if [ "$R14_UPDATE_TEST1" = "" ] && [ "$R14_UPDATE_TEST2" = "" ] && [ "$R14_UPDATE_TEST3" = "" ] \
  && [ "$R14_UPDATE_TEST4" = "" ] && [ "$R14_UPDATE_TEST5" = "" ] && [ "$R14_UPDATE_TEST6" = "" ] \
  && [ "$R14_UPDATE_TEST7" = "" ] && [ "$R14_UPDATE_TEST8" = "" ] && [ "$R14_UPDATE_TEST9" = "" ] \
  && [ "$R14_UPDATE_TEST10" = "" ]; then
  $TDEDIR/bin/kwriteconfig --file "$PROFILE_DIR/share/config/kdeglobals" --group "R14 XDG Updates" --key Updated --type bool "true"
  $TDEDIR/bin/kwriteconfig --file "$PROFILE_DIR/share/config/kdeglobals" --group "R14 XDG Updates" --key Version "$SCRIPT_VERSION"
  Log "R14 XDG updates completed successfully."
else
  # Don't use the --type parameter here because the value no longer is boolean.
  $TDEDIR/bin/kwriteconfig --file "$PROFILE_DIR/share/config/kdeglobals" --group "R14 XDG Updates" --key Updated "$KDEGLOBALS_KEY_VALUE"
  MESSAGE="\n\nThe r14-xdg-update script did not complete successfully.\n\nThe script will run with each login until corrected.\n\nPlease contact an administrator or take appropriate\nadmininstrative action to correct the problem.\n\nThe error code is $KDEGLOBALS_KEY_VALUE.\n\nCommon failures include file and directory permissions.\n\n"
  # Are we in X? Display an X dialog explaining breakage.
  if [ "$USER_DIR" = "$HOME" ] && [ "$DISPLAY" != "" ]; then
    printf "%b" "$MESSAGE" | xmessage -center -file - -buttons OK > /dev/null 2>/dev/null
  else
    Message_Prefix
    printf "%b" "$MESSAGE"
  fi
fi

unset USER_DIR
unset PROFILE_DIR
unset R14_UPDATED
unset R14_VERSION
unset R14_UPDATE_TEST1
unset R14_UPDATE_TEST2
unset R14_UPDATE_TEST3
unset R14_UPDATE_TEST4
unset R14_UPDATE_TEST5
unset R14_UPDATE_TEST6
unset KDEGLOBALS_KEY_VALUE
unset TEST_NUM
unset SCRIPT_NAME
unset SCRIPT_VERSION
unset CUSTOM_MENU
exit 0