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

. /scripts/functions-uniklu

# Parameter: Where to mount the filesystem
mountroot ()
{
  # stop splash
    if [ -x /sbin/usplash_write ]; then
        /sbin/usplash_write "QUIT"
    fi
    chvt 1
    if [ -x /bin/plymouth ] && plymouth --ping; then
        /bin/plymouth hide-splash > /dev/null 2>&1
    fi


    if [ -n "$BREAK" ]; then
        panic "start mountroot (installimage)"
    fi
    # disable screen saver
    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
    
    if [ -n "$BREAK" ]; then
        panic "after installimagei-top"
    fi
    
    [ "$quiet" != "y" ] && log_begin_msg "Running /scripts/installimage-premount"
    run_scripts /scripts/installimage-premount
    [ "$quiet" != "y" ] && log_end_msg

    if [ -n "$BREAK" ]; then
        panic "after installimage-premount"
    fi
    
    
    # premount should have prepared UNIKLU filesystem 
    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:, cifs:, http:, ... !"
    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
    
########## partimage
    if  [ "$IMAGEFORMAT" = "partimage"  ]; then
    # remove all references to hddev from mtab: (only needed for partimage)
        /bin/umount /target
        /bin/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 ..."
        resize2fs $hddev

    # fsck 
        if [ "$DOFSCK" = "yes" ]; then
	    echo "force filesystem check..."
	    fsck.ext2 -y -f $hddev
        fi
    fi
########## partimage
    if  [ "$IMAGEFORMAT" = "tarbz2"  ]; then
      /bin/mount -L $PART1LABEL /target
      if [ -n "$PART2LABEL" -a "$PART2FSTYPE" != "swap" ]; then
        mkdir -p /target/$PART2MOUNT
        /bin/mount -L $PART2LABEL /target/$PART2MOUNT
        if [ -n "$PART3LABEL" -a "$PART3FSTYPE" != "swap" ]; then
          mkdir -p /target/$PART3MOUNT
          /bin/mount -L $PART3LABEL /target/$PART3MOUNT
          if [ -n "$PART4LABEL" -a "$PART4FSTYPE" != "swap" ]; then
            mkdir -p /target/$PART4MOUNT
            /bin/mount -L $PART4LABEL /target/$PART4MOUNT
            if [ -n "$PART5LABEL" -a "$PART5FSTYPE" != "swap" ]; then
              mkdir -p /target/$PART5MOUNT
              /bin/mount -L $PART5LABEL /target/$PART5MOUNT
              if [ -n "$PART6LABEL" -a "$PART6FSTYPE" != "swap" ]; then
                mkdir -p /target/$PART6MOUNT
                /bin/mount -L $PART6LABEL /target/$PART6MOUNT
                if [ -n "$PART7LABEL" -a "$PART7FSTYPE" != "swap" ]; then
                  mkdir -p /target/$PART7MOUNT
                  /bin/mount -L $PART7LABEL /target/$PART7MOUNT
                fi
              fi
            fi
          fi
        fi
      fi
      if [ -n "$BREAK" ]; then
        panic "after mounting all filesystems"
      fi
      if  [ "$IMAGETYPE" = "http"  ]; then
          /bin/wget --progress=bar http://$IMAGEIP/$IMAGELOCATION/$IMAGENAME -O - | tar xjpf - -C /target 
      fi
      if  [ "$IMAGETYPE" = "cifs"  ]; then
          /bin/tar xjpf /source/$IMAGENAME -C /target
      fi
      if [ -n "$BREAK" ]; then
        panic "after untar"
      fi
      cp -p /tmp/fstab /target/etc/fstab
    fi    
    # configure image
        if  [ "$IMAGEFORMAT" = "partimage"  ]; then
            tune2fs -L UNIKLU $hddev
            /bin/mount -L UNIKLU /target
        fi
#    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
    
    #FIXME: ?? cp -p /etc/resolv.conf /target/etc/resolv.conf
    #TODO: 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:
    
      if [ -n "$PART7LABEL" -a "$PART7FSTYPE" != "swap" ]; then
        /bin/umount /target/$PART7MOUNT
      fi
      if [ -n "$PART6LABEL" -a "$PART6FSTYPE" != "swap" ]; then
        /bin/umount /target/$PART6MOUNT
      fi
      if [ -n "$PART5LABEL" -a "$PART5FSTYPE" != "swap" ]; then
        /bin/umount /target/$PART5MOUNT
      fi
      if [ -n "$PART4LABEL" -a "$PART4FSTYPE" != "swap" ]; then
        /bin/umount /target/$PART4MOUNT
      fi
      if [ -n "$PART3LABEL" -a "$PART3FSTYPE" != "swap" ]; then
        /bin/umount /target/$PART3MOUNT
      fi
      if [ -n "$PART2LABEL" -a "$PART2FSTYPE" != "swap" ]; then
        /bin/umount /target/$PART2MOUNT
      fi
      /bin/umount /target
    echo s > /proc/sysrq-trigger
    echo u > /proc/sysrq-trigger
    echo b > /proc/sysrq-trigger
    
}
