#!/bin/bash
# updateuser --- updates user's configuration files from 
#                   /usr/local/acl/etc/skel/
#            --- saves original versions in ~/oldconfigs/
# This will not work if /usr/local/acl/etc/skel/ includes any directories
#  which, themselves, include directories
# 
# As of Aug/2011 (gdm 2.30.2.is.2.30.0-0ubuntu5.2) we include an initial .dmrc
#  file in etc/skel which selects Xsession as the initial default session for 
#  new users.  Since we don't want to clobber the preferences of those who
#  are updating their dot files, this is not copied.
#

#SOURCEDIR=~jrogers/psg/psg/ACLskel/QUARK/usr/local/acl/etc/skel
SOURCEDIR=/usr/local/acl/etc/skel
TARGETDIR=~
BACKUPDIR=~/oldconfigs

EMACSDIR=/usr/local/acl/share/emacs/site-lisp

savefiles=""
copyfiles=""
copydirs=""
delfiles=""

# Choose files to copy files from $SOURCEDIR
for fname in $SOURCEDIR/* $SOURCEDIR/.* 
do
   bname=`basename $fname`
   if [ -f $fname -a -r $fname -a "$bname" != ".dmrc" \
       -a "$bname" != ".dontPrune" ] \
       && { [ ! -f $TARGETDIR/$bname ]  || ! cmp -s $fname $TARGETDIR/$bname; }
   then
###        echo "Top level file $bname"
       if [ -f $TARGETDIR/$bname ]
       then
           savefiles="$savefiles $bname"
       fi
       copyfiles="$copyfiles $bname"
   elif [ -d $fname -a "$bname" != "." -a  "$bname" != ".." -a "$bname" != "CVS" ]
   then
###        echo "$fname is directory"
       copydirs="$copydirs $bname"
       for fname2 in $fname/* $fname/.*
       do
###            echo $fname2
           bname2=`basename $fname2`
           if [ -f $fname2 -a -r $fname2 -a "$bname2" != ".dmrc"  \
               -a "$bname2" != ".dontPrune" ]  \
               && { ! -f $TARGETDIR/$fname2 ] || ! cmp -s $fname $TARGETDIR/$fname2; }
           then
###                echo "Second level file $bname/$bname2"
               if [ -f $TARGETDIR/$bname ]
               then
                   savefiles="$savefiles $bname/$bname2"
               fi
               copyfiles="$copyfiles $bname/$bname2"
           fi
       done
   fi
done

# Choose files to delete from $TARGETDIR/emacs that are in $EMACSDIR/
if [ -d $TARGETDIR/emacs ]
then
   for fname in $EMACSDIR/*
   do
       dname="emacs/`basename $fname`"
###        echo $dname
       if [ -f  $TARGETDIR/$dname ]
       then
###            echo "delete $TARGETDIR/$dname"
           delfiles="$delfiles $dname"
       fi
   done
   if [ -n "$delfiles" ]
   then
       copydirs="$copydirs emacs"
   fi
fi

# ... plus $TARGETDIR/.profile if it exists
if [ -f $TARGETDIR/.profile ]
then
###            echo "delete $TARGETDIR/.profile"
           delfiles="$delfiles .profile"
fi

echo
echo "===============         updateuser        ================"
echo "Updates your configuration files to their current versions"
echo
if [ -n "$copyfiles$dirfiles" ]
then
   echo "This will---"
   if [ -n "$copyfiles" ]
   then
       echo "- move the files: "
       echo  "$savefiles $delfiles"
       echo "-  from $TARGETDIR into $BACKUPDIR"
       echo "- then copy the files:"
       echo "$copyfiles"
       echo "-  from $SOURCEDIR into $TARGETDIR"
   fi
   if [ -n $delfiles ]
   then
   echo "- delete the files: "
   echo "$delfiles"
   echo "These can be restored from $BACKUPDIR, but do not restore any files"
   echo " that show up in $EMACSDIR."
   fi

   if [ -e $BACKUPDIR ]
   then
       echo "NOTE: $BACKUPDIR exists (probably with old configuration files)."
       echo "If you proceed they will be clobbered.  (This is probably OK.)"
   fi

   echo
   echo "Proceed [y/N]?"

   answer="n"
   rest=""

   read answer rest

   if [ -n "$rest" ]
   then
       echo "Please answer y or n"
       echo "No files were changed"
   elif [ "$answer" == "y" -o "$answer" == "Y" ]
   then
       ok=0 # 0 iff every operation so far has succeeded

   # Remove BACKUPDIR if it is an ordinary file
       if [ -f $BACKUPDIR ]
       then
           echo rm $BACKUPDIR
           rm $BACKUPDIR
           ok=$((ok | $?)) # bitwise or---logical ops get true/false backwards
       fi
   # Create BACKUPDIR if necessary
       if [ ! -e $BACKUPDIR ]
       then
           echo mkdir $BACKUPDIR
           mkdir $BACKUPDIR
           ok=$((ok | $?))
       fi
   # Create second level directories if needed
       for dname in $copydirs
       do
           if [ ! -e $BACKUPDIR/$dname ]
           then
               echo mkdir $BACKUPDIR/$dname
               mkdir $BACKUPDIR/$dname
               ok=$((ok | $?))
           fi
       done

       if [ $ok -eq 0 ]
       then
       # backup copyfiles
           for fname in $savefiles
           do
               echo cp -b $TARGETDIR/$fname $BACKUPDIR/
               cp -b $TARGETDIR/$fname $BACKUPDIR/
               ok=$((ok | $?))
           done
       fi

       if [ $ok -eq 0 ]
       then
       # backup delfiles
           for fname in $delfiles
           do
               echo cp -b $TARGETDIR/$fname $BACKUPDIR/$fname
               cp -b $TARGETDIR/$fname $BACKUPDIR/$fname
               ok=$((ok | $?))
           done
       fi

       if [ $ok -ne 0 ]
       then
           echo "*********** Problems backing up existing files *************"
       else
       # copy copyfiles
       # Create second level directories if needed
           for dname in $copydirs
           do
               if [ ! -e $TARGETDIR/$dname ]
               then
                   echo mkdir $TARGETDIR/$dname
                   mkdir $TARGETDIR/$dname
                   ok=$((ok | $?))
               fi
           done

           if [ $ok -eq 0 ]
           then
               for fname in $copyfiles
               do
                   echo cp $SOURCEDIR/$fname $TARGETDIR/
                   cp $SOURCEDIR/$fname $TARGETDIR/
                   ok=$((ok | $?))
               done
           fi

           if [ $ok -ne 0 ]
           then
               echo "*********** Problems installing new configuration files *************"
           else

           # delete delfiles
               for fname in $delfiles
               do
                   echo rm $TARGETDIR/$fname
                   rm $TARGETDIR/$fname
                   ok=$((ok | $?))
               done

               if [ $ok -ne 0 ]
               then
                   echo "*********** Problems deleting outdated configuration files *************"
               fi
           fi
       fi

       if [ $ok -ne 0 ]
       then
           echo "*********** There were errors in updating your files *************"
       else
           echo "+++++++++++ Update completed successfully. +++++++++++++"
       fi

   elif [ "$answer" != "n" -a "$answer" != "N" ]
   then
       echo "Please answer y or n"
       echo "No files were changed"
   else
       echo "No files were changed"
   fi
else
echo "Your configuration files are current. No files were changed"
fi


