#!/bin/sh
CONF_SWVERSION=C-0-1-2-0_AC-1.5
EXITVAL_OK=0
MIN_REMAINING_SIZE=20048
EXITVAL_ERR=1

UPDATE_PATH=/evbb/update
CFG_PATH=/evbb/cfg
UPDATE_FILES_PATH=$UPDATE_PATH/tmp
IPK_PATH=/evbb/ipk/
OLD_UPDATE_PATH=/home/root/update
INSTALL_RES="None"
UPDATELOGFILE=$UPDATE_PATH/install_last_log
RESULTDOC=$UPDATE_PATH/install_result
OLD_RESULTDOC=$OLD_UPDATE_PATH/install_result

LIST_INSTALLED_IPKS_FILE=$UPDATE_PATH/list_new_installed_ipks
LIST_REMOVED_IPKS_FILE=$UPDATE_PATH/list_removed_ipks
LIST_UPDATED_IPKS_FILE=$UPDATE_PATH/list_updated_ipks
CLEANING_IN_PROGRESS_FILE=$UPDATE_PATH/cleaning_in_progress

PACKAGES_PATH=$UPDATE_FILES_PATH/packages
PHASE1_PATH=$PACKAGES_PATH/phase1
PHASE2_PATH=$PACKAGES_PATH/phase2
ADDITIONS_PATH="additions"
UPDATES_PATH="updates"
REMOVALS_PATH="removals"
REMOVALS_FILE="list.txt"


FAIL=failed
SUCCESS=succeed

# Define function that checks remaining disk space
# REMARK: MIN_REMAINING_SIZE variable in function below is added to this script when the firmware update is created (check timpackupdate for more details).
# By security, if it is not defined, we consider that 60Mo is required to perform the update.
check_remaining_disk_space()
{
    local __entete="CheckRemainingDiskSize"
    local l_remaining_size
    l_remaining_size=$(df / | awk '/[0-9]%/{print $(NF-2)}')

    # Check is MIN_REMAINING_SIZE is defined, else we consider 60Mo is required...
    if [ -z "$MIN_REMAINING_SIZE" ]
    then
        lastlogadd "[ ] $__entete: Default size of update package (60Mo) is used as no value was provided in the package !"
        MIN_REMAINING_SIZE=60000
    else
        lastlogadd "[ ] $__entete: Size of update package : '$MIN_REMAINING_SIZE' !"
    fi

    if [ $l_remaining_size -gt $MIN_REMAINING_SIZE ]
    then
        lastlogadd "[ ] $__entete: Disk space available : '$l_remaining_size' - Installation will continue"
    else
        lastlogadd "[ ] $__entete: Not enough disk space available : '$l_remaining_size' - Installation stopped"
        exit 1
    fi
}

datestr()
{
    echo "[$(date +%Y/%m/%d\ %H:%M:%S)] [$(basename "$0")] $1"
}

dmesgstr()
{
    datestr "$1" | tee /dev/kmsg
}

lastlogadd()
{
    TMPSTR=$(datestr "$1")
    echo "$TMPSTR"
    echo "$TMPSTR" >> $UPDATELOGFILE
    sync
}

psplash_write_text()
{
    local l_MSG=$1
    if [ -z "$l_MSG" ]
    then
        psplash-write "CLEAR"
    else
        psplash-write "MSG $l_MSG"
    fi
}

start_psplash()
{
    . /etc/init.d/functions
    . /etc/default/psplash

    ROTATION_FILE=/home/root/screen_orientation.txt
    if [ ! -f $ROTATION_FILE ] ; then
        echo "90" > $ROTATION_FILE
    fi

    ROTATION_ANGLE=$(<$ROTATION_FILE)
    REAL_ANGLE="270"

    case "$ROTATION_ANGLE" in
        0 )
            REAL_ANGLE="0"
            ;;
        90 )
            REAL_ANGLE="270"
            ;;
        180 )
            REAL_ANGLE="180"
            ;;
        270 )
            REAL_ANGLE="90"
            ;;
        * )
            REAL_ANGLE="270"
            ;;
    esac

    case `machine_id` in
        "atmel_at91sam9x5-ek")
        PARAMS="-a $REAL_ANGLE";;
    esac
    export TMPDIR=/mnt/.splash
    if [ ! -d /mnt/.splash ]
    then
        mount tmpfs -t tmpfs $TMPDIR -o,size=40k
    fi
    if [ -f /usr/bin/psplash ]
    then
        /usr/bin/psplash -c $PARAMS &
        #TODO Check PID instead of sleep
        sleep 2
        psplash_write_text "Installation in progress"
    else
        lastlogadd "[X] start_psplash: psplash not found!"
    fi
}

psplash_set_progressbar()
{
    local l_VAL=$1
    if [ -z $l_VAL ]
    then
        psplash-write "CLEAR"
    else
        psplash-write "PROGRESS $l_VAL"
    fi
}

stop_watchdog()
{
    local __entete="Watchdog"
    echo 'V' > /dev/watchdog
    while [ $? != 0 ]
    do
        sleep 1
        echo 'V' > /dev/watchdog
    done
    #echo 'a' > /dev/watchdog
    lastlogadd "[ ] $__entete: Watchdog stopped"
}

remove_ipks() {
    local __entete="PackagesRemoval"
    local l_IPKS_TO_REMOVE=("$@")

    local l_FILE
    local l_PACKAGE_NAME=""

    for l_FILE in "${l_IPKS_TO_REMOVE[@]}"
    do
        # All IPK file names should have a "_" field between package name and release, even public ones
        l_PACKAGE_NAME="$(echo "$l_FILE" | sed 's/.*\///' | sed 's/_.*//')"
        opkg remove $l_PACKAGE_NAME
        lastlogadd "[ ] $__entete: Package $l_PACKAGE_NAME removed"
    done
}

ReinstallIpks()
{
    local __entete="ReinstallIpks"
    local l_FILES_TO_INSTALL=("$@")
    for l_FILE in "${l_FILES_TO_INSTALL[@]}"
    do
        l_PACKAGE_NAME="$(echo "$l_FILE" | sed 's/.*\///' | sed 's/_.*//')"
        opkg install $IPK_PATH/$l_PACKAGE_NAME* --nodeps --force-depends --force-overwrite
        RES=$?
        if [ $RES -ne 0 ]; then
            lastlogadd "[x] $__entete: Package $l_PACKAGE_NAME from file '$l_FILE' failed to install - opkg install returned $RES"
            INSTALL_RES="FAILED"
            break
        else
            lastlogadd "[ ] $__entete: Package $l_PACKAGE_NAME installed"
        fi
    done
}

Restore()
{
    local __entete="Restore"
    local l_NEW_IPKS=""
    local l_UPDATED_IPKS=""
    local l_REMOVED_IPKS=""

    lastlogadd "[ ] $__entete: Start"
    if [ -f $LIST_INSTALLED_IPKS_FILE ]
    then
        l_NEW_IPKS=$(<$LIST_INSTALLED_IPKS_FILE)
    fi
    if [ ! -z "$l_NEW_IPKS" ]
    then
        lastlogadd "[ ] $__entete: New IPKs found : will be removed"
        remove_ipks $l_NEW_IPKS
    fi

    if [ -f $LIST_UPDATED_IPKS_FILE ] 
    then
        l_UPDATED_IPKS=$(<$LIST_UPDATED_IPKS_FILE)
    fi
    if [ ! -z "$l_UPDATED_IPKS" ]
    then
        lastlogadd "[ ] $__entete: Updated IPKs found : will be rollbacked to previous release"
        remove_ipks $l_UPDATED_IPKS
        ReinstallIpks $l_UPDATED_IPKS
    fi

    if [ -f $LIST_REMOVED_IPKS_FILE ]
    then
        l_REMOVED_IPKS=$(<$LIST_REMOVED_IPKS_FILE)
    fi
    if [ ! -z "$l_REMOVED_IPKS" ]
    then
        lastlogadd "[ ] $__entete: Removed IPKs will be reinstalled"
        ReinstallIpks $l_REMOVED_IPKS
    fi
    lastlogadd "[ ] $__entete: End"
}

l_current_number=0

UpdateIpks() {
    INSTALL_RES="INPROGRESS"
    local __entete="UpdateIpks"
    lastlogadd "[ ] $__entete: Start"

    # Install using ipk packages
    local l_FILE

    local l_REMOVED_IPKS=""
    if [ -f $LIST_REMOVED_IPKS_FILE ] 
    then
        l_REMOVED_IPKS=$(<$LIST_REMOVED_IPKS_FILE)
        l_REMOVED_IPKS+=" "
    fi

    local l_UPDATED_IPKS=""
    if [ -f $LIST_UPDATED_IPKS_FILE ] 
    then
        l_UPDATED_IPKS=$(<$LIST_UPDATED_IPKS_FILE)
        l_UPDATED_IPKS+=" "
    fi

    local l_INSTALLED_IPKS=""
    if [ -f $LIST_INSTALLED_IPKS_FILE ] 
    then
        l_INSTALLED_IPKS=$(<$LIST_INSTALLED_IPKS_FILE)
        l_INSTALLED_IPKS+=" "
    fi
    local l_INSTALL_PATH=$1
    local l_FILES_TO_UPDATE=$l_INSTALL_PATH/$UPDATES_PATH/*
    local l_FILES_TO_INSTALL=$l_INSTALL_PATH/$ADDITIONS_PATH/*
    local l_REMOVALS_FILE=$l_INSTALL_PATH/$REMOVALS_PATH/$REMOVALS_FILE

    local l_IPKS_TO_REMOVE=""
    if [ -f $l_REMOVALS_FILE ]
    then
        l_IPKS_TO_REMOVE=$(<$l_REMOVALS_FILE)
    fi

    local l_nb_ipks_to_remove=$(cat $PACKAGES_PATH/*/*/$REMOVALS_FILE | wc -w)
    local l_nb_ipks_to_install=$(ls -la $PACKAGES_PATH/*/*/*.ipk | wc -l)
    #l_nb_ipks_to_install = $l_nb_ipks_to_install + nombre d'IPK à supprimer
    l_nb_ipk_operations=$(($l_nb_ipks_to_install + $l_nb_ipks_to_remove))
    lastlogadd "[ ] $__entete: Operations to be done : $l_nb_ipks_to_remove and $l_nb_ipks_to_install - total : $l_nb_ipk_operations"
    local l_current_percent=0

    if [ ! -z "$l_IPKS_TO_REMOVE" ]
    then
        for l_FILE in $l_IPKS_TO_REMOVE
        do
            # All IPK file names should have a "_" field between package name and release, even public ones
            l_PACKAGE_NAME="$(echo "$l_FILE" | sed 's/.*\///' | sed 's/_.*//')"
            l_current_number=$(( $l_current_number + 1 ))
            l_current_percent=$(($(($l_current_number*100))/$l_nb_ipk_operations))
            opkg remove $l_PACKAGE_NAME
            RES=$?
            if [ $RES -ne 0 ]; then
                lastlogadd "[x] $__entete: Unable to remove package '$l_PACKAGE_NAME' - opkg remove returned $RET"
                INSTALL_RES="FAILED"
                break
            else
                psplash_set_progressbar $l_current_percent
                lastlogadd "[ ] $__entete: Package $l_PACKAGE_NAME removed"
                l_REMOVED_IPKS+=$l_FILE
                l_REMOVED_IPKS+=" "
                echo $l_REMOVED_IPKS > $LIST_REMOVED_IPKS_FILE
                sync
            fi
        done
    fi
    if [ $INSTALL_RES != "FAILED" ]
    then
        for l_FILE in ${l_FILES_TO_UPDATE}
        do
            l_FILE_EXTENSION="$(echo "$l_FILE" | sed 's/.*\.//')"
            if [ -f "$l_FILE" ] && [ $l_FILE_EXTENSION = "ipk" ]
            then
                l_current_number=$(( $l_current_number + 1 ))
                l_current_percent=$(($(($l_current_number*100))/$l_nb_ipk_operations))
                lastlogadd "[ ] $__entete: Will remove and reinstall package '$l_FILE'"
                l_PACKAGE_NAME="$(echo "$l_FILE" | sed 's/.*\///' | sed 's/_.*//')"
                opkg remove $l_PACKAGE_NAME
                RES=$?
                if [ $RES -ne 0 ]; then
                    lastlogadd "[x] $__entete: Unable to remove package '$l_PACKAGE_NAME' - opkg remove returned $RET"
                    INSTALL_RES="FAILED"
                    break
                else
                    lastlogadd "[ ] $__entete: Package $l_PACKAGE_NAME removed"
                    l_UPDATED_IPKS+=$l_FILE
                    l_UPDATED_IPKS+=" "
                    echo $l_UPDATED_IPKS > $LIST_UPDATED_IPKS_FILE
                    sync
                fi
                # To be sure not installing unvolunteer dependencies, deactivate their installation and continue the current installation.
                opkg install $l_FILE --nodeps --force-depends --force-overwrite
                RES=$?
                if [ $RES -ne 0 ]; then
                    lastlogadd "[x] $__entete: Package '$l_FILE' failed to install - opkg install returned $RET"
                    INSTALL_RES="FAILED"
                    break
                else
                    psplash_set_progressbar $l_current_percent
                    lastlogadd "[ ] $__entete: Package $l_FILE installed"
                fi
            fi
        done
    fi
    if [ $INSTALL_RES != "FAILED" ]
    then
        for l_FILE in ${l_FILES_TO_INSTALL}
        do
            l_FILE_EXTENSION="$(echo "$l_FILE" | sed 's/.*\.//')"
            if [ -f "$l_FILE" ] && [ $l_FILE_EXTENSION = "ipk" ]
            then
                l_current_number=$(( $l_current_number + 1 ))
                l_current_percent=$(($(($l_current_number*100))/$l_nb_ipk_operations))
                # To be sure not installing unvolunteer dependencies, deactivate their installation and continue the current installation.
                opkg install $l_FILE --nodeps --force-depends --force-overwrite
                RES=$?
                if [ $RES -ne 0 ]; then
                    lastlogadd "[x] $__entete: Package '$l_FILE' failed to install - opkg install returned $RET"
                    INSTALL_RES="FAILED"
                    break
                else
                    psplash_set_progressbar $l_current_percent
                    lastlogadd "[ ] $__entete: Package $l_FILE installed"
                    l_INSTALLED_IPKS+=$l_FILE
                    l_INSTALLED_IPKS+=" "
                    echo $l_INSTALLED_IPKS > $LIST_INSTALLED_IPKS_FILE
                    sync
                fi
            fi
        done
    fi

    if [ $INSTALL_RES = "FAILED" ]
    then
        echo $FAIL > $RESULTDOC
        if [ -d $OLD_UPDATE_PATH ]
        then
            echo $FAIL > $OLD_RESULTDOC
        fi
        lastlogadd "[ ] $__entete: Installation failed - will revert already installed packages"
        psplash_write_text "Installation failed"
        Restore
    fi
    lastlogadd "[ ] $__entete: End"
}

ManageIpkBackup()
{
    local __entete="ManageIpkBackup"
    local l_PACKAGE_NAME
    local l_REMOVED_IPKS=""
    local l_UPDATED_IPKS=""
    local l_FILE
    local l_PHASE1_FILES=$PHASE1_PATH/*/*
    local l_PHASE2_FILES=$PHASE2_PATH/*/*

    # Update now stored ipk in the target
    if [ -f $LIST_REMOVED_IPKS_FILE ]
    then
        lastlogadd "[ ] $__entete: Found a list of IPKs to remove"
        l_REMOVED_IPKS=$(<$LIST_REMOVED_IPKS_FILE)
        for l_FILE in $l_REMOVED_IPKS
        do
            l_PACKAGE_NAME="$(echo "$l_FILE" | sed 's/.*\///' | sed 's/_.*//')"
            lastlogadd "[ ] $__entete: Removing file '$l_PACKAGE_NAME'"
            rm -fr $IPK_PATH/$l_PACKAGE_NAME*
        done
    fi
    if [ -f $LIST_UPDATED_IPKS_FILE ]
    then
        lastlogadd "[ ] $__entete: Found a list of IPKs updated"
        l_UPDATED_IPKS=$(<$LIST_UPDATED_IPKS_FILE)
        for l_FILE in $l_UPDATED_IPKS
        do
            l_PACKAGE_NAME="$(echo "$l_FILE" | sed 's/.*\///' | sed 's/_.*//')"
            lastlogadd "[ ] $__entete: Removing file '$l_PACKAGE_NAME'"
            rm -fr $IPK_PATH/$l_PACKAGE_NAME*
        done
    fi
    for l_FILE in ${l_PHASE1_FILES}
    do
        l_FILE_EXTENSION="$(echo "$l_FILE" | sed 's/.*\.//')"
        if [ -f "$l_FILE" ] && [ $l_FILE_EXTENSION = "ipk" ]
        then
            lastlogadd "[ ] $__entete: Store IPK '$l_FILE' into $IPK_PATH folder"
            mv -f $l_FILE $IPK_PATH
        fi
    done
    # Update now stored ipk in the target
    for l_FILE in ${l_PHASE2_FILES}
    do
        l_FILE_EXTENSION="$(echo "$l_FILE" | sed 's/.*\.//')"
        if [ -f "$l_FILE" ] && [ $l_FILE_EXTENSION = "ipk" ]
        then
            lastlogadd "[ ] $__entete: Store IPK '$l_FILE' into $IPK_PATH folder"
            mv -f $l_FILE $IPK_PATH
        fi
    done
    sync
}

StartInstall()
{
    local __entete="StartInstall"
    lastlogadd "[ ] $__entete: Start"

    UpdateIpks $PHASE1_PATH
    RET=$(<$RESULTDOC)
    if [ $RET != "FAILED" ]
    then
        lastlogadd "[ ] $__entete: Step 1 ok, starting step 2"
        UpdateIpks $PHASE2_PATH
    fi
    RET=$(<$RESULTDOC)
    if [ $RET != "FAILED" ]
    then
        lastlogadd "[ ] $__entete: Step 2 OK, cleaning files"
        echo $SUCCESS > $RESULTDOC
        echo "true" > $CLEANING_IN_PROGRESS_FILE
        sync
        INSTALL_RES="SUCCESS"

        ManageIpkBackup

        psplash_write_text "Installation finished"
        lastlogadd "[ ] $__entete: Installation Succeeded!"
        psplash-write "PROGRESS 100"
        sleep 1
        killall psplash
    fi
    lastlogadd "[ ] $__entete: End"
}

cleansaves()
{
    local __entete="Suppression des sauvegardes"
    lastlogadd "/// $__entete"

    if [ -d $OLD_UPDATE_PATH ] && [ $INSTALL_RES = "SUCCESS" ]
    then
        lastlogadd "/// $__entete: found an old update folder: moving it to new update folder/old_logs"
        mkdir -p $UPDATE_PATH/old_logs
        mv -f $OLD_UPDATE_PATH $UPDATE_PATH/old_logs
    fi
    lastlogadd "/// $__entete: terminée"
}


lastlogclean()
{
    if test ! -f $UPDATELOGFILE
    then
        dmesgstr "[!] Fichier manquant (fichier $UPDATELOGFILE)"
    fi

    echo "" > $UPDATELOGFILE
    R=$?
    if [ $R -ne 0 ]; then
        dmesgstr "[x] Raz du fichier $UPDATELOGFILE ($R)"

        exit $EXITVAL_ERR
    else
        datestr "[ ] Raz du fichier $UPDATELOGFILE"
    fi

    sync
}

restartgui()
{
    local __entete="Relance de l'IHM"
    lastlogadd "/// $__entete"

    pidSTARTINTERFACE=$(pidof -x startInterfaceLog.sh)
    pidIHM=$(pidof Success-v2-2-Unity-a2)
    if [ "$pidIHM" == "" ];then
        lastlogadd "[!] $__entete: pid non trouvé"
    else
        lastlogadd "[ ] $__entete: pid($pidIHM)"
        kill -9 $pidIHM $pidSTARTINTERFACE
        ICI=$(pwd)
        cd /home/root/
        ./watchdog-magicClose &
        cd $ICI
    fi
    lastlogadd "/// $__entete: terminée"
}

SaveConfigs()
{
    local __entete="SaveConfigs"
    lastlogadd "[ ] $__entete: Saving current configs to /evbb/cfg..."
    if [ ! -d $CFG_PATH ]
    then
        mkdir -p $CFG_PATH
    fi
    if [ -f /home/root/logo.png ]
    then
        cp /home/root/logo.png $CFG_PATH/
    fi
    cp /home/root/setup.xml $CFG_PATH/
    cp /home/root/screen_orientation.txt $CFG_PATH/
    cp /home/root/grid.xml $CFG_PATH/
    cp /home/root/BornePrincipale.txt $CFG_PATH/
    cp /home/root/connectivite.xml $CFG_PATH/
    cp /etc/inittab $CFG_PATH/
    lastlogadd "[ ] $__entete: Done."
}

RestoreConfigs()
{
    local __entete="RestoreConfigs"
    lastlogadd "[ ] $__entete: Restoring configs from /evbb/cfg..."

    if [ -f $CFG_PATH/logo.png ]
    then
        mv $CFG_PATH/logo.png /home/root/
    fi

    if [ -f $CFG_PATH/setup.xml ]
    then
        mv $CFG_PATH/setup.xml /home/root/
    fi

    if [ -f $CFG_PATH/screen_orientation.txt ]
    then
        mv $CFG_PATH/screen_orientation.txt /home/root/
    fi

    if [ -f $CFG_PATH/grid.xml ]
    then
        mv $CFG_PATH/grid.xml /home/root/
    fi

    if [ -f $CFG_PATH/BornePrincipale.txt ]
    then
        mv $CFG_PATH/BornePrincipale.txt /home/root/
    fi

    if [ -f $CFG_PATH/connectivite.xml ]
    then
        mv $CFG_PATH/connectivite.xml /home/root/
    fi

    if [ -f $CFG_PATH/inittab ]
    then
        mv $CFG_PATH/inittab /etc/
    fi

    lastlogadd "[ ] $__entete: Done."
}

customize()
{
    lastlogadd "/// Finalisation de la mise à jour"
    # ------------------------------------------------------------
    # Fill below specific commands to execute for this FW update -
    # ------------------------------------------------------------

    # 0) Update SW Version if provided
    if [ ! -z "$CONF_SWVERSION" ]
    then
        LINE_NBER=$(awk '/firmwareVersion/{print NR}' $CFG_PATH/BornePrincipale.txt)
        if [ ! -z "$LINE_NBER" ]
        then
            ((LINE_NBER++))
            STR1=".*"
            STR2="$CONF_SWVERSION"
            PATTERN="${LINE_NBER}s|${STR1}|${STR2}|"
            sed -e "$PATTERN" $CFG_PATH/BornePrincipale.txt > $CFG_PATH/BornePrincipale2.txt
            if [ $? -eq 0 ]
            then
                mv $CFG_PATH/BornePrincipale2.txt $CFG_PATH/BornePrincipale.txt
            fi
        fi
    fi

    # 1) handle grid.xml file
    # We want to recover previous max_current value from grid.xml
    read MAX_CURRENT <<< `echo "cat /grid/max_current/text()" | xmllint --nocdata --shell $CFG_PATH/grid.xml | sed '1d;3d;$d'`

    LINE_NBER=$(awk '/<grid>/{print NR}' /home/root/grid.xml)
    if [ ! -z "$LINE_NBER" ]
    then
        ((LINE_NBER++))
        STR1="<max_current>.*</max_current>"
        STR2="<max_current>$MAX_CURRENT</max_current>"
        PATTERN="${LINE_NBER}s|${STR1}|${STR2}|"
        sed -e "$PATTERN" /home/root/grid.xml > /home/root/grid2.xml
        if [ $? -eq 0 ]
        then
            mv /home/root/grid2.xml /home/root/grid.xml
            # Remove obsolete grid.xml file
            rm $CFG_PATH/grid.xml
        fi
    fi

    # 2) handle inittab file
    LINE_NBER=$(awk '/logRotate/{print NR}' $CFG_PATH/inittab)
    if [ ! -z "$LINE_NBER" ]
    then
        STR1=".*"
        STR2="log:5:respawn:/home/root/dlt_archiveLogs.sh"
        PATTERN="${LINE_NBER}s|${STR1}|${STR2}|"
        sed -e "$PATTERN" $CFG_PATH/inittab > $CFG_PATH/inittab2
        if [ $? -eq 0 ]
        then
            mv $CFG_PATH/inittab2 $CFG_PATH/inittab
        fi
    fi

    # 3) handle dlt log files
    # Move current dlt logs into archive folder
    mkdir -p /evbb/opt/dlt/archive
    mv /evbb/opt/dlt/*.dlt /evbb/opt/dlt/archive
    mv /evbb/opt/dlt/*.zip /evbb/opt/dlt/archive
    # Remove previous logs
    rm -rf /home/root/logs/

    # ------------------------------------------------------------
    lastlogadd "/// Finalisation de la mise à jour: terminée"
}

restartHttpd()
{
    local __entete="Relance du server HTTP"
    lastlogadd "/// $__entete"

    # We have to restart the httpd service after updating the web site
    killall httpd

    lastlogadd "/// $__entete: terminée"
}



#-----------------
#    START
#-----------------
if [ "$#" -eq 1 ] && [ "$1" = "--no-exec" ]
then
    echo "Script not executed : will exit"
else
# First try to remove legacy 'gestionnaire_borne' program in order to save space
rm -f /home/root/gestionnaire_borne
# then start the update:
check_remaining_disk_space
stop_watchdog
start_psplash

# Reset log file for update (/home/root/update/install_last_log)
lastlogclean

lastlogadd " >  Mise à jour: début"

if [ -f $CLEANING_IN_PROGRESS_FILE ]
then
    rm -fr $CLEANING_IN_PROGRESS_FILE
fi

# The following part is associated to local VPN management. 
# We keep it here for the moment to be sure that such logs are cleaned.
#--------------------------------------------------------------------------------------------
IPSEC_LOGS=/var/volatile/log/pluto
if test -d $IPSEC_LOGS
then
    lastlogadd "[ ] ipsec - dossier temporaire $IPSEC_LOGS: trouvé"
    lastlogadd "[ ] ipsec - dossier temporaire $IPSEC_LOGS: suppression"
    rm -r $IPSEC_LOGS
    R=$?
    if [ $R -eq 0 ]; then
        lastlogadd "[ ] ipsec - dossier temporaire $IPSEC_LOGS: nettoyé"
        sync
    else
        lastlogadd "[x] ipsec - dossier temporaire $IPSEC_LOGS: erreur sur suppression ($R)"
    fi
else
    lastlogadd "[ ] ipsec - dossier temporaire $IPSEC_LOGS: absent, ok"
fi
#--------------------------------------------------------------------------------------------


# Copy current configuration files and folders not managed by Get/SetConfig yet
SaveConfigs

# Insert new files and folders from the update package to the system
StartInstall

# Make the custom action of the update
if [ $INSTALL_RES = "SUCCESS" ]
then
    customize
fi

RestoreConfigs

# Delete old files and folders
cleansaves

#-----------------------------------------
lastlogadd " >  Mise à jour: fin"
# Restart syslogd service due to potential new configuration for DLT
/etc/init.d/syslog restart

telinit Q
#-----------------------------------------

exit $EXITVAL_OK
fi

