# Local filesystem mounting			-*- shell-script -*-

. /scripts/functions-uniklu

# Parameter: Where to mount the filesystem
mountroot ()
{
    setterm -powerdown 0
    setterm -blank 0

    [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/installimage-top"
    run_scripts /scripts/installimage-top
    [ "$quiet" != "y" ] && log_end_msg
    
    
    [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/installimage-premount"
    run_scripts /scripts/installimage-premount
    [ "$quiet" != "y" ] && log_end_msg
    
    
    mkdir -p /target
    /bin/mount -L UNIKLU /target
    hddev=$(/bin/mount | gawk '/target/ {print $1}'| sort | uniq)
    /bin/umount /target
    
    mkdir -p /source
    # mount image path
    if  [ -z "$IMAGETYPE"  ]; then
	panic "IMAGETYPE should be cd: or cifs: !"
    fi
    
    if  [ "$IMAGETYPE" = "cifs"  ]; then
	/sbin/mount.cifs $IMAGELOCATION /source -o ip=$IMAGEIP,user=$IMAGEUSER
    fi
    if  [ "$IMAGETYPE" = "cd"  ]; then
	/bin/mount -o bind /cdrom /source 
    fi
    
    # activate swap:
    swapon -L SWAP
    
    # remove all references to hddev from mtab:
    /bin/umount /target
    grep -v target /etc/mtab > /tmp/mtab
    rm -f /etc/mtab
    cp /tmp/mtab /etc/mtab
    if [ ! -e /source/$IMAGENAME ]; then
	panic "partimage source file /source/$IMAGENAME not found!"
    fi

    if [ -n "$break" ]; then
        panic "before calling partimage"
    fi

    PARTIMAGE_FAILED=""
    partimage -b  restore $hddev /source/$IMAGENAME || PARTIMAGE_FAILED="yes"
    if [ "$PARTIMAGE_FAILED" = "yes" ]; then
        echo "partimage failed, now retrying in fully batch mode using command:"
	echo "partimage -b -B = restore $hddev /source/$IMAGENAME"
	partimage -b -B = restore $hddev /source/$IMAGENAME || \
	panic "partimage: restore failed again"
    fi


    # grow to full partition size
    echo "resize image to full partition size ..."
    ext2resize $hddev

    # fsck 
    if [ "$DOFSCK" = "yes" ]; then
	echo "force filesystem check..."
	fsck.ext2 -y -f $hddev
    fi


    # configure image
    tune2fs -L UNIKLU $hddev
    /bin/mount -L UNIKLU /target
    
    if [ ! -e /target/etc/multiboot/multiboot.cfg ]; then
	panic "extracted filesystem image not found in /target, something failed!"
    fi
    
#   rmdir /target/.var.run
#   rmdir /target/.var.lock
    mkdir -p /target/proc
    chroot /target /bin/mount -t proc proc proc
    
#   chroot /target /etc/init.d/multiboot-config runonce
#   chroot /target /etc/init.d/hwdetect
    
    MYHOSTNAME=$(cat /etc/hostname)
    MYFQDN=$(host $MYHOSTNAME | cut -f 1 -d ' ')
    
    sed -e "s/%MYHOSTNAME%/$MYHOSTNAME/g" /target/etc/hosts.template > /target/etc/hosts
    sed -e "s/%MYHOSTNAME%/$MYHOSTNAME/g" /target/etc/hostname.template > /target/etc/hostname
    
    cp -p /etc/resolv.conf /target/etc/resolv.conf
    cp -p /target/etc/udev/rules.d/70-persistent-net.rules.clean /target/etc/udev/rules.d/70-persistent-net.rules
    cat /target/etc/shadow | sed -e 's/^root:.*$/root:*:14123:0:99999:7:::/' > /tmp/shadow
    mv /tmp/shadow /target/etc/shadow
    chmod 0640 /target/etc/shadow
    chroot /target chown root.shadow /etc/shadow
    
    chroot /target /sbin/cups-cron.sh force $MYFQDN
    
    swapoff -L SWAP
    sync
    sync
    /bin/umount /target/proc
    /bin/umount /target
    
    
    [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/installimage-bottom"
    run_scripts /scripts/installimage-bottom
    [ "$quiet" != "y" ] && log_end_msg

    # reboot:
    
    /bin/umount /target
    echo s > /proc/sysrq-trigger
    echo u > /proc/sysrq-trigger
    echo b > /proc/sysrq-trigger
    
}
