#!/bin/bash
. /usr/share/fog/lib/funcs.sh
### If USB Boot device we need a way to get the kernel args properly
if [[ $boottype == usb && ! -z $web ]]; then
    sysuuid=$(dmidecode -s system-uuid)
    sysuuid=${sysuuid,,}
    mac=$(getMACAddresses)
    base64mac=$(echo $mac | base64)
    token=$(curl -Lks --data "mac=$base64mac" "${web}status/hostgetkey.php")
    curl -Lks -o /tmp/hinfo.txt --data "sysuuid=${sysuuid}&mac=$mac&hosttoken=${token}" "${web}service/hostinfo.php" -A ''
    [[ -f /tmp/hinfo.txt ]] && . /tmp/hinfo.txt
fi
dots "Running post init scripts"
[[ ! -d /imagesinit ]] && mkdir /imagesinit >/dev/null 2>&1
umount /imagesinit >/dev/null 2>&1
mount -o nolock,proto=tcp,rsize=32768,wsize=32768,intr,noatime "$storage" /imagesinit >/tmp/mount-output 2>&1
if [[ $? -eq 0 ]]; then
    if [[ -f /imagesinit/.mntcheck ]]; then
        if [[ -f /imagesinit/postinitscripts/fog.postinit ]]; then
            postinitpath="/imagesinit/postinitscripts/"
        elif [[ -f /imagesinit/dev/postinitscripts/fog.postinit ]]; then
            postinitpath="/imagesinit/dev/postinitscripts/"
        fi
        . ${postinitpath}fog.postinit
    fi
    umount /imagesinit >/dev/null 2>&1
    echo "Done"
    debugPause
else
    echo "Skipped"
    debugPause
fi
if [[ -n $mode && $mode != +(*debug*) && -z $type ]]; then
    case $mode in
        wipe)
            fog.wipe
            ;;
        checkdisk)
            fog.testdisk
            ;;
        photorec)
            fog.photorec
            ;;
        badblocks)
            fog.surfacetest
            ;;
        clamav)
            fog.av
            ;;
        autoreg)
            fog.auto.reg
            ;;
        manreg)
            fog.man.reg
            ;;
        inventory)
            fog.inventory
            ;;
        capone)
            fog.capone
            ;;
        winpassreset)
            fog.chntpw
            ;;
        quickimage)
            fog.quickimage
            ;;
        sysinfo)
            fog.sysinfo
            ;;
        *)
            handleError "Fatal Error: Unknown mode :: $mode ($0)\n   Args Passed: $*"
            ;;
    esac
else
    case $type in
        down)
            fog.download
            ;;
        up)
            fog.upload
            ;;
        *)
            [[ -z $type ]] && type="Null"
            handleError "Fatal Error: Unknown request type :: $type"
            ;;
    esac
fi
