#!/usr/bin/ksh93

#
# This file is a part of the NsCDE - Not so Common Desktop Environment
# Author: Hegel3DReloaded
# Licence: GPLv3
#

export TEXTDOMAINDIR="$NSCDE_ROOT/share/locale"
export TEXTDOMAIN="nscde-migrate-1x_to_2x"

whence -q gettext
if (($? == 0)); then
   text="gettext -s"
   textn="gettext -sn"
else
   text="print"
   textn="print -rn"
fi

if [ -f "$HOME/.NsCDE/.migrate_nscde_1x_2x_norun" ] && [ "x$1" == "x-i" ]; then
   $textn "File "; echo -ne "$HOME/.NsCDE/.migrate_nscde_1x_2x_norun"; $text " exists."
   $text "This script is refusing to run in the interactive mode while that file exists."
   exit 0
fi

if [ "x$1" == "x-i" ]; then
   trap "" INT
   trap "" TERM
   trap "" QUIT
   trap "" ABRT
   trap "" PIPE
   trap "" USR1
   trap "" USR2
   trap "" HUP SIGHUP
   interactive=1
else
   interactive=0
fi

cd "$HOME/.NsCDE"
if (( $? != 0)); then
   $textn "Error: cannot change directory to"; echo "$HOME/.NsCDE"
   sleep 3
   exit 4
fi

ISED="$NSCDE_TOOLSDIR/ised"
OS=$(uname -s)

if [ ! -x "$ISED" ]; then
   $text "Error: ised script wrapper not found. Check if \$NSCDE_TOOLSDIR is set."
   sleep 3
   exit 5
fi

function backup_current_dot_nscde
{
   if [ -d "$HOME/.NsCDE" ]; then
      confdir_size=$(du -ks "$HOME/.NsCDE" | awk '{ print $1 }')
      if (( $confdir_size > 204800 )); then
         $textn "Error: your "; echo -ne "$HOME/.NsCDE "; $text "directory has more then 200 MB. Something appears to be"
         $text "wrong with your NsCDE profile. Examine your profile, or back it up manually, and"
         $text "rerun migration script without \"-i\" option."
         sleep 3
         exit 2
      else
         cd "$HOME"
         if (($? != 0)); then
            $textn "Error: cannot change directory to "; echo "${HOME}."
	    sleep 3
            exit 6
         else
            stamp=$(date +%d-%m-%Y-%H-%M-%S)
            case $OS in
            Linux|NetBSD|DragonFly|FreeBSD)
               tar cpf dot_NsCDE_${stamp}.tar --exclude '.NsCDE/tmp/fvwm_mfl.sock' .NsCDE
               RETVAL1=$?
            ;;
            OpenBSD)
               tar -s '/.NsCDE\/tmp\/fvwm_mfl\.sock//' -cf dot_NsCDE_stamp.tar .NsCDE
               RETVAL1=$?
            ;;
            SunOS)
               tar cpf dot_NsCDE_${stamp}.tar .NsCDE
               true
               RETVAL1=$?
            ;;
            esac
            if (($RETVAL1 == 0)); then
               $textn "Backup copy of "; echo -ne "$HOME/.NsCDE "; $textn "saved as "; echo "$HOME/dot_NsCDE_${stamp}.tar."
               cd - > /dev/null
               if (($? != 0)); then
                  $textn "Error: cannot change directory back to "; echo "$HOME/.NsCDE"
                  sleep 3
                  exit 5
               fi
            else
               $textn "Error: backing up "; echo -ne "$HOME/.NsCDE"; $textn " exited with exit status "; echo "${RETVAL1}."
               sleep 3
               exit 3
            fi
         fi
      fi
   fi
}

function rename_config_files
{
   old_confs="NsCDE-Backer.conf NsCDE-Event.conf NsCDE-FinishInit.conf \
 NsCDE-Form.conf NsCDE-FrontPanel.conf NsCDE-Functions.conf NsCDE-IconMan.conf \
 NsCDE-Ident.conf NsCDE-Init.conf NsCDE-Keybindings.conf NsCDE-Main.conf \
 NsCDE-Menus.conf NsCDE-Monitors.conf NsCDE-Mousebindings.conf \
 NsCDE-Pager-LocalPager.conf NsCDE-Pager-WspLocPager.conf \
 NsCDE-Sandbox.conf NsCDE-Script.conf NsCDE-Style.conf"

   old_genconfs="NsCDE-Animate.conf NsCDE-Backdrops.conf NsCDE-Colorset.conf \
 NsCDE-Font-120dpi.conf NsCDE-Font-144dpi.conf NsCDE-Font-192dpi.conf \
 NsCDE-Font-75dpi.conf NsCDE-Font-96dpi.conf"

   old_locals="NsCDE-Animate.local NsCDE-Backdrops.local NsCDE-Backer.local \
 NsCDE-Colorset.local NsCDE-Event.local NsCDE-FinishInit.local NsCDE-Font-120dpi.local \
 NsCDE-Font-144dpi.local NsCDE-Font-192dpi.local NsCDE-Font-75dpi.local \
 NsCDE-Font-96dpi.local NsCDE-Form.local NsCDE-FrontPanel.local NsCDE-Functions.local \
 NsCDE-IconMan.local NsCDE-Ident.local NsCDE-Init.local NsCDE-Keybindings.local \
 NsCDE-Main.local NsCDE-Menus.local NsCDE-Monitors.local NsCDE-Mousebindings.local \
 NsCDE-Pager-LocalPager.local NsCDE-Pager-WspLocPager.local NsCDE-Sandbox.local \
 NsCDE-Script.local NsCDE-Style.local NsCDE-Subpanels.local"

   # Will be regenerated upon restart
   rm -f "NsCDE-Subpanels.conf"

   # Convert old configs which will become "fvwmconf" type.
   for oc in $old_confs
   do
      new_oc1="${oc##*NsCDE-}"
      new_oc2="${new_oc1/.conf/.fvwmconf}"
      new_oc="${new_oc2}"

      if [ -r "$HOME/.NsCDE/${oc}" ] [ -h "$HOME/.NsCDE/${oc}" ]; then
         $textn "Renaming "; echo -ne "$oc"; $textn " to "; echo "$new_oc ..."
         mv -f "$oc" "$new_oc"
         RETVAL=$?
         if (($RETVAL != 0)); then
            $textn "Error: Renaming "; echo -ne "${oc}"; $textn " to "; echo -ne "${new_oc}"; $textn " returned error "; echo "${RETVAL}."
	    sleep 3
	    exit 7
         fi
      fi
   done

   # Convert old configs which will become "fvwmgen" type.
   for og in $old_genconfs
   do
      new_og1="${og##*NsCDE-}"
      new_og2="${new_og1/.conf/.fvwmgen}"
      new_og="${new_og2}"

      if [ -r "$HOME/.NsCDE/${og}" ] || [ -h "$HOME/.NsCDE/${og}" ]; then
         $textn "Renaming "; echo -ne "$og"; $textn " to "; echo "$new_og ..."
         mv -f "$og" "$new_og"
         RETVAL=$?
         if (($RETVAL != 0)); then
            $textn "Error: Renaming "; echo -ne "${og}"; $textn " to "; echo -ne "${new_og}"; $textn " returned error "; echo "${RETVAL}."
	    sleep 3
	    exit 7
         fi
      fi
   done

   # Convert old configs which will become "fvwmlocal" type.
   for ol in $old_locals
   do
      new_ol1="${ol##*NsCDE-}"
      new_ol2="${new_ol1/.local/.fvwmlocal}"
      new_ol="${new_ol2}"

      if [ -r "$HOME/.NsCDE/${ol}" ] || [ -h "$HOME/.NsCDE/${ol}" ]; then
         $textn "Renaming "; echo -ne "$ol"; $textn " to "; echo "$new_ol ..."
         mv -f "$ol" "$new_ol"
         RETVAL=$?
         if (($RETVAL != 0)); then
            $textn "Error: Renaming "; echo -ne "${ol}"; $textn " to "; echo -ne "${new_ol}"; $textn " returned error "; echo "${RETVAL}."
	    sleep 3
	    exit 7
         fi
      fi
   done

   # Handle renaming of pager config.
   if [ -f "Pager-LocalPager.fvwmconf" ]; then
      $text "Renaming Pager-LocalPager.fvwmconf to LocalPager.fvwmconf"
      mv -f Pager-LocalPager.fvwmconf LocalPager.fvwmconf
   fi
   if [ -f "Pager-LocalPager.fvwmlocal" ]; then
      $text "Renaming Pager-LocalPager.fvwmlocal to LocalPager.fvwmlocal"
      mv -f Pager-LocalPager.fvwmlocal LocalPager.fvwmlocal
   fi
   if [ -f "Pager-WspLocPager.fvwmconf" ]; then
      $text "Renaming Pager-WspLocPager.fvwmconf to WspLocPager.fvwmconf"
      mv -f Pager-WspLocPager.fvwmconf WspLocPager.fvwmconf
   fi
   if [ -f "Pager-WspLocPager.fvwmlocal" ]; then
      $text "Renaming Pager-WspLocPager.fvwmlocal to WspLocPager.fvwmlocal"
      mv -f Pager-WspLocPager.fvwmlocal WspLocPager.fvwmlocal
   fi

   # Handle local for of the FrontPanel configuration if exists.
   if [ -f "FrontPanel.fvwmconf" ]; then
      if [ "x$FVWM_IS_FVWM3" == "x1" ]; then
         $text "Renaming FrontPanel.fvwmconf to FrontPanel.fvwm3.fvwmconf"
         mv -f FrontPanel.fvwmconf FrontPanel.fvwm3.fvwmconf
      else
         $text "Renaming FrontPanel.fvwmconf to FrontPanel.fvwm2.fvwmconf"
         mv -f FrontPanel.fvwmconf FrontPanel.fvwm2.fvwmconf
      fi
   fi

   # Relink fontsets
   for fo in Font-*dpi.fvwmgen
   do
      if [ -L "${fo}" ]; then
         base_fontset=$(readlink ${fo})
         if [ ! -f "fontsets/${base_fontset##*/}" ]; then
            if [ -f "${NSCDE_DATADIR}/fontsets/${base_fontset##*/}" ]; then
              $textn "Relinking "; echo -ne "${fo}"; $textn " to "; echo "${NSCDE_DATADIR}/fontsets/${base_fontset##*/}."
              ln -sf "${NSCDE_DATADIR}/fontsets/${base_fontset##*/}" "${fo}"
            fi
         fi
      fi
   done
}

function replace_pathnames_in_files
{
   # Start with bin
   cat "$1" | $ISED -c 's/\$NSCDE_ROOT\/bin\/confget\.py/\$NSCDE_TOOLSDIR\/confget/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/bin\/confget\.py/\${NSCDE_TOOLSDIR}\/confget/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/bin\/confget\.py/\$NSCDE_TOOLSDIR\/confget/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/bin\/confget\.py/\$\[NSCDE_TOOLSDIR\]\/confget/g' -o -f - > /tmp/_migrate_tmp0.$$

   cat /tmp/_migrate_tmp0.$$ | $ISED -c 's/\$NSCDE_ROOT\/bin\/confset\.py/\$NSCDE_TOOLSDIR\/confset/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/bin\/confset\.py/\${NSCDE_TOOLSDIR}\/confset/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/bin\/confset\.py/\$NSCDE_TOOLSDIR\/confset/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/bin\/confset\.py/\$\[NSCDE_TOOLSDIR\]\/confset/g' -o -f - > /tmp/_migrate_tmp1.$$

   OSMACH=$(uname -sm | tr ' ' '_')
   cat /tmp/_migrate_tmp1.$$ | $ISED -c "s/\$NSCDE_ROOT\/bin\/fpclock-$OSMACH/\$NSCDE_TOOLSDIR\/$OSMACH\/fpclock/g" -o -f - | \
   $ISED -c "s/\${NSCDE_ROOT}\/bin\/fpclock-$OSMACH/\${NSCDE_TOOLSDIR}\/$OSMACH\/fpclock/g" -o -f - | \
   $ISED -c "s/\/opt\/NsCDE\/bin\/fpclock-$OSMACH/\$NSCDE_TOOLSDIR\/$OSMACH\/fpclock/g" -o -f - | \
   $ISED -c "s/\$\[NSCDE_ROOT\]\/bin\/fpclock-$OSMACH/\$\[NSCDE_TOOLSDIR\]\/$OSMACH\/fpclock/g" -o -f - > /tmp/_migrate_tmp0.$$

   cat /tmp/_migrate_tmp0.$$ | $ISED -c "s/\$NSCDE_ROOT\/bin\/fpclock-\$(uname -s)_\$(uname -m)/\$NSCDE_TOOLSDIR\/$OSMACH\/fpclock/g" -o -f - | \
   $ISED -c "s/\${NSCDE_ROOT}\/bin\/fpclock-\$(uname -s)_\$(uname -m)/\${NSCDE_TOOLSDIR}\/$OSMACH\/fpclock/g" -o -f - | \
   $ISED -c "s/\/opt\/NsCDE\/bin\/fpclock-\$(uname -s)_\$(uname -m)/\$NSCDE_TOOLSDIR\/$OSMACH\/fpclock/g" -o -f - | \
   $ISED -c "s/\$\[NSCDE_ROOT\]\/bin\/fpclock-\$(uname -s)_\$(uname -m)/\$\[NSCDE_TOOLSDIR\]\/$OSMACH\/fpclock/g" -o -f - > /tmp/_migrate_tmp1.$$

   cat /tmp/_migrate_tmp1.$$ | $ISED -c 's/\$NSCDE_ROOT\/bin\/fpclock/\$NSCDE_TOOLSDIR\/fpclock/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/bin\/fpclock/\${NSCDE_TOOLSDIR}\/fpclock/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/bin\/fpclock/\$NSCDE_TOOLSDIR\/fpclock/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/bin\/fpclock/\$\[NSCDE_TOOLSDIR\]\/fpclock/g' -o -f - > /tmp/_migrate_tmp0.$$

   cat /tmp/_migrate_tmp0.$$ | $ISED -c 's/\$NSCDE_ROOT\/bin\/getdpi/\$NSCDE_TOOLSDIR\/getdpi/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/bin\/getdpi/\${NSCDE_TOOLSDIR}\/getdpi/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/bin\/getdpi/\$NSCDE_TOOLSDIR\/getdpi/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/bin\/getdpi/\$\[NSCDE_TOOLSDIR\]\/getdpi/g' -o -f - > /tmp/_migrate_tmp1.$$

   cat /tmp/_migrate_tmp1.$$ | $ISED -c 's/\$NSCDE_ROOT\/bin\/getfont/\$NSCDE_TOOLSDIR\/getfont/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/bin\/getfont/\${NSCDE_TOOLSDIR}\/getfont/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/bin\/getfont/\$NSCDE_TOOLSDIR\/getfont/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/bin\/getfont/\$\[NSCDE_TOOLSDIR\]\/getfont/g' -o -f - > /tmp/_migrate_tmp0.$$

   cat /tmp/_migrate_tmp0.$$ | $ISED -c 's/\$NSCDE_ROOT\/bin\/ised/\$NSCDE_TOOLSDIR\/ised/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/bin\/ised/\${NSCDE_TOOLSDIR}\/ised/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/bin\/ised/\$NSCDE_TOOLSDIR\/ised/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/bin\/ised/\$\[NSCDE_TOOLSDIR\]\/ised/g' -o -f - > /tmp/_migrate_tmp1.$$

   cat /tmp/_migrate_tmp1.$$ | $ISED -c 's/\$NSCDE_ROOT\/bin\/mkpagemenu/\$NSCDE_TOOLSDIR\/mkpagemenu/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/bin\/mkpagemenu/\${NSCDE_TOOLSDIR}\/mkpagemenu/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/bin\/mkpagemenu/\$NSCDE_TOOLSDIR\/mkpagemenu/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/bin\/mkpagemenu/\$\[NSCDE_TOOLSDIR\]\/mkpagemenu/g' -o -f - > /tmp/_migrate_tmp0.$$

   cat /tmp/_migrate_tmp0.$$ | $ISED -c 's/\$NSCDE_ROOT\/bin\/nscde_colorpicker/\$NSCDE_TOOLSDIR\/colorpicker/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/bin\/nscde_colorpicker/\${NSCDE_TOOLSDIR}\/colorpicker/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/bin\/nscde_colorpicker/\$NSCDE_TOOLSDIR\/colorpicker/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/bin\/nscde_colorpicker/\$\[NSCDE_TOOLSDIR\]\/colorpicker/g' -o -f - > /tmp/_migrate_tmp1.$$

   cat /tmp/_migrate_tmp1.$$ | $ISED -c 's/\$NSCDE_ROOT\/bin\/nscde_usleep/\$NSCDE_TOOLSDIR\/usleep/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/bin\/nscde_usleep/\${NSCDE_TOOLSDIR}\/usleep/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/bin\/nscde_usleep/\$NSCDE_TOOLSDIR\/usleep/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/bin\/nscde_usleep/\$\[NSCDE_TOOLSDIR\]\/usleep/g' -o -f - > /tmp/_migrate_tmp0.$$

   cat /tmp/_migrate_tmp0.$$ | $ISED -c 's/\$NSCDE_ROOT\/bin\/nscde_var_append/\$NSCDE_TOOLSDIR\/var_append/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/bin\/nscde_var_append/\${NSCDE_TOOLSDIR}\/var_append/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/bin\/nscde_var_append/\$NSCDE_TOOLSDIR\/var_append/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/bin\/nscde_var_append/\$\[NSCDE_TOOLSDIR\]\/var_append/g' -o -f - > /tmp/_migrate_tmp1.$$

   cat /tmp/_migrate_tmp1.$$ | $ISED -c 's/\$NSCDE_ROOT\/bin\/sysinfo\.py/\$NSCDE_TOOLSDIR\/sysinfo/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/bin\/sysinfo\.py/\${NSCDE_TOOLSDIR}\/sysinfo/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/bin\/sysinfo\.py/\$NSCDE_TOOLSDIR\/sysinfo/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/bin\/sysinfo\.py/\$\[NSCDE_TOOLSDIR\]\/sysinfo/g' -o -f - > /tmp/_migrate_tmp0.$$

   cat /tmp/_migrate_tmp0.$$ | $ISED -c 's/\$NSCDE_ROOT\/bin\/xdowrapper/\$NSCDE_TOOLSDIR\/xdowrapper/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/bin\/xdowrapper/\${NSCDE_TOOLSDIR}\/xdowrapper/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/bin\/xdowrapper/\$NSCDE_TOOLSDIR\/xdowrapper/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/bin\/xdowrapper/\$\[NSCDE_TOOLSDIR\]\/xdowrapper/g' -o -f - > /tmp/_migrate_tmp1.$$

   # Libexec
   cat /tmp/_migrate_tmp1.$$ | $ISED -c 's/\$NSCDE_ROOT\/libexec\/appfinder/\$NSCDE_TOOLSDIR\/appfinder/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/libexec\/appfinder/\${NSCDE_TOOLSDIR}\/appfinder/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/libexec\/appfinder/\$NSCDE_TOOLSDIR\/appfinder/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/libexec\/appfinder/\$\[NSCDE_TOOLSDIR\]\/appfinder/g' -o -f - > /tmp/_migrate_tmp0.$$

   cat /tmp/_migrate_tmp0.$$ | $ISED -c 's/\$NSCDE_ROOT\/libexec\/backdropmgr/\$NSCDE_TOOLSDIR\/backdropmgr/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/libexec\/backdropmgr/\${NSCDE_TOOLSDIR}\/backdropmgr/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/libexec\/backdropmgr/\$NSCDE_TOOLSDIR\/backdropmgr/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/libexec\/backdropmgr/\$\[NSCDE_TOOLSDIR\]\/backdropmgr/g' -o -f - > /tmp/_migrate_tmp1.$$

   cat /tmp/_migrate_tmp1.$$ | $ISED -c 's/\$NSCDE_ROOT\/libexec\/colormgr/\$NSCDE_TOOLSDIR\/colormgr/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/libexec\/colormgr/\${NSCDE_TOOLSDIR}\/colormgr/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/libexec\/colormgr/\$NSCDE_TOOLSDIR\/colormgr/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/libexec\/colormgr/\$\[NSCDE_TOOLSDIR\]\/colormgr/g' -o -f - > /tmp/_migrate_tmp0.$$

   cat /tmp/_migrate_tmp0.$$ | $ISED -c 's/\$NSCDE_ROOT\/libexec\/fontmgr/\$NSCDE_TOOLSDIR\/fontmgr/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/libexec\/fontmgr/\${NSCDE_TOOLSDIR}\/fontmgr/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/libexec\/fontmgr/\$NSCDE_TOOLSDIR\/fontmgr/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/libexec\/fontmgr/\$\[NSCDE_TOOLSDIR\]\/fontmgr/g' -o -f - > /tmp/_migrate_tmp1.$$

   cat /tmp/_migrate_tmp1.$$ | $ISED -c 's/\$NSCDE_ROOT\/libexec\/fpexec/\$NSCDE_TOOLSDIR\/fpexec/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/libexec\/fpexec/\${NSCDE_TOOLSDIR}\/fpexec/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/libexec\/fpexec/\$NSCDE_TOOLSDIR\/fpexec/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/libexec\/fpexec/\$\[NSCDE_TOOLSDIR\]\/fpexec/g' -o -f - > /tmp/_migrate_tmp0.$$

   cat /tmp/_migrate_tmp0.$$ | $ISED -c 's/\$NSCDE_ROOT\/libexec\/fp_manage_subpanel/\$NSCDE_TOOLSDIR\/fp_manage_subpanel/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/libexec\/fp_manage_subpanel/\${NSCDE_TOOLSDIR}\/fp_manage_subpanel/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/libexec\/fp_manage_subpanel/\$NSCDE_TOOLSDIR\/fp_manage_subpanel/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/libexec\/fp_manage_subpanel/\$\[NSCDE_TOOLSDIR\]\/fp_manage_subpanel/g' -o -f - > /tmp/_migrate_tmp1.$$

   cat /tmp/_migrate_tmp1.$$ | $ISED -c 's/\$NSCDE_ROOT\/libexec\/fpseticon/\$NSCDE_TOOLSDIR\/fpseticon/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/libexec\/fpseticon/\${NSCDE_TOOLSDIR}\/fpseticon/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/libexec\/fpseticon/\$NSCDE_TOOLSDIR\/fpseticon/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/libexec\/fpseticon/\$\[NSCDE_TOOLSDIR\]\/fpseticon/g' -o -f - > /tmp/_migrate_tmp0.$$

   cat /tmp/_migrate_tmp0.$$ | $ISED -c 's/\$NSCDE_ROOT\/libexec\/generate_app_menus/\$NSCDE_TOOLSDIR\/generate_app_menus/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/libexec\/generate_app_menus/\${NSCDE_TOOLSDIR}\/generate_app_menus/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/libexec\/generate_app_menus/\$NSCDE_TOOLSDIR\/generate_app_menus/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/libexec\/generate_app_menus/\$\[NSCDE_TOOLSDIR\]\/generate_app_menus/g' -o -f - > /tmp/_migrate_tmp1.$$

   cat /tmp/_migrate_tmp1.$$ | $ISED -c 's/\$NSCDE_ROOT\/libexec\/generate_subpanels/\$NSCDE_TOOLSDIR\/generate_subpanels/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/libexec\/generate_subpanels/\${NSCDE_TOOLSDIR}\/generate_subpanels/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/libexec\/generate_subpanels/\$NSCDE_TOOLSDIR\/generate_subpanels/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/libexec\/generate_subpanels/\$\[NSCDE_TOOLSDIR\]\/generate_subpanels/g' -o -f - > /tmp/_migrate_tmp0.$$

   cat /tmp/_migrate_tmp0.$$ | $ISED -c 's/\$NSCDE_ROOT\/libexec\/keymenu/\$NSCDE_TOOLSDIR\/keymenu/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/libexec\/keymenu/\${NSCDE_TOOLSDIR}\/keymenu/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/libexec\/keymenu/\$NSCDE_TOOLSDIR\/keymenu/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/libexec\/keymenu/\$\[NSCDE_TOOLSDIR\]\/keymenu/g' -o -f - > /tmp/_migrate_tmp1.$$

   cat /tmp/_migrate_tmp1.$$ | $ISED -c 's/\$NSCDE_ROOT\/libexec\/colorconv\.py/\$NSCDE_TOOLSDIR\/colorconv/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/libexec\/colorconv\.py/\${NSCDE_TOOLSDIR}\/colorconv/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/libexec\/colorconv\.py/\$NSCDE_TOOLSDIR\/colorconv/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/libexec\/colorconv\.py/\$\[NSCDE_TOOLSDIR\]\/colorconv/g' -o -f - > /tmp/_migrate_tmp0.$$

   cat /tmp/_migrate_tmp0.$$ | $ISED -c 's/\$NSCDE_ROOT\/libexec\/geticon\.py/\$NSCDE_TOOLSDIR\/geticon/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/libexec\/geticon\.py/\${NSCDE_TOOLSDIR}\/geticon/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/libexec\/geticon\.py/\$NSCDE_TOOLSDIR\/geticon/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/libexec\/geticon\.py/\$\[NSCDE_TOOLSDIR\]\/geticon/g' -o -f - > /tmp/_migrate_tmp1.$$

   cat /tmp/_migrate_tmp1.$$ | $ISED -c 's/\$NSCDE_ROOT\/libexec\/getla1\.py/\$NSCDE_TOOLSDIR\/getla1/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/libexec\/getla1\.py/\${NSCDE_TOOLSDIR}\/getla1/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/libexec\/getla1\.py/\$NSCDE_TOOLSDIR\/getla1/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/libexec\/getla1\.py/\$\[NSCDE_TOOLSDIR\]\/getla1/g' -o -f - > /tmp/_migrate_tmp0.$$

   cat /tmp/_migrate_tmp0.$$ | $ISED -c 's/\$NSCDE_ROOT\/libexec\/nscde-acpi/\$NSCDE_TOOLSDIR\/acpimgr/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/libexec\/nscde-acpi/\${NSCDE_TOOLSDIR}\/acpimgr/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/libexec\/nscde-acpi/\$NSCDE_TOOLSDIR\/acpimgr/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/libexec\/nscde-acpi/\$\[NSCDE_TOOLSDIR\]\/acpimgr/g' -o -f - > /tmp/_migrate_tmp1.$$

   cat /tmp/_migrate_tmp1.$$ | $ISED -c 's/\$NSCDE_ROOT\/libexec\/nscde_get_logical_screens/\$NSCDE_TOOLSDIR\/get_logical_screens/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/libexec\/nscde_get_logical_screens/\${NSCDE_TOOLSDIR}\/get_logical_screens/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/libexec\/nscde_get_logical_screens/\$NSCDE_TOOLSDIR\/get_logical_screens/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/libexec\/nscde_get_logical_screens/\$\[NSCDE_TOOLSDIR\]\/get_logical_screens/g' -o -f - > /tmp/_migrate_tmp0.$$

   cat /tmp/_migrate_tmp0.$$ | $ISED -c 's/\$NSCDE_ROOT\/libexec\/nscde_palette_colorgen\.py/\$NSCDE_TOOLSDIR\/palette_colorgen/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/libexec\/nscde_palette_colorgen\.py/\${NSCDE_TOOLSDIR}\/palette_colorgen/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/libexec\/nscde_palette_colorgen\.py/\$NSCDE_TOOLSDIR\/palette_colorgen/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/libexec\/nscde_palette_colorgen\.py/\$\[NSCDE_TOOLSDIR\]\/palette_colorgen/g' -o -f - > /tmp/_migrate_tmp1.$$

   cat /tmp/_migrate_tmp1.$$ | $ISED -c 's/\$NSCDE_ROOT\/libexec\/nscde_setup/\$NSCDE_TOOLSDIR\/bootstrap/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/libexec\/nscde_setup/\${NSCDE_TOOLSDIR}\/bootstrap/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/libexec\/nscde_setup/\$NSCDE_TOOLSDIR\/bootstrap/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/libexec\/nscde_setup/\$\[NSCDE_TOOLSDIR\]\/bootstrap/g' -o -f - > /tmp/_migrate_tmp0.$$

   cat /tmp/_migrate_tmp0.$$ | $ISED -c 's/\$NSCDE_ROOT\/libexec\/strip_icon_path/\$NSCDE_TOOLSDIR\/strip_icon_path/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/libexec\/strip_icon_path/\${NSCDE_TOOLSDIR}\/strip_icon_path/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/libexec\/strip_icon_path/\$NSCDE_TOOLSDIR\/strip_icon_path/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/libexec\/strip_icon_path/\$\[NSCDE_TOOLSDIR\]\/strip_icon_path/g' -o -f - > /tmp/_migrate_tmp1.$$

   cat /tmp/_migrate_tmp1.$$ | $ISED -c 's/\$NSCDE_ROOT\/libexec\/style_managers\.shlib/\$NSCDE_TOOLSDIR\/style_managers\.shlib/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/libexec\/style_managers\.shlib/\${NSCDE_TOOLSDIR}\/style_managers\.shlib/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/libexec\/style_managers\.shlib/\$NSCDE_TOOLSDIR\/style_managers\.shlib/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/libexec\/style_managers\.shlib/\$\[NSCDE_TOOLSDIR\]\/style_managers\.shlib/g' -o -f - > /tmp/_migrate_tmp0.$$

   cat /tmp/_migrate_tmp0.$$ | $ISED -c 's/\$NSCDE_ROOT\/libexec\/subpanel_menuitem_props/\$NSCDE_TOOLSDIR\/subpanel_menuitem_props/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/libexec\/subpanel_menuitem_props/\${NSCDE_TOOLSDIR}\/subpanel_menuitem_props/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/libexec\/subpanel_menuitem_props/\$NSCDE_TOOLSDIR\/subpanel_menuitem_props/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/libexec\/subpanel_menuitem_props/\$\[NSCDE_TOOLSDIR\]\/subpanel_menuitem_props/g' -o -f - > /tmp/_migrate_tmp1.$$

   cat /tmp/_migrate_tmp1.$$ | $ISED -c 's/\$NSCDE_ROOT\/libexec\/themegen.py/\$NSCDE_TOOLSDIR\/themegen/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/libexec\/themegen.py/\${NSCDE_TOOLSDIR}\/themegen/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/libexec\/themegen.py/\$NSCDE_TOOLSDIR\/themegen/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/libexec\/themegen.py/\$\[NSCDE_TOOLSDIR\]\/themegen/g' -o -f - > /tmp/_migrate_tmp0.$$

   cat /tmp/_migrate_tmp0.$$ | $ISED -c 's/\$NSCDE_ROOT\/libexec\/xrandr_backer/\$NSCDE_TOOLSDIR\/xrandr_backer/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/libexec\/xrandr_backer/\${NSCDE_TOOLSDIR}\/xrandr_backer/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/libexec\/xrandr_backer/\$NSCDE_TOOLSDIR\/xrandr_backer/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/libexec\/xrandr_backer/\$\[NSCDE_TOOLSDIR\]\/xrandr_backer/g' -o -f - > /tmp/_migrate_tmp1.$$

   # Share paths
   cat /tmp/_migrate_tmp1.$$ | $ISED -c 's/\$NSCDE_ROOT\/share\/config_templates\/integration/\$NSCDE_DATADIR\/integration/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/share\/config_templates\/integration/\${NSCDE_DATADIR}\/integration/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/share\/config_templates\/integration/\$NSCDE_DATADIR\/integration/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/share\/config_templates\/integration/\$\[NSCDE_DATADIR\]\/integration/g' -o -f - > /tmp/_migrate_tmp0.$$

   cat /tmp/_migrate_tmp0.$$ | $ISED -c 's/\$NSCDE_ROOT\/share\/config_templates/\$NSCDE_DATADIR\/config_templates/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/share\/config_templates/\${NSCDE_DATADIR}\/config_templates/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/share\/config_templates/\$NSCDE_DATADIR\/config_templates/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/share\/config_templates/\$\[NSCDE_DATADIR\]\/config_templates/g' -o -f - > /tmp/_migrate_tmp1.$$

   cat /tmp/_migrate_tmp1.$$ | $ISED -c 's/\$NSCDE_ROOT\/share\/palettes/\$NSCDE_DATADIR\/palettes/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/share\/palettes/\${NSCDE_DATADIR}\/palettes/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/share\/palettes/\$NSCDE_DATADIR\/palettes/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/share\/palettes/\$\[NSCDE_DATADIR\]\/palettes/g' -o -f - > /tmp/_migrate_tmp0.$$

   cat /tmp/_migrate_tmp0.$$ | $ISED -c 's/\$NSCDE_ROOT\/share\/backdrops/\$NSCDE_DATADIR\/backdrops/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/share\/backdrops/\${NSCDE_DATADIR}\/backdrops/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/share\/backdrops/\$NSCDE_DATADIR\/backdrops/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/share\/backdrops/\$\[NSCDE_DATADIR\]\/backdrops/g' -o -f - > /tmp/_migrate_tmp1.$$

   cat /tmp/_migrate_tmp1.$$ | $ISED -c 's/\$NSCDE_ROOT\/share\/photos/\$NSCDE_DATADIR\/photos/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/share\/photos/\${NSCDE_DATADIR}\/photos/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/share\/photos/\$NSCDE_DATADIR\/photos/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/share\/photos/\$\[NSCDE_DATADIR\]\/photos/g' -o -f - > /tmp/_migrate_tmp0.$$

   # Scripts
   cat /tmp/_migrate_tmp0.$$ | $ISED -c 's/\$NSCDE_ROOT\/lib\/scripts/\$NSCDE_LIBDIR\/scripts/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/lib\/scripts/\${NSCDE_LIBDIR}\/scripts/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/lib\/scripts/\$NSCDE_LIBDIR\/scripts/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/lib\/scripts/\$\[NSCDE_LIBDIR\]\/scripts/g' -o -f - > /tmp/_migrate_tmp1.$$

   # Config
   cat /tmp/_migrate_tmp1.$$ | $ISED -c 's/\$NSCDE_ROOT\/config\/AppMenus\.conf/\$NSCDE_DATADIR\/defaults\/AppMenus\.conf/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/config\/AppMenus\.conf/\${NSCDE_DATADIR}\/defaults\/AppMenus\.conf/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/config\/AppMenus\.conf/\$NSCDE_DATADIR\/defaults\/AppMenus\.conf/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/config\/AppMenus\.conf/\$\[NSCDE_DATADIR\]\/defaults\/AppMenus\.conf/g' -o -f - > /tmp/_migrate_tmp0.$$

   cat /tmp/_migrate_tmp0.$$ | $ISED -c 's/\$NSCDE_ROOT\/config\/FrontPanel\.actions/\$NSCDE_DATADIR\/defaults\/FrontPanel\.actions/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/config\/FrontPanel\.actions/\${NSCDE_DATADIR}\/defaults\/FrontPanel\.actions/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/config\/FrontPanel\.actions/\$NSCDE_DATADIR\/defaults\/FrontPanel\.actions/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/config\/FrontPanel\.actions/\$\[NSCDE_DATADIR\]\/defaults\/FrontPanel\.actions/g' -o -f - > /tmp/_migrate_tmp1.$$

   cat /tmp/_migrate_tmp1.$$ | $ISED -c 's/\$NSCDE_ROOT\/config\/Keymenu\.actions/\$NSCDE_DATADIR\/defaults\/Keymenu\.actions/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/config\/Keymenu\.actions/\${NSCDE_DATADIR}\/defaults\/Keymenu\.actions/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/config\/Keymenu\.actions/\$NSCDE_DATADIR\/defaults\/Keymenu\.actions/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/config\/Keymenu\.actions/\$\[NSCDE_DATADIR\]\/defaults\/Keymenu\.actions/g' -o -f - > /tmp/_migrate_tmp0.$$

   cat /tmp/_migrate_tmp0.$$ | $ISED -c 's/\$NSCDE_ROOT\/config\/Subpanels\.actions/\$NSCDE_DATADIR\/defaults\/Subpanels\.actions/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/config\/Subpanels\.actions/\${NSCDE_DATADIR}\/defaults\/Subpanels\.actions/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/config\/Subpanels\.actions/\$NSCDE_DATADIR\/defaults\/Subpanels\.actions/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/config\/Subpanels\.actions/\$\[NSCDE_DATADIR\]\/defaults\/Subpanels\.actions/g' -o -f - > /tmp/_migrate_tmp1.$$

   cat /tmp/_migrate_tmp1.$$ | $ISED -c 's/\$NSCDE_ROOT\/config\/WSM\.conf/\$NSCDE_DATADIR\/defaults\/WSM\.conf/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/config\/WSM\.conf/\${NSCDE_DATADIR}\/defaults\/WSM\.conf/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/config\/WSM\.conf/\$NSCDE_DATADIR\/defaults\/WSM\.conf/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/config\/WSM\.conf/\$\[NSCDE_DATADIR\]\/defaults\/WSM\.conf/g' -o -f - > /tmp/_migrate_tmp0.$$

   cat /tmp/_migrate_tmp0.$$ | $ISED -c 's/\$NSCDE_ROOT\/config/\$NSCDE_DATADIR\/fvwm/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/config/\${NSCDE_DATADIR}\/fvwm/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/config/\$NSCDE_DATADIR\/fvwm/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/config/\$\[NSCDE_DATADIR\]\/fvwm/g' -o -f - > /tmp/_migrate_tmp1.$$

   cat /tmp/_migrate_tmp1.$$ | $ISED -c 's/\$NSCDE_ROOT\/share\/icons\/NsCDE/\$NSCDE_DATADIR\/icons\/NsCDE/g' -o -f - | \
   $ISED -c 's/\${NSCDE_ROOT}\/share\/icons\/NsCDE/\${NSCDE_DATADIR}\/icons\/NsCDE/g' -o -f - | \
   $ISED -c 's/\/opt\/NsCDE\/share\/icons\/NsCDE/\$NSCDE_DATADIR\/icons\/NsCDE/g' -o -f - | \
   $ISED -c 's/\$\[NSCDE_ROOT\]\/share\/icons\/NsCDE/\$\[NSCDE_DATADIR\]\/icons\/NsCDE/g' -o -f - > /tmp/_migrate_tmp0.$$

   # Use cat(1) instead of mv(1) to preserve permissions and other file attributes.
   cat /tmp/_migrate_tmp0.$$ > $1
   rm -f /tmp/_migrate_tmp1.$$
}

function replace_confnames_in_files {
   old_confs_escaped="NsCDE-Backer\.conf NsCDE-Event\.conf NsCDE-FinishInit\.conf NsCDE-Form\.conf NsCDE-FrontPanel\.conf NsCDE-Functions\.conf NsCDE-IconMan\.conf NsCDE-Ident\.conf NsCDE-Init\.conf NsCDE-Keybindings\.conf NsCDE-Main\.conf NsCDE-Menus\.conf NsCDE-Monitors\.conf NsCDE-Mousebindings\.conf NsCDE-Pager-LocalPager\.conf NsCDE-Pager-WspLocPager\.conf NsCDE-Sandbox\.conf NsCDE-Script\.conf NsCDE-Style\.conf"

   old_genconfs_escaped="NsCDE-Animate\.conf NsCDE-Backdrops\.conf NsCDE-Colorset\.conf NsCDE-Font-120dpi\.conf NsCDE-Font-144dpi\.conf NsCDE-Font-192dpi\.conf NsCDE-Font-75dpi\.conf NsCDE-Font-96dpi\.conf"

   old_locals_escaped="NsCDE-Animate\.local NsCDE-Backdrops\.local NsCDE-Backer\.local NsCDE-Colorset\.local NsCDE-Event\.local NsCDE-FinishInit\.local NsCDE-Font-120dpi\.local NsCDE-Font-144dpi\.local NsCDE-Font-192dpi\.local NsCDE-Font-75dpi\.local NsCDE-Font-96dpi\.local NsCDE-Form\.local NsCDE-FrontPanel\.local NsCDE-Functions\.local NsCDE-IconMan\.local NsCDE-Ident\.local NsCDE-Init\.local NsCDE-Keybindings\.local NsCDE-Main\.local NsCDE-Menus\.local NsCDE-Monitors\.local NsCDE-Mousebindings\.local NsCDE-Pager-LocalPager\.local NsCDE-Pager-WspLocPager\.local NsCDE-Sandbox\.local NsCDE-Script\.local NsCDE-Style\.local NsCDE-Subpanels\.local"

   for oce in $old_confs_escaped
   do
      oce_new=$(echo "${oce}" | sed 's/^NsCDE-//g; s/\.conf/\.fvwmconf/g')
      $textn "Changing "; echo -ne "${oce/\\/}"; $textn " occurences to "; echo -ne "${oce_new/\\/}"; $text " in all regular files inside ~/.NsCDE"
      find . -xdev -type f ! -name "*.xpm" ! -name "*.png" ! -name "*.pm" ! -name "*.log" ! -name .BGdefault ! -name .Exec-History ! -name "*.pid" -exec $ISED -c "s/${oce}/${oce_new}/g" -f {} \;
   done

   for oge in $old_genconfs_escaped
   do
      oge_new=$(echo "${oge}" | sed 's/^NsCDE-//g; s/\.conf/\.fvwmgen/g')
      $textn "Changing "; echo -ne "${oge/\\/}"; $textn " occurences to "; echo -ne "${oge_new/\\/}"; $text " in all regular files inside ~/.NsCDE"
      find . -xdev -type f ! -name "*.xpm" ! -name "*.png" ! -name "*.pm" ! -name "*.log" ! -name .BGdefault ! -name .Exec-History ! -name "*.pid" -exec $ISED -c "s/${oge}/${oge_new}/g" -f {} \;
   done

   for ole in $old_locals_escaped
   do
      ole_new=$(echo "${ole}" | sed 's/^NsCDE-//g; s/\.local/\.fvwmlocal/g')
      $textn "Changing "; echo -ne "${ole/\\/}"; $textn " occurences to "; echo -ne "${ole_new/\\/}"; $text " in all regular files inside ~/.NsCDE"
      find . -xdev -type f ! -name "*.xpm" ! -name "*.png" ! -name "*.pm" ! -name "*.log" ! -name .BGdefault ! -name .Exec-History ! -name "*.pid" -exec $ISED -c "s/${ole}/${ole_new}/g" -f {} \;
   done
}

function walk_and_change_pathnames_in_files
{
   files_to_process=$(find . -xdev -type f ! -name "*.xpm" ! -name "*.png" ! -name "*.pm" ! -name "*.jpg" \
 ! -name "*.jpeg" ! -name "*.svg" ! -name "*.log" ! -name .BGdefault ! -name ".Exec-History" \
 ! -name "*.pid" ! -name "*.old" ! -name "Xdefaults*" ! -name "*SysActionDialog.lastval" \
 ! -name "WSM.conf" ! -name "Xsettingsd.conf" ! -name "GeoDB.ini" ! -name "Stalonetray.conf" \
 ! -name NsCDE-Subpanels.conf ! -name "*.fontset" ! -name ".FvwmConsole-History" \
 ! -name "*Font-*.fvwmgen" ! -name NsCDE.rasi | \
 egrep -v '\.\/(app-defaults|templates\/app-defaults|palettes|backdrops|photos|icons\/\.theme)')

   for ftp in $files_to_process
   do
      $textn "Processing "; echo -ne "$ftp"; $text " with pathname replacements for NsCDE 2.0"
      replace_pathnames_in_files "$ftp"
   done

   # Handling old share/icons/CDE/ as share/icons/NsCDE (best effort)
   if [ -r "FrontPanel.actions" ]; then
      $text "Processing icon paths in FrontPanel.actions"
      $ISED -c 's/\(^Btn.*\),\(CDE\)\(\/.*\.pm\),\(M1,.*,.*\)/\1,NsCDE\3,\4/g' -f FrontPanel.actions
      cat FrontPanel.actions | while IFS="," read Btn fpicon mice chk cmdrest
      do
         echo "$fpicon" | egrep -q 'NsCDE\/'
         if (($? == 0)); then
            $textn "Processing "; echo -ne "$fpicon"; $textn " for button "; echo "$Btn"
            if [ ! -f "$NSCDE_DATADIR/icons/$fpicon" ]; then
               fpicon_candidate=$(echo "$fpicon" | sed 's/SDt/Dt/g')
               if [ ! -f "$NSCDE_DATADIR/icons/$fpicon_candidate" ]; then
                  fpicon_candidate_two=$(echo "$fpicon_candidate" | sed 's/\.pm$/\.xpm/g')
                  if [ ! -f "$NSCDE_DATADIR/icons/$fpicon_candidate_two" ]; then
                     fpicon_candidate_three=$(echo "$fpicon" | sed 's/\.pm$/\.xpm/g')
                     if [ -f "$NSCDE_DATADIR/icons/$fpicon_candidate_three" ]; then
                        $ISED -c "s@$fpicon@$fpicon_candidate_three@g" -f FrontPanel.actions
                     fi
                  else
                     if [ -f "$NSCDE_DATADIR/icons/$fpicon_candidate_two" ]; then
                        $ISED -c "s@$fpicon@$fpicon_candidate_two@g" -f FrontPanel.actions
                     fi
                  fi
               else
                  if [ -f "$NSCDE_DATADIR/icons/$fpicon_candidate" ]; then
                     $ISED -c "s@$fpicon@$fpicon_candidate@g" -f FrontPanel.actions
                  fi
               fi
            fi
         fi
      done
   fi

   # Handling old share/icons/CDE/ as share/icons/NsCDE (best effort)
   if [ -r "Subpanels.actions" ]; then
      $text "Processing icon paths in Subpanels.actions"
      $ISED -c 's/\(^S[[:digit:]]\+,ENTRY,.*,.*,\)CDE\(\/.*\.pm,.*\)/\1NsCDE\2/g' -f Subpanels.actions
      cat Subpanels.actions | while IFS="," read S ENTRY name spec subicon cmdrest
      do
         echo "$subicon" | egrep -q '^NsCDE\/SDt.*\.pm'
         if (($? == 0)); then
            $textn "Processing "; echo -ne "$subicon"; $textn " for entry "; echo "$name"
            if [ ! -f "$NSCDE_DATADIR/icons/$subicon" ]; then
               subicon_candidate=$(echo "$subicon" | sed 's/SDt/Dt/g')
               if [ ! -f "$NSCDE_DATADIR/icons/$subicon_candidate" ]; then
                  subicon_candidate_two=$(echo "$subicon_candidate" | sed 's/\.pm$/\.xpm/g')
                  if [ ! -f "$NSCDE_DATADIR/icons/$subicon_candidate_two" ]; then
                     subicon_candidate_three=$(echo "$subicon" | sed 's/\.pm$/\.xpm/g')
                     if [ -f "$NSCDE_DATADIR/icons/$subicon_candidate_three" ]; then
                        $ISED -c "s@$subicon@$subicon_candidate_three@g" -f Subpanels.actions
                     fi
                  else
                     if [ -f "$NSCDE_DATADIR/icons/$subicon_candidate_two" ]; then
                        $ISED -c "s@$subicon@$subicon_candidate_two@g" -f Subpanels.actions
                     fi
                  fi
               else
                  if [ -f "$NSCDE_DATADIR/icons/$subicon_candidate" ]; then
                     $ISED -c "s@$subicon@$subicon_candidate@g" -f Subpanels.actions
                  fi
               fi
            fi
         fi
      done
   fi
}

function preface
{
   echo ""
   $text "It appears that you have ~/.NsCDE profile from NsCDE 1.x installation."
   $text "This migration script is here to convert your old profile to match"
   $text "new file names and paths of NsCDE 2.x."
   echo ""
   $text "This migration script wants to perform the following actions:"
   echo ""
   $text "* Backup your existing ~/.NsCDE into ~/dot_NsCDE_dd_mm_YY_HH_MM_SS.tar"
   $text "  just in case if anything goes wrong."
   echo ""
   $text "* Rename old names NsCDE-XYZ.conf into XYZ.fvwm(conf|gen) in ~/.NsCDE"
   echo ""
   $text "* Rename NsCDE-XYZ.local into XYZ.fvwmlocal in ~/.NsCDE"
   echo ""
   $text "* Examine regular configrations and scripts in ~/.NsCDE and replace"
   $text "  old names and paths to their new pathnames."
   echo ""
   $text "* Restart NsCDE in normal mode."
   echo ""

   PS3=$(gettext -s "Choose 1, 2 or 3: ")

   select action in "$($text 'Migrate')" "$($text 'Skip Once')" "$($text 'Abort')"
   do
      case $action in
      "$($text 'Abort')")
	 print ""
         $text "No automatic migration will be performed. This migration process will"
         $text "not run again. You will have to handle migration manually - either by"
         $text "running this script in non-interactive or interactive mode again after"
         $textn "removing "; echo "$HOME/.NsCDE/.migrate_nscde_1x_2x_norun"
	 $text "or by editing files."
	 print ""
	 $textn "Press return to cancel migration and exit. "
         read ret
         touch $HOME/.NsCDE/.migrate_nscde_1x_2x_norun
         exit 0
      ;;
   
      "$($text 'Skip Once')")
	 print ""
         $text "No automatic migration will be performed. This migration script will"
         $text "run again on next log in. To disable, choose Abort action from the menu."
         $text "This migration process will now restart NsCDE in normal mode. Many of"
         $text "your configurations will not work properly until files are not renamed"
         $text "and pathnames consolidated in them."
	 print ""
	 $textn "Press return to escape migration for now. "
         read ret
         exit 0
      ;;
   
      "$($text 'Migrate')")
         echo ""
         $text "Starting migration of your NsCDE profile ..."
         sleep 1
         echo ""
         sleep 1

         backup_current_dot_nscde
         rename_config_files
         replace_confnames_in_files
         walk_and_change_pathnames_in_files

         echo ""
         $text "Done."
         echo ""

         $text "ATTENTION:"
         $text "If you have used NsCDE CSS styling for Firefox and/or Thunderbird,"
         $text "you will have to either fix manually new symlink and @import paths"
         $text "in your Firefox and/or Thunderbird chrome subdirectory inside profile"
         $text "directory. The easiest way (if you don't have any other customizations"
         $text "there) is to remove all contents of chrome/ subdirectory and rerun"
         $text "Color Style Manager with the choosen color scheme to recreate NsCDE"
         $text "CSS customizations for this Mozilla applications."
         echo ""
         $text "Press return to restart NsCDE with migrated profile."
	 read ret
	 $text "Restarting ..."

         # Make sure there is no garbage here
         rm -f  $HOME/.NsCDE/Subpanels.fvwm3.fvwmgen
         rm -f  $HOME/.NsCDE/Subpanels.fvwm2.fvwmgen

         # Restart finally
         $NSCDE_ROOT/bin/nscde_fvwmclnt Restart
	 sleep 2

         exit $?
      ;;
   
      *)
         $text "Please try again: type 1, 2 or 3"
      ;; 
      esac
   done
}

if (( $interactive == 1 )); then
   preface
else
   backup_current_dot_nscde
   rename_config_files
   replace_confnames_in_files
   walk_and_change_pathnames_in_files
fi

