#
#  Configure the live system. User specific files can i.e. be used 
#  by providing a
#      $CONFIG_PATH/<system>/etc/rcS.d/S00LiveBackup 
#  script
#
#  if you need some dirty hacks, you can provide hooks in
#
#    $CONFIG_PATH/etc/LiveBackup/initrd-hooks/_pre-configure 
#    $CONFIG_PATH/etc/LiveBackup/initrd-hooks/_post-configure
# 
#  these files are sourced beginning/end of this scriplet
#  
# BUGS:
#        If the filecopy/symlink method is used, this will be always a dirty
#        hack and never really work.
#
# part of the LiveBackup project:  
#
#                     http://livebackup.sourceforge.net/
#
# Copyright 2006-2010 by Wolfgang Rohrmoser <info@rohrmoser-engineering.de>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#


if [ "$LB_CONFIG" = "_default_" ]; then
    CONFIG_PATH="$MEDIA_MNT/config/$SYSTEM"
else
    CONFIG_PATH="$MEDIA_MNT/config/$LB_CONFIG"
fi


#
# copy/replace additional files
#
if [ "$LB_CONFIG" != "NO" ]; then

    if [ -d $CONFIG_PATH ]; then

        if [ -f $CONFIG_PATH/etc/LiveBackup/initrd-hooks/_pre-configure ]; then
            msg -i "execute pre-configuration script: $CONFIG_PATH/etc/LiveBackup/initrd-hooks/_pre-configure"
            . $CONFIG_PATH/etc/LiveBackup/initrd-hooks/_pre-configure
        fi

        msg -i "copy additional files from $CONFIG_PATH ....";
        (
            cd $CONFIG_PATH

	    if [ "$LB_METHOD" = "symlinks" ]; then
		# this may fail to create new directories
                # because we have links to the cdrom filesystem
		for file in $(find . -type d); do
		    if [ ! -d $file ]; then
			mkdir -p /$file > /dev/null 2>&1
		    fi
		done

		# try to copy as much as possible from root
		for file in $(find . -type f); do
		    if [ `basename $file` != "TRANS.TBL" ]; then
			overwrt $file /$file > /dev/null 2>&1
		    fi
		done

                if [ -d $CONFIG_PATH/home ]; then
                    # /home should be writable, try to copy everything 
                    ( 
                        cd $CONFIG_PATH/home
                        tar cpf - .  | (cd /home; tar xpf -)
                        for name in $(find . -name "TRANS.TBL"); do 
                            rm -rf /home/$name; 
                        done
                    )
                fi

	    else # not filecopy/symlink

                # copy everything and preserve permissions
                tar cpf - .  | (cd /; tar xpf -)
                # not known by busybox-tar:  --exclude "TRANS.TBL"
                for name in $(find . -name "TRANS.TBL"); do 
		    rm -rf /$name; 
		done
	    fi
        )

        if [ -f $CONFIG_PATH/etc/LiveBackup/initrd-hooks/_post-configure ]; then
            msg -i "execute post-configuration script: $CONFIG_PATH/etc/LiveBackup/initrd-hooks/_post-configure"
            . $CONFIG_PATH/etc/LiveBackup/initrd-hooks/_post-configure
        fi

    else
        msg -c "$CONFIG_PATH not found";
        echo "continue without configuration? (Y|n) : "
        read -t 10 ans
        if [ "$ans" = 'n' ] || [ "$ans" = 'N' ]; then
	    rescue_shell "please fix your configuration, boot continues after exit" 
        fi
    fi

fi



#
# Local Variables: 
# mode: shell-script
# End: 
#
