#!/bin/sh

# The proto_user is the user of which you've configured the KDE desktop
# and want other user's desktop to look like
PROTO_USER=user

FIND=/usr/bin/find
TAR=/bin/tar
CUT=/usr/bin/cut

# PROTO_USER's homedir (no need to change that) :
HOMEDIR=$(eval echo ~${PROTO_USER})

echo "Note: Using $PROTO_USER as modele (homedir : ${HOMEDIR})." 1>&2

# When the script ends (that means either on normal exit, or on SIGTERM)
trap "[ -f ${HOMEDIR}/dont-tar.lst ] && rm -f ${HOMEDIR}/dont-tar.lst" 0

# Displays a notice if the /etc/kde-config.tgz already exists and delete it
[ -f /etc/kde-config.tgz ] && { echo "/etc/kde-config.tgz already exists. \
Deleting..." 1>&2; rm -f /etc/kde-config.tgz; }

echo "Now building the archive..."

# Try to cd to user's homedir, exit if it fails
cd ${HOMEDIR}/ || { echo "Failed to cd $HOMEDIR !" 1>&2; exit 1; }

# We list all the symlinks because we don't want them in the tarball
$FIND . -type l -print | $CUT -c 3- > ${HOMEDIR}/dont-tar.lst || { \
echo "Failed to find the symlinks in ${HOMEDIR} !" 1>&2; exit 1; }

# Tar the .kde/ directory, exclude files (symlinks !) in dont-tar.lst
$TAR cvfzX /etc/kde-config.tgz ${HOMEDIR}/dont-tar.lst .kde/ >/dev/null \
|| { echo "Failed to build the tarball !"; exit 1; }

# Just for paranoia :)
chown root.root /etc/kde-config.tgz
chmod 0644 /etc/kde-config.tgz

echo "Done ! The tarball has been saved in /etc/kde-config.tgz." 1>&2
exit 0

