#
#  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 a script
#  $CONFIG_PATH/etc/LiveBackup/_configure which will be sourced
#  at the end of this scriplet
#  
# BUGS:
#        If the filecopy/symlink method is used, this will be always a dirty
#        hack and never work really well. This is the reason why an additional 
#        configuration hook 
#                                $CONFIG_PATH/etc/LiveBackup/_configure 
#        exists.
#
# part of the LiveBackup project:  
#
#                     http://livebackup.sourceforge.net/
#
# Copyright 2006 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
        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
        )

    else
        msg -w "$CONFIG_PATH not found, skipping startup configuration ....";
    fi

    if [ -f $CONFIG_PATH/etc/LiveBackup/_configure ]; then
        msg -i "executing: $CONFIG_PATH/etc/LiveBackup/_configure"
        . $CONFIG_PATH/etc/LiveBackup/_configure
    fi
fi



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