#!/bin/bash

MYUSER=$1

if [ -z "$MYUSER" ] ; then
	echo " usage: $0 username "
	exit 1
fi

#abort if user doesn't exist - maybe we rebooted since then
getent passwd "$MYUSER"
if [ $? -ne 0 ] ; then
        echo "ERROR: error looking up user $MYUSER "
        exit 1
fi

HD=`getent passwd $MYUSER | cut -f 6 -d:`
HDNAME=$(basename $(getent passwd $MYUSER | cut -f 6 -d:))
#HD=/home/$MYUSER/

#abort this script if we can't cd to the users home dir
ls -lart /home/
cd $HD
if [ $? -ne 0 ] ; then
	echo "ERROR: can't cd $HD "
	exit 1
fi

#lock down panel just in case it was unlocked for editing
#su $MYUSER -c "gconftool-2 --type bool --set /apps/panel/global/locked_down true"

#get rid of stuff we never want
rm -rf $HD/.bash_history $HD/nohup.out $HD/.openoffice.org/*/.lock

#get rid of unnecessary things that are hard to transform to another user but may be useful in some cases
if [ "$MYUSER" = "groovixtemplateuser" ] ; then
        rm -rf $HD/.config/menus/xfce-applications.menu.undo*
        for T in .local/share/Trash .cache .pki ; do
                rm -rf $HD/$T
        done
fi

# clean up firefox 
#this prevents it from checking extension compatibility
perl -ni -e 'print unless (/extensions.lastAppVersion/)' $HD/.mozilla/firefox/*.default/prefs.js
perl -ni -e 'print unless (/extensions.lastPlatformVersion/)' $HD/.mozilla/firefox/*.default/prefs.js
#hopefully we'll never need to see the override url by using this trick:
ffprefset.pl user_pref browser.startup.homepage_override.mstone "\"ignore\"" $HD/.mozilla/firefox/*.default/prefs.js

############################################################################
# SYNC THE FILES
#find . | cpio --quiet -o > /var/lib/groovix/pac/home/$MYUSER.cpio
echo rsync -a --delete --exclude=.gvfs  $HD/ /var/lib/groovix/home/$MYUSER/ 
rsync -a --delete --exclude=.gvfs  $HD/ /var/lib/groovix/home/$MYUSER/
############################################################################

#make sure everything is owned by that user in case they have done something odd with chown/chmod
# /var/lib/groovix/home/ is set 700 so users can't do anything to the files under there even though they "own" them
chown -R $MYUSER.$MYUSER /var/lib/groovix/home/$MYUSER/



#all saved, remove save flag
rm /var/lib/groovix/session-info/$MYUSER.save-home

