#!/bin/bash
export TERM=vt100
MVHOSTNAME=larch5
. /etc/profile
TEMP_TEMPLATES=/tmp/templates
disk=$2
rootfs=$3
rootsize=$4
datafs=$5
datasize=$6
swapsize=$7
mountpoint="/new_boot"
run=$MV_ROOT/bin/install_functions.sh
. /etc/systemconfig 2>/dev/null
. $MV_ROOT/bin/networkconfig.sh
CMDLINE=$(cat /proc/cmdline)
BACKUPFILE="/myth/backup/mythconverg.sql.gz"


progress () {
    mypercent=`echo "$1" | cut -d. -f1`
    if [  x$mypercent = x ]
    then
    echo "" > /tmp/.install_percent
    else
    echo "${mypercent}% complete" > /tmp/.install_percent
    fi
}


case $1 in
    full_install_it )
             mv_install.py --rootdisk=$disk --rootfs=$rootfs --rootsize=$rootsize --datafs=$datafs --datasize=$datasize --datadisk=$disk --swapsize=$swapsize -c full_install
             rc=$?
             echo "mv_install.py exit code $rc"
             if [ $rc != 0 ]
             then
                echo "mv_install error" >> /tmp/.install_error
             fi
             exit $rc
            ;;

    NETBOOT)
            echo "-----------------------------------------------------------------------3-----"
            netboot-full_install
            ;;

    find_update)
            mv_install.py --rootdisk=$disk -c find_upgrade
            ;;

    upgrade_it)
            mv_install.py --rootdisk=$disk --rootfs=$rootfs -c upgrade
            ;;

    disk_model_it)
            model="unknown"
            size="xxx"
            case $2 in
                h* )
                        model=`cat /proc/ide/$2/model`
                        ;;
                s*)     model=`sginfo /dev/$2|grep Product|awk ' { print $2 } '`
                        ;;
            esac
            size=`parted /dev/$2  print |grep Disk|awk  ' { print $3 } '  `
            echo ""$model" $size " >/tmp/model
            ;;

    network_check_it)
            #Should never return from check_network
            case $2 in
                    setup_network)
                                vnc_check
                                install_network_setup
                    ;;

                    check_self)
                                myipdefault=`ifconfig  | grep -C1 $default_interface| grep -v $default_interface | cut -d: -f2 | awk '{ print $1}'`
                                echo "network check myself"
                                ifconfig
                                ping -c 1 $myipdefault
                                if [ ! $? = 0 ]
                                then
                                    exit 3
                                    #can't ping yourself
                                fi
                    ;;
                    check_gw)
                                #check gateway
                                echo "network check gateway"
                                /sbin/route -n
                                mygwdefault=`/sbin/route -n  |grep $default_interface|grep UG|awk '{ print $2}'`
                                echo "Default gateway seems to be $mygwdefault"
                                if  [ !  x$mygwdefault = x ]
                                then
                                    sleep 2
                                    ping -c 5 $mygwdefault
                                    if [ ! $? = 0 ]
                                    then
                                        exit 4
                                    fi
                                fi
                    ;;
                    check_ns)
                                #check nameserver
                                        echo " network check nameserver"
                                        mydns=`cat /etc/resolv.conf|grep nameserver|head -1|awk '{ print $2}'`
                                        ping -c 1 $mydns
                                        if [ ! $? = 0 ]
                                        then
                                            exit 5
                                        fi
                    ;;
                    check_names)
                                echo "network check resolve names"
                                host google.com
                                if [ ! $? = 0 ]
                                then
                                    exit 6
                                fi
                    ;;
                    host_names)
                                #if host is found, then check to see if it's online
                                #if hoip matchs my ip assume it's ok
                                echo "network check my hostname $hostname"
                                hout=`host $hostname|head -n1`
                                status=$?
                                hoip=`echo $hout|awk  '{print $4}'`
                                if [ $hoip = found: ]
                                then
                                    status=1
                                fi

                                if [ $hoip = "out;" ]
                                then
                                    #;; connection timed out; no servers could be reached
                                    echo "DNS servers not found"
                                    status=1
                                fi

                                unkownhost=`host unkown.linhes.org |head -n1 `
                                unkownhostip=`echo $unkownhost |awk  '{print $4}'`

                                #found host, now grab the ip
                                if [ $status = 0 ]
                                then
                                    hoip=`echo $hout|awk  '{print $4}'`
                                    /sbin/ifconfig -a |grep -q $hoip
                                    if [  $? = 0 ]
                                    then
                                        echo "DNS ip matches on of my ips $hoip"
                                        status=0
                                    else
                                        if [ $unkownhostip = $hoip ]
                                        then
                                            echo "Bad DNS returning false hits"
                                            echo "Assuming hostname is OK"
                                            echo "DNS unknown: $unkownhostip"
                                            echo "    HOSTDNS: $hoip       "
                                            /sbin/ifconfig -a |grep "inet addr"
                                        else
                                            echo "IP in DNS but doesn't match mine, hostname is not safe to use"
                                            echo "DNS:$hoip"
                                            echo "HOST: "
                                            /sbin/ifconfig -a |grep "inet addr"
                                            status=7
                                            fi
                                    fi
                                else
                                    echo "Couldn't find hostname in DNS, must be safe to use"
                                    status=0
                                fi
                                exit $status
                    ;;

                esac
            exit 0
        ;;

    * )
        if [ -f /tmp/.install_state ]
        then
                state=`cat /tmp/.install_state`
        else
        state=" "
        fi
            if  [  x"${state}" =  "xConfiguring system" ]
            then
                progress ""
            else

                if [ -f /tmp/.startsize.io ]
                then
                    . /tmp/.startsize.io
                    #finding the drive
                    ddrive=`df |  grep $mountpoint | head -n1 | awk -F/ ' { print $3 } ' | cut -d" " -f 1|cut -b1,2,3 `
                    used=`statgrab -M disk.|grep ${ddrive}.write_bytes|cut -d= -f 2|tr -d " "`
                    used=`echo $used-$STARTSIZE|bc`
                        #echo $used
                fi

        if [ ! x$used = x ]
            then
                .  /root/.install_size
                #somehow find if it's an install or update
                if [ -f /tmp/.this_is_upgrade ]
                then
                    total=$UPGRADESIZE
                else
                    total=$TOTALSIZE
                fi
            percent=`echo "scale=2 ;  ($used/$total) * 100 " |bc `
            cond2=$(echo "$percent > 98" |bc  )
            if [ $cond2 -eq 1  ]
            then
                percent=98
            fi
            if  [  x"${state}" =  "xDone" ]
            then
                percent=100
            fi
            cond3=$(echo "$percent < 3" |bc )
            if [ $cond3 -eq 0   ]
                then
                    progress $percent
                fi
            fi
        fi
    ;;
esac