#!/bin/bash

featureversion=$(featureversion)

if [ ! -z "${featureversion}" ]; then
    if [ -e /tmp/gateconfig.featureversion ]; then
        gateconfigversion=$(cat /tmp/gateconfig.featureversion)
        if [ -z "${gateconfigversion}" -o ${gateconfigversion} -lt ${featureversion} ]; then
            echo "***********************************************************************************"
            echo "* OfficeMaster GateConfiguration too old. Please download new version"
            echo "* http://www.ferrari-electronic.de/fileadmin/download_center/OfficeMasterHardware/Setup-OfficeMaster-Gate-Config.exe"
            echo "***********************************************************************************"
            read -p "^GOfficeMaster GateConfiguration too old. Please download new version. [Abort/Default=Abort]!" answer
            sleep 5
            echo "## Installation failed ## ..."
            echo "FLASH_FAILED" # Old gateConfig
            touch /tmp/flashfailed
            exit 1
        fi
    else
        if [ -e /var/config.active -o "${HISTFILE}" != "" ]; then
            echo "***********************************************************************************"
            echo "* OfficeMaster GateConfiguration too old. Please download new version"
            echo "* http://www.ferrari-electronic.de/fileadmin/download_center/OfficeMasterHardware/Setup-OfficeMaster-Gate-Config.exe"
            echo "***********************************************************************************"
            read -p "^GOfficeMaster GateConfiguration too old. Please download new version. [Abort/Default=Abort]!" answer
            sleep 5
            echo "## Installation failed ## ..."
            echo "FLASH_FAILED" # Old gateConfig
            touch /tmp/flashfailed
            exit 2
        fi
    fi
fi

if [ ! -e /sbin/restore.packages ]; then
    if [ -e /usr/bin/restore.packages ]; then
        cp /usr/bin/restore.packages /sbin
    fi
fi

# Update fe packages

if [ "x${OEM}" = "x1" ]; then
    PACKAGES="omg-utilities omg-modules"
else
    PACKAGES="omg-utilities omg-modules omg-userfs"
fi

. /tmp/cmdline

if [ ! -z "${HTTP_PROXY}" ]; then
    export http_proxy=${HTTP_PROXY}
fi

let FULL=0
let REINSTALL=0
let REBOOT=0

while getopts frhb opt;  do
    case "$opt" in
        
	f)  let FULL=1
	    ;;
        
	r)  let REINSTALL=1
	    ;;

    b)  let REBOOT=1
        ;;

	h)  echo "Usage: $0 [-f]"
	    echo "       update OfficeMasterGate"
	    echo "    -f full update"
	    echo "    -r reinstall"
	    echo "    -h gives this message"
	    exit 1;;
        
	*)
	    echo "See $0 -h for usage"
	    exit 1;;
    esac
done

# Sometimes it takes a long time ...
if [ -x /usr/bin/wdstop ] ; then
    /usr/bin/wdstop
fi

# Remove leftovers
fuser -k /var/lib/dpkg/lock;rm -f /var/lib/dpkg/lock

omgstop

apt-get clean

if ! $(file /bin/bunzip2|grep 'executable,' > /dev/null 2>&1) ; then
    # Kein programm
    echo "bunzip2 corrupt, trying to reinstall"
    apt-get install --reinstall bzip2
fi


if [ ${FULL} -eq 1 ] ; then
    if [ ${REINSTALL} -eq 1 ] ; then
        apt-get update
        apt-get -y install --reinstall ${PACKAGES}
        rc=$?
    else
        apt-get -y update
        apt-get -y upgrade
        rc=$?
    fi    
else
    if [ ${REINSTALL} -eq 1 ] ; then
        apt-get -o Dir::Etc::sourcelist=/etc/apt/fe-only.d.list update
        apt-get -y -o Dir::Etc::sourcelist=/etc/apt/fe-only.d.list install --reinstall ${PACKAGES}
    else
        apt-get -o Dir::Etc::sourcelist=/etc/apt/fe-only.d.list update
        apt-get -y -o Dir::Etc::sourcelist=/etc/apt/fe-only.d.list install ${PACKAGES}
    fi
fi

if [ ${REBOOT} -eq 1 ] ; then
    reboot
fi

if [ ! -e /tmp/powercyle ]; then
    /usr/bin/omgstopilid
    /usr/bin/omgstartilid
    /etc/init.d/omg.setup
    /usr/bin/omgrestart
fi
sleep 30


