#!/bin/bash
#
###############################################################
#
# Restore the system to a usable and bootable installation
#
# Urs Beyerle
#
###############################################################

function usage() {

   ## Usage
   # ----------------------------------------------------------

   cat <<EOF

   Options:

    -h:       print this screen
    -psi:     customize for PSI Live CD

EOF

}

###############################################################

### read options from command-line
PSI=""
while [ $# -gt 0 ]; do

    case "$1" in
       -h)
            usage; exit;;
       -psi)
            PSI=true; shift; continue;;
       *)
            usage; exit;;
    esac

done

### check if I run inside my directory
if [ ! -x $( basename $0 ) ]; then
    echo "Please run $( basename $0 ) within its directory"
    exit 1
fi

# source livecd.conf
. livecd.conf

LIVECD_DIR=$( pwd )

### restore /etc/rc.d/rc.sysinit
cp -a /etc/rc.d/rc.sysinit.ori /etc/rc.d/rc.sysinit

### enable ssh again
chkconfig sshd on 2>/dev/null

### create link to liblinuxlive
mkdir -p /livecd/live
rm -f /livecd/live/liblinuxlive
ln -s $LIVECD_DIR/linux-live.sl/initrd/liblinuxlive /livecd/live/liblinuxlive

### restore /etc/resolv.conf
mv -f /etc/resolv.conf.ori /etc/resolv.conf

### move /etc/yum.repos.d, it will interfere with our yum.conf.psi
if [ -e /etc/yum.conf.psi ]; then
    if [ -d /etc/yum.repos.d ]; then
	rm -rf /etc/yum.repos.d.ori
	mv -f /etc/yum.repos.d /etc/yum.repos.d.ori
    fi
fi

### restore folders from /mini
echo "restore folders from /mini ..."
mv /mini/usr/share/doc/* /usr/share/doc/ 2>/dev/null
mv /mini/usr/share/backgrounds/images/* /usr/share/backgrounds/images/ 2>/dev/null
mv /mini/usr/share/anaconda/pixmaps/rnotes /usr/share/anaconda/pixmaps/ 2>/dev/null

java_src=$( rpm -ql jdk 2>/dev/null | grep /src.zip )
[ "$java_src" ]    && mv -v /mini/src.zip "$java_src"    2>/dev/null
java_demo=$( rpm -ql jdk | grep /demo$ | head -n 1 )
[ "$java_demo" ]   && mv -v /mini/demo    "$java_demo"   2>/dev/null 
java_sample=$( rpm -ql jdk | grep /sample$ | head -n 1 )
[ "$java_sample" ] && mv -v /mini/sample  "$java_sample" 2>/dev/null

java_src=$( rpm -ql j2sdk 2>/dev/null | grep /src.zip )
[ "$java_src" ]    && mv -v /mini/src.zip "$java_src"    2>/dev/null
java_demo=$( rpm -ql j2sdk | grep /demo$ | head -n 1 )
[ "$java_demo" ]   && mv -v /mini/demo    "$java_demo"   2>/dev/null 
java_sample=$( rpm -ql j2sdk | grep /sample$ | head -n 1 )
[ "$java_sample" ] && mv -v /mini/sample  "$java_sample" 2>/dev/null

if [ -f /mini/usr/share/info/bash.info.gz ]; then
    ./mini-livecd.sh -restore
fi

echo "done."
echo

### restore cronjobs
mv /etc/cron_backup/sysstat /etc/cron.d/ 2>/dev/null
mv /etc/cron_backup/00-makewhatis.cron.weekly /etc/cron.weekly/00-makewhatis.cron 2>/dev/null
mv /etc/cron_backup/* /etc/cron.daily/

### set root password again
[ "$SET_ROOTPWD" = "yes" ] && passwd
if [ "$SET_ROOTPWD" != "yes" ]; then
    echo "Please note:"
    echo "If you are not building the LiveCD in chroot,"
    echo "root password is unset. Run passwd to set it again."
fi
