#!/bin/bash
#
###############################################################
#
# LiveCD customization script
# 
# Run this script on a SL Installation
# to prepare the system for a LiveCD 
#
# Urs Beyerle
#
###############################################################

### source livecd.conf
. livecd.conf

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

function usage() {

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

   cat <<EOF

   Optional Options:

    -h:       print this screen
    -psi:     customize for PSI Live CD
    -dvd:     customize for Live DVD
    -mini:    customize for Mini Live CD

EOF

}

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

###############################################################
# Definitions
###############################################################

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

    case "$1" in
       -h)
            usage; exit;;
       -psi)
            PSI=-psi; shift; continue;;
       -dvd)
            DVD=-dvd; shift; continue;;
       -mini)
            MINI=-mini; shift; continue;;
       *)
            usage; exit;;
    esac

done

### arch x86_64 or i686 ?
ARCH=$( /bin/arch )
[ "$ARCH" != "x86_64" ] && ARCH=i686

### set local username for PSI
[ $PSI ] && LOCALUSER=l_psi



###############################################################
# Backup some original files
###############################################################

echo "Backup original files ..."

ori_files="/etc/init.d/netfs \
           /etc/init.d/autofs \
           /etc/init.d/halt \
           /etc/init.d/network
           /etc/init.d/functions \
           /etc/rc.d/rc.sysinit \
           /etc/sysconfig/afs \
           /etc/motd \
           /etc/redhat-release \
	   /etc/rc.d/rc.local \
           /etc/resolv.conf"

for file in $ori_files; do
    [ ! -e ${file}.ori ] && cp -a ${file} ${file}.ori 2>/dev/null
done



###############################################################
# Configure system
###############################################################

echo "Configure system ..."

# build nvidia kernel modules 
[ -x /usr/sbin/mknvidia ] && mknvidia
dkms status -m nvidia 2>/dev/null | grep -q nvidia
if [ "$?" = "0" ]; then
    # already built?
    dkms status -m nvidia -k $KERNEL | grep installed
    if [ "$?" != "0" ]; then
	module_version=$( dkms status -m nvidia | cut -d"," -f2 | cut -d":" -f1 )
	dkms build -m nvidia -v $module_version -k $KERNEL
	dkms install -m nvidia -v $module_version -k $KERNEL
    fi
fi

# build vpnclient kernel module
running_kernel=$( uname -r )
if [ -x /usr/sbin/mkvpnclient ]; then
    if [ "$running_kernel" = "$KERNEL" ]; then
	mkvpnclient
    else
	if [ -e /lib/modules/$KERNEL/CiscoVPN/cisco_ipsec.ko ]; then
	    echo "VPN client kernel module found."
	    sleep 2
	else
	    echo "VPN client kernel module could not be build ..."
	    sleep 5
	fi
    fi
fi

# Update Virus definitions 
# run freshclam
if [ -x /usr/bin/freshclam ]; then
    echo; echo "Run /usr/bin/freshclam:"
    /etc/init.d/clamd status || /etc/init.d/clamd start && /usr/bin/freshclam
    /etc/init.d/clamd stop
    echo
fi
# update f-prot and uvscan
[ -x /usr/local/f-prot/tools/check-updates.pl ] && /usr/local/f-prot/tools/check-updates.pl
[ -x /usr/local/uvscan/update-dat ] && /usr/local/uvscan/update-dat

# run depmod -a for the LiveCD kernels
echo "Run depmod -a $KERNEL ..."
depmod -a $KERNEL
if [ $SMP ]; then
    echo "Run depmod -a ${KERNEL}smp ..."
    depmod -a ${KERNEL}smp
fi

# delete users (just to be sure)
userdel -r $LOCALUSER 2>/dev/null
userdel -r l_psi 2>/dev/null
userdel -r sluser 2>/dev/null
userdel -r user 2>/dev/null

# copy back /etc/yum.repos.d.ori to /etc/yum.repos.d
if [ -d /etc/yum.repos.d.ori ]; then
    cp -a /etc/yum.repos.d.ori /etc/yum.repos.d
fi

echo "done."
echo "--------------------------------------------"



###############################################################
# Clean up and delete files
###############################################################

echo "Cleaning up ..."

### set LANG
export LANG=C

### stop AFS
/etc/init.d/afs stop 2>/dev/null

### clean up yum
yum clean all >/dev/null
rm -rf /var/cache/yum/*

### remove cfengine log files
rm -f /var/cfengine/*log

### delete log files
find /var/log/ -type f -exec rm -f {} \;

### clean rpm database cache
rm -rf /var/lib/rpm/__db.*

### clean /var/spool/mail
rm -rf /var/spool/mail/*
touch /var/spool/mail/root
chmod 600 /var/spool/mail/root

### delete .rpmori .rpm Files in /etc
find /etc -name "*\.rpmorig" -exec rm -f {} \;
find /etc -name "*\.rpmnew"  -exec rm -f {} \;
find /etc -name "*\.rpmsave" -exec rm -f {} \;

### delete *~ files in /etc
find /etc | grep "~$" | while read f; do rm -f "$f"; done

### clean up /root
rm -f  /root/.bash_history
rm -f  /root/.lesshst
rm -f  /root/.rnd
rm -rf /root/.emacs.d
rm -f  /root/.ssh/known_hosts
rm -rf /root/.subversion

### clean AFS cache
if [ -d /usr/vice/cache ]; then
    rm -rf /usr/vice/cache
    mkdir /usr/vice/cache
fi
if [ -d /var/cache/openafs ]; then
    rm -rf /var/cache/openafs
    mkdir /var/cache/openafs
fi

### clean up /var/tmp
rm -rf /var/tmp/*

### remove root passwd
sed -i "s|^root:.*|root:\*:12943:0:99999:7:::|" /etc/shadow
sed -i "s|^root:.*:0:0|root:x:0:0|" /etc/passwd

### remove /.autofsck
rm -f /.autofsck

### disable cfagent 
[ $PSI ] && mv /etc/cron.d/cfengine      /etc/cron_backup/ 2>/dev/null

### disable psi-cronjobs
[ $PSI ] && mv /etc/cron.d/psi-cronjobs  /etc/cron_backup/ 2>/dev/null

### disable check_update
[ $PSI ] && sed -i "s|/etc/init.d/check_update|# /etc/init.d/check_update|" /etc/rc.d/rc.local.psi
[ $PSI ] && chkconfig check_update off

### create in /boot link to LiveCD kernel(s)
ln -fs /boot/vmlinuz-$KERNEL /boot/vmlinuz
[ $SMP ] && ln -fs /boot/vmlinuz-${KERNEL}smp /boot/vmlinuz${SMP}

### remove ssh keys
FILES_REMOVE="/etc/ssh/ssh_host_key \
              /etc/ssh/ssh_host_key.pub \
              /etc/ssh/ssh_host_rsa_key.pub \
              /etc/ssh/ssh_host_dsa_key \
              /etc/ssh/ssh_host_dsa_key.pub"
for file in $FILES_REMOVE; do
    rm -f $file
done

### remove useless cronjobs
mkdir -p /etc/cron_backup
mv /etc/cron.d/sysstat                 /etc/cron_backup/ 2>/dev/null
mv /etc/cron.weekly/00-makewhatis.cron /etc/cron_backup/00-makewhatis.cron.weekly 2>/dev/null
mv /etc/cron.daily/00-makewhatis.cron  /etc/cron_backup/ 2>/dev/null
mv /etc/cron.daily/makewhatis.cron     /etc/cron_backup/ 2>/dev/null
mv /etc/cron.daily/rpm                 /etc/cron_backup/ 2>/dev/null
mv /etc/cron.daily/slocate.cron        /etc/cron_backup/ 2>/dev/null
mv /etc/cron.daily/tetex.cron          /etc/cron_backup/ 2>/dev/null
mv /etc/cron.daily/yum.cron            /etc/cron_backup/ 2>/dev/null
mv /etc/cron.daily/prelink             /etc/cron_backup/ 2>/dev/null

### remove backup of /etc/X11/xorg.conf
rm -f /etc/X11/xorg.conf.ori    2>/dev/null
rm -f /etc/X11/xorg.conf.backup 2>/dev/null

### remove unused dirs in /lib/modules
ls /lib/modules/ | grep -v $KERNEL | while read mod_dir; do
    rpm -qf --quiet /lib/modules/$mod_dir
    if [ $? != "0" ]; then
	if [ $mod_dir != "" ]; then
	    cleaned_dir=/tmp/cleaned-$( date +%Y%m%d%H%M )
	    mkdir -p $cleaned_dir
            echo "Move dir /lib/modules/$mod_dir to $cleaned_dir"
	    mv -f /lib/modules/$mod_dir $cleaned_dir/
        fi
    fi
done

### move some unneeded files to /mini (not for LiveDVD and miniCD)
#   to save disk space

if [ ! $DVD ] && [ ! $MINI ]; then

    mkdir -p /mini

    echo "Move some folders to /mini, to save disk space:"

    # move some dirs in /usr/share/doc to /mini
    mkdir -p /mini/usr/share/doc
    mv -v /usr/share/doc/openafs-*        /mini/usr/share/doc 2>/dev/null
    mv -v /usr/share/doc/gcc-*            /mini/usr/share/doc 2>/dev/null
    mv -v /usr/share/doc/pine-*           /mini/usr/share/doc 2>/dev/null
    mv -v /usr/share/doc/ntp-*            /mini/usr/share/doc 2>/dev/null
    mv -v /usr/share/doc/cups-*           /mini/usr/share/doc 2>/dev/null
    mv -v /usr/share/doc/testdisk-*       /mini/usr/share/doc 2>/dev/null
    mv -v /usr/share/doc/htdig-*          /mini/usr/share/doc 2>/dev/null
    mv -v /usr/share/doc/gnome-vfs2-*     /mini/usr/share/doc 2>/dev/null
    mv -v /usr/share/doc/bash-*           /mini/usr/share/doc 2>/dev/null
    mv -v /usr/share/doc/exim-*           /mini/usr/share/doc 2>/dev/null
    mv -v /usr/share/doc/ghostscript-*    /mini/usr/share/doc 2>/dev/null
    mv -v /usr/share/doc/gnuplot-*        /mini/usr/share/doc 2>/dev/null
    mv -v /usr/share/doc/ImageMagick-*    /mini/usr/share/doc 2>/dev/null
    mv -v /usr/share/doc/isdn4k-utils-*   /mini/usr/share/doc 2>/dev/null
    mv -v /usr/share/doc/sane-backends-*  /mini/usr/share/doc 2>/dev/null
    mv -v /usr/share/doc/libsigc++20-*    /mini/usr/share/doc 2>/dev/null
    mv -v /usr/share/doc/selinux-policy-* /mini/usr/share/doc 2>/dev/null

    mkdir -p /mini/usr/share/backgrounds/images
    mv -v /usr/share/backgrounds/images/*.gz /mini/usr/share/backgrounds/images/ 2>/dev/null
    
    # move zipped java source to /mini, if j2sdk is installed
    java_src=$( rpm -ql j2sdk 2>/dev/null | grep src.zip )
    [ -e "$java_src" ] && mv -v "$java_src" /mini/
    
fi

echo "done."
echo "--------------------------------------------"



###############################################################
# Modify files
###############################################################

echo "Modify files ..."

### create /etc/sysconfig/clock, if not existing
if [ ! -e /etc/sysconfig/clock ]; then
    echo "UTC=false" >> /etc/sysconfig/clock
    echo "ARC=false" >> /etc/sysconfig/clock
fi

### remove AFS startup warning about cache
[ $PSI ] && sed  -i "/\!\!\!/d" /etc/init.d/afs

### disable umount of loop device during shutdown
#   this is done really dirty at the moment:
#    -> just replace "loop" with non existing device "lo_fake"

[ -e /etc/init.d/netfs ]     && sed -i "s|/loop/|/lo_fake/|g" /etc/init.d/netfs
[ -e /etc/init.d/autofs ]    && sed -i "s|/loop/|/lo_fake/|g" /etc/init.d/autofs
[ -e /etc/init.d/halt ]      && sed -i "s|/loop/|/lo_fake/|g" /etc/init.d/halt
[ -e /etc/init.d/functions ] && sed -i "s|/loop/|/lo_fake/|g" /etc/init.d/functions

# do not shuttdown loopback interface
sed -i "s|[^#]action \$\"Shutting down loopback interface:|\t#action \$\"Shutting down loopback interface:|" /etc/init.d/network

### suppress error message of already mounted devpts when doing mount -a
sed -i "s|mount -a -t nonfs,nfs4,smbfs,cifs,ncpfs,gfs$|mount -a -t nonfs,nfs4,smbfs,cifs,ncpfs,gfs 2>/dev/null|" /etc/init.d/netfs

### copy new /etc/init.d/halt 
cp -a customize/sl${OS_RELEASE}/halt /etc/init.d/halt

### in /etc/rc.d/rc.sysinit
#   comment out 'initlog -c "fsck -T -a $rootdev $fsckoptions"' 
#   to disable fsck of root filesystem
sed -i "s|\tfsck -T -a \$rootdev|\tsleep 0; #fsck -T -a \$rootdev|" /etc/rc.d/rc.sysinit
sed -i "s|\tinitlog -c \"fsck -T -a \$rootdev|\tsleep 0; #initlog -c \"fsck -T -a \$rootdev|" /etc/rc.d/rc.sysinit
#   disable "Remounting root filesystem in read-write mode"
sed -i "s| action \$\"Remounting root filesystem| sleep 0; #action \$\"Remounting root filesystem|" /etc/rc.d/rc.sysinit

### start afs with option -memcache !
if [ -e /etc/sysconfig/afs ]; then
    if [ $PSI ]; then
	sed -i "s|^EXTRA_OPTIONS=.*|EXTRA_OPTIONS='-fakestat -memcache'|" /etc/sysconfig/afs
	sed -i "s|ENABLE_MEMCACHE='no'|ENABLE_MEMCACHE='yes'|" /etc/sysconfig/afs
    else
	# for SL4
	grep -q "\-fakestat \-memcache" /etc/sysconfig/afs
	if [ "$?" != "0" ]; then
	    sed -i "s|-fakestat|-fakestat -memcache|" /etc/sysconfig/afs
	fi
	# for SL5
	rpm -q openafs | grep -q SL5
	if [ "$?" = "0" ]; then
	    sed -i "s|^OPTIONS=.*|OPTIONS=\"-memcache\"|" /etc/sysconfig/afs
	fi
    fi
fi

### source /etc/sysconfig/psi
[ $PSI ] && . /etc/sysconfig/psi

### Set /etc/motd
LiveCD="LiveCD"
[ "$ARCH" = "x86_64" ] && LiveCD="LiveCD 64bit"
echo "Welcome to $LIVECD_OS ${LiveCD}" > /etc/motd
[ $PSI ] && echo "Welcome to PSI ${LiveCD} (${CLASS} ${SUBCLASS} SL${RELEASE})" > /etc/motd
[ $DVD ]  && sed -i "s|CD|DVD|"    /etc/motd
[ $MINI ] && sed -i "s|CD|MiniCD|" /etc/motd

### Set hostname to psi or slinux (not really necessary)
if [ $PSI ]; then
    HOSTNAME="psi"
    sed -i "s/hostname=.*/hostname=${HOSTNAME}.psi.ch/" /etc/ssmtp/ssmtp.conf
    sed -i "s/HOSTNAME=.*/HOSTNAME=${HOSTNAME}/" /etc/sysconfig/psi
else
    HOSTNAME="$DEFAULT_HOSTNAME"
fi

### Change hostname
sed -i "s/HOSTNAME=.*/HOSTNAME=${HOSTNAME}/" /etc/sysconfig/network
sed -i "s/DHCP_HOSTNAME=.*/DHCP_HOSTNAME==${HOSTNAME}/" /etc/sysconfig/networking/devices/ifcfg-eth0 2>/dev/null

### Modify /etc/redhat-release
ADD=" - LiveCD"
[ $DVD ]  && ADD=" - LiveDVD"
[ $MINI ] && ADD=" - LiveMiniCD"
if [ -e /etc/redhat-release.ori ]; then
    echo "$( cat /etc/redhat-release.ori )${ADD}" > /etc/redhat-release
fi

### Set default runlevel to $RUNLEVEL
if [ $RUNLEVEL ]; then
    sed -i "s/id:.:initdefault:/id:$RUNLEVEL:initdefault:/" /etc/inittab
fi

### Edit /etc/sysconfig/desktop
if [ $DESKTOP ]; then
    sed -i "/^DESKTOP=.*/d" /etc/sysconfig/desktop 2&>/dev/null
    echo "DESKTOP=$DESKTOP" >> /etc/sysconfig/desktop
fi
if [ $DISPLAYMANAGER ]; then
    sed -i "/^DISPLAYMANAGER=.*/d" /etc/sysconfig/desktop 2&>/dev/null
    echo "DISPLAYMANAGER=$DISPLAYMANAGER" >> /etc/sysconfig/desktop
fi

### GDM login background
if [ -e /usr/share/gdm/themes/SL/background.png ]; then
    cp -a customize/sl${OS_RELEASE}/background.png /usr/share/gdm/themes/SL/background.png
fi

### KDE default background
if [ ! $PSI ]; then
    if [ -e /usr/share/backgrounds/images/default.png ]; then
	cp -a customize/sl${OS_RELEASE}/default.png /usr/share/backgrounds/images/default.png
    fi
fi

### KDE startup/exit sound
if [ -e /usr/share/config/knotify.eventsrc ]; then
    cp -a customize/sl/knotify.eventsrc /usr/share/config/knotify.eventsrc
    cp -a customize/sl/kmixrc /usr/share/config/kmixrc
fi

### Configure SELinux
if [ $SELINUX ]; then
    if [ -e /etc/selinux/config ]; then
	sed -i "s|^SELINUX=.*|SELINUX=$SELINUX|" /etc/selinux/config
    fi
fi

### Do not like jumping CD icon when starting autorun.desktop
if [ -e /etc/skel/.kde/Autostart/Autorun.desktop ]; then
    grep -q "StartupNotify=false" /etc/skel/.kde/Autostart/Autorun.desktop
    if [ "$?" != "0" ]; then
	echo "StartupNotify=false" >> /etc/skel/.kde/Autostart/Autorun.desktop
    fi
fi

### make thunderbird the default mail application in gnome, if evolution is not installed
rpm -q evolution >/dev/null 2>&1
if [ "$?" = "1" ]; then
    rpm -q thunderbird >/dev/null 2>&1
    if [ "$?" = "0" ]; then
	[ -e /etc/gconf/gconf.xml.defaults/%gconf-tree.xml ] && \
        sed -i.ori "s|evolution --component=mail|thunderbird -compose|" \
        /etc/gconf/gconf.xml.defaults/%gconf-tree.xml
	[ -e /etc/gconf/schemas/desktop_gnome_url_handlers.schemas ] && \
	sed -i.ori "s|evolution --component=mail|thunderbird -compose|" \
        /etc/gconf/schemas/desktop_gnome_url_handlers.schemas

	# the above may not work for SL4, this should work
	sed -i.ori "s|exec evolution|exec thunderbird|" /usr/bin/launchmail
    fi
fi

### fix a bug in dag's gparted
if [ -e /usr/bin/run-gparted ]; then
    grep -q "/bin/bash" /usr/bin/run-gparted
    if [ "$?" != "0" ]; then
	echo '#!/bin/bash'        > /usr/bin/run-gparted
	echo '/usr/sbin/gparted' >> /usr/bin/run-gparted
	chmod 755 /usr/bin/run-gparted
    fi
fi

echo "done."
echo "--------------------------------------------"



###############################################################
# Add-ons
###############################################################

echo "Add-ons ..."

### System icon on desktop
if [ -d /usr/share/apps/kdesktop/DesktopLinks ]; then
    cp -a customize/System.desktop /usr/share/apps/kdesktop/DesktopLinks/
fi

### files for PSI User, will be copied during bootup to /home/$LOCALUSER/
if [ $PSI ]; then
    rm -rf /usr/share/$LOCALUSER
    cp -a customize/$LOCALUSER /usr/share/
fi

### psi-menu, psi-scanvirus
if [ $PSI ]; then
    cp -a customize/psi/psi-menu /usr/bin/
    cp -a customize/psi/psi-scanvirus /usr/bin/
    [ -d /usr/local/uvscan ] && cp -a customize/psi/update-dat /usr/local/uvscan/
    [ -x /usr/bin/freshclam ] && cp -a customize/psi/psi-freshclam /usr/bin/
fi

### script to install LiveCD on local harddisk
cp -a customize/livecd-install  /usr/bin/

### GUI for livecd-install
cp -a customize/livecd-install-gui/livecd-install-gui /usr/sbin/
ln -fs consolehelper /usr/bin/livecd-install-gui
cp -a customize/livecd-install-gui/livecd-install-gui.desktop /usr/share/applications/
cp -a customize/livecd-install-gui/livecd-install-gui.console.apps /etc/security/console.apps/livecd-install-gui
cp -a customize/livecd-install-gui/livecd-install-gui.pam.d /etc/pam.d/livecd-install-gui

### livecd-mkinitrd (used by livecd-install)
if [ -e customize/sl${OS_RELEASE}/livecd-mkinitrd ]; then
    cp -a customize/sl${OS_RELEASE}/livecd-mkinitrd /usr/sbin/
else
    cp -a /sbin/mkinitrd /usr/sbin/livecd-mkinitrd
fi


echo "done."
echo "--------------------------------------------"


###############################################################
# Create missing files
###############################################################

echo "Create missing files ..."

###############################################################
# /etc/hosts
### copy host file, if not existing
[ -e /etc/hosts ] || cp -a customize/hosts /etc/hosts
[ -e /etc/sysconfig/networking/profiles/default/hosts ] || cp -a customize/hosts /etc/sysconfig/networking/profiles/default/hosts


###############################################################
# /etc/shadow
if [ ! -e /etc/shadow ]; then
    sed -i "s|\*|x|" /etc/passwd
    sed -i "s|^root::|root:x:|" /etc/passwd
    cat /etc/passwd | cut -d":" -f 1 | while read u; do echo "$u:*:12345:0:99999:1:::"; done >> /etc/shadow
    chmod 600 /etc/shadow
fi

# /etc/gshadow
if [ ! -e /etc/gshadow ]; then
    cp -a /etc/group /etc/gshadow
    sed -i "s|:x:|::|"       /etc/gshadow
    sed -i "s|:[0-9]\+:|::|" /etc/gshadow
    chmod 600 /etc/gshadow
fi

echo "done."
echo "--------------------------------------------"



###############################################################
# Create special files
###############################################################

echo "Create special files ..."

###############################################################
# /etc/rc.d/init.d/kudzu-auto
### Noninteractive HW detection and configuration

cp -a customize/kudzu-auto /etc/rc.d/init.d/kudzu-auto
chmod +x /etc/rc.d/init.d/kudzu-auto
chkconfig --add kudzu-auto
chkconfig kudzu-auto on


###############################################################
# /etc/rc.d/init.d/runveryfirst
### Fix some things during bootup - run VERY first
# runveryfirst will run at the begining of /etc/rc.d/rc.sysinit

cp -a customize/runveryfirst /etc/init.d/runveryfirst
chmod +x /etc/rc.d/init.d/runveryfirst

# execute runveryfirst just before "Initialize hardware"
grep -q runveryfirst /etc/rc.d/rc.sysinit
if [ "$?" != "0" ]; then
    sed -i -e "/^# Initialize hardware/a\/etc\/init.d\/runveryfirst" /etc/rc.d/rc.sysinit
fi


###############################################################
# /etc/rc.d/init.d/runfirst
### Fix some things during bootup - run first
# runfirst will run at the end of /etc/rc.d/rc.sysinit

cp -a customize/runfirst /etc/init.d/runfirst
chmod +x /etc/rc.d/init.d/runfirst

sysinit_line="/etc/rc.d/init.d/runfirst"
grep -q "$sysinit_line" /etc/rc.d/rc.sysinit
if [ "$?" != "0" ]; then
    echo "$sysinit_line" >> /etc/rc.d/rc.sysinit
    echo >> /etc/rc.d/rc.sysinit
fi


###############################################################
# /etc/rc.d/init.d/runlast
### Fix some things during bootup - run last

cp -a customize/runlast /etc/init.d/runlast
chmod +x /etc/rc.d/init.d/runlast

### Add /etc/rc.d/init.d/runlast to rc.local
LINE=/etc/rc.d/init.d/runlast
grep -q $LINE /etc/rc.d/rc.local
if [ "$?" != "0" ]; then
    # add line
    echo "" >> /etc/rc.d/rc.local
    echo $LINE >> /etc/rc.d/rc.local
    echo "" >> /etc/rc.d/rc.local    
fi


###############################################################
# /usr/bin/save-localdata
### stores data on a usbstick
cp -a customize/save-localdata /usr/bin/save-localdata
chmod +x /usr/bin/save-localdata

# add /usr/bin/save-localdata to /etc/sudoers
if [ -e /etc/sudoers ]; then
    grep -q "save-localdata" /etc/sudoers 2>/dev/null
    if [ "$?" != "0" ]; then
	cp -a /etc/sudoers /etc/sudoers.ori
	echo "$LOCALUSER ALL = NOPASSWD: /usr/bin/save-localdata" >> /etc/sudoers
    fi
fi

# create menu entry
cp -a customize/save-localdata.desktop /usr/share/applications/


###############################################################
# /usr/bin/set-volume
### unmute all mixers and set volumes
cp -a customize/set-volume /usr/bin/set-volume
chmod +x /usr/bin/set-volume


###############################################################
# /etc/sysconfig/networking/devices/ifcfg-eth0
# /etc/sysconfig/networking/devices/ifcfg-eth1

for iface in eth0 eth1; do 

    # remove it first
    rm -f /etc/sysconfig/networking/devices/ifcfg-${iface} 2>/dev/null
    rm -f /etc/sysconfig/networking/profiles/default/ifcfg-${iface} 2>/dev/null
    rm -f /etc/sysconfig/network-scripts/ifcfg-${iface} 2>/dev/null

    # create it, if we have a sample
    if [ -e customize/sl${OS_RELEASE}/ifcfg-${iface} ]; then
	cp -a customize/sl${OS_RELEASE}/ifcfg-${iface} /etc/sysconfig/networking/devices/ifcfg-${iface}
        # make hard links
	cp -lf /etc/sysconfig/networking/devices/ifcfg-${iface} /etc/sysconfig/networking/profiles/default/
	cp -lf /etc/sysconfig/networking/devices/ifcfg-${iface} /etc/sysconfig/network-scripts/
    fi
done


###############################################################
# /etc/profile.d/setsysfont.sh
### setsysfont 
cat > /etc/profile.d/setsysfont.sh <<EOF
# setsysfont once
if [ ! -e /tmp/.sysfont_has_been_set ]; then
    touch /tmp/.sysfont_has_been_set
    /bin/setfont 2>/dev/null
    /sbin/setsysfont 2>/dev/null
fi
EOF
chmod 755 /etc/profile.d/setsysfont.sh


###############################################################
# /etc/profile.d/setsysfont.csh
### setsysfont 
cat > /etc/profile.d/setsysfont.csh <<EOF
# setsysfont once
if ( ! -e /tmp/.sysfont_has_been_set ) then
    touch /tmp/.sysfont_has_been_set
    /bin/setfont 2>/dev/null
    /sbin/setsysfont 2>/dev/null
endif
EOF
chmod 755 /etc/profile.d/setsysfont.csh


###############################################################
# /etc/cron.d/psi
### PSI specific cronjobs

if [ $PSI ] && [ -e customize/psi/cron_psi.sl${OS_RELEASE} ]; then
    cp -a customize/psi/cron_psi.sl${OS_RELEASE} /etc/cron.d/psi 
    chmod +x /etc/cron.d/psi
fi

echo "done."
echo "--------------------------------------------"



###############################################################
# Configure services
###############################################################

echo "Configure services ..."

### services off
if [ $PSI ]; then
    chkconfig cfenvd off  2>/dev/null
    chkconfig cfexecd off 2>/dev/null
    chkconfig cfservd off 2>/dev/null
fi
if [ ! "$SERVICES_OFF" = "" ]; then
    for service in $SERVICES_OFF; do
	chkconfig $service off 2>/dev/null
    done
fi

### we do kudzu-auto
chkconfig kudzu off 

### services on
if [ ! "$SERVICES_ON" = "" ]; then
    for service in $SERVICES_ON; do
	chkconfig $service on 2>/dev/null
    done
fi

echo "done."
echo "--------------------------------------------"


###############################################################
# Empty files
###############################################################

echo "Empty files ..."

### /etc/security/users
if [ $PSI ]; then
    rm -f /etc/security/ssh.allow
    touch /etc/security/ssh.allow
fi

### Files to empty
FILES_TOUCH="/etc/sysconfig/hwconf \
             /etc/resolv.conf \
             /etc/adjtime \
             /etc/modprobe.conf \
             /etc/dhclient-eth0.conf"

for file in $FILES_TOUCH; do
    rm -rf $file
    touch $file
done

echo "done."
echo "--------------------------------------------"



###############################################################
# Update locate db, prelink, makewhatis
###############################################################

### run updatedb
if [ -x /usr/bin/updatedb ]; then
    echo "Run updatedb..."
    . /etc/updatedb.conf 2>/dev/null
    rpm -q mlocate >/dev/null
    if [ "$?" = "0" ]; then
	/usr/bin/updatedb -e "/media /sfs /tmp /boot /livecd /home /net /trunk"
    else
	/usr/bin/updatedb -e /media,/tmp,/boot,/livecd,/home,/net,/trunk
    fi
    echo "done."
fi

### run prelink
if [ -x /etc/cron.daily/prelink ]; then
    echo "Run prelink..."
    /etc/cron.daily/prelink
    echo "done."
fi
if [ -x /etc/cron_backup/prelink ]; then
    echo "Run prelink..."
    /etc/cron_backup/prelink
    echo "done."
fi

### clean prelink log
rm -f /var/log/prelink/prelink.log 2>/dev/null
rm -f /var/log/prelink.log 2>/dev/null

### run makewhatis
which makewhatis >/dev/null 2>&1
if [ "$?" = "0" ]; then
    echo "Run makewhatis..."
    makewhatis -u -w
fi

echo "done."
echo "--------------------------------------------"

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