#!/bin/bash

LOGFILE=$1

function choice {

        CHOICE=''
        local prompt="$*"
        local answer

        read -p "$prompt" answer
        case "$answer" in
                [yY1] ) CHOICE='y';;
                [nN0] ) CHOICE='n';;
                *     ) CHOICE="$answer";;
        esac
}

function wait_ten {
	echo -n "10 "
	sleep 1
        echo -n "9 "
        sleep 1
        echo -n "8 "
        sleep 1
        echo -n "7 "
        sleep 1
        echo -n "6 "
        sleep 1
        echo -n "5 "
        sleep 1
        echo -n "4 "
        sleep 1
        echo -n "3 "
        sleep 1
        echo -n "2 "
        sleep 1
        echo -n "1 "
        sleep 1
	echo ""
}


echo "================================================================="
echo 
echo "ViciDial Installer Script for Ubuntu Server 8.04.2"
echo 
echo "PHASE 1 - Updates"
echo
echo "================================================================="
wait_ten

echo

# Lets go to a safe-place to start with
cd /tmp 2>> $LOGFILE >> $LOGFILE

echo "Synchronizing with Ubuntu repository"
echo "Synchronizing with Ubuntu repository" 2>> $LOGFILE >> $LOGFILE

/tmp/spinner & # Spin cursor spin
SPINPID=`ps -e | grep spinner| sed 's/^ *//g' | cut --delimiter=" " -f1`

sleep 1
apt-get update 2>> $LOGFILE >> $LOGFILE

disown $SPINPID
kill $SPINPID 2> /dev/null > /dev/null # spin no more

echo

echo "Installing base OS updates"
echo "Installing base OS updates" 2>> $LOGFILE >> $LOGFILE

/tmp/spinner & # Spin cursor spin
SPINPID=`ps -e | grep spinner| sed 's/^ *//g' | cut --delimiter=" " -f1`

apt-get -y --force-yes upgrade 2>> $LOGFILE >> $LOGFILE

disown $SPINPID
kill $SPINPID 2> /dev/null > /dev/null # spin no more

echo

echo "Install latest kernel"
echo "Install latest kernel" 2>> $LOGFILE >> $LOGFILE

/tmp/spinner & # Spin cursor spin
SPINPID=`ps -e | grep spinner| sed 's/^ *//g' | cut --delimiter=" " -f1`

apt-get -y --force-yes install linux-server 2>> $LOGFILE >> $LOGFILE

disown $SPINPID
kill $SPINPID 2> /dev/null > /dev/null # spin no more

echo
echo "=========================================================================="
echo
echo "Phase 1 is complete. The operating system is up to date."
echo
echo "Your computer will reboot in ten seconds so it is using the latest kernel"
echo
echo "=========================================================================="
wait_ten
