#!/bin/sh
#----------------------------------------------------------------------------
# control_recover.cgi
#
# Creation:	2007-02-24 lanspezi
# Last Update:  $Id$
#----------------------------------------------------------------------------
format_output()
{
    htmlspecialchars | while read line
    do
        dat=`echo $line | cut -d "|" -f 1`
        event=`echo $line | cut -d "|" -f 2`
        echo "
"
        echo "| $dat | "
        echo "$event | "
        echo "
"
    done
}
get_one_var()
{
    var=$1
    name=$2
    file=$3
    eval $var=`sed -n -e "s/^$name='\(.*\)'/\1/p" $file`
}
show_version_info ()
{
    get_one_var var_version FLI4L_VERSION   $2
    get_one_var var_date    FLI4L_BUILDDATE $2
    get_one_var var_time    FLI4L_BUILDTIME $2
    get_one_var var_kernel  KERNEL_VERSION $2
    get_one_var var_arch    ARCH $2
    
    var_typ=" "
    if [ "$2" = "/etc/rc.cfg" ]
    then
        var_typ="not defined"
        if grep -q "fli4l_mode=normal" /proc/cmdline
        then
            var_typ="$_RECOVER_normal_version"
        fi
        if grep -q "fli4l_mode=recover" /proc/cmdline
        then
            var_typ="$_RECOVER_recover_version"
        fi
        echo ""
    else
        echo "
"
    fi
    if [ "x$var_arch" = "x" ]
    then
        var_arch="N.A."
    fi
    
    echo "| $1 | "
    echo "$var_version | "
    echo "$var_kernel | "
    echo "$var_date | "
    echo "$var_time | "
    echo "$var_arch | "
    echo "$var_typ | "
    echo "
"
}
show_versions ()
{
    show_tab_header "$_RECOVER_version_info" no
    cat <
 | $_RECOVER_version | Kernel | $_RECOVER_date | $_RECOVER_time | $_RECOVER_arch | Typ | 
EOF
    echo " | 
"
    show_version_info "$_RECOVER_current_version" /etc/rc.cfg
    echo " | 
"
    show_version_info "$_RECOVER_normal_version" /boot/rc.cfg
    if [ -f /boot/kernel2 ]
    then
        show_version_info "$_RECOVER_recover_version" /boot/rc2.cfg
        value=ask
    fi
    echo ""
    show_tab_footer
}
. /srv/www/include/cgi-helper
: ${FORM_action:=view}
check_rights "recover" "$FORM_action"
case $FORM_action in
    mkrecoverask)
        show_html_header "$_RECOVER_title"
        show_tab_header "$_RECOVER_titledo" no
        echo '
'
        show_error "" "$_RECOVER_msg1ask"
        show_versions
        cat <
$_RECOVER_msg2ask
EOF
        show_tab_footer
        show_html_footer
        ;;
    mkrecover|$_RECOVER_yes)
        show_html_header "$_RECOVER_title" "refresh=5;url=control_recover.cgi"
        show_tab_header "$_RECOVER_titledo" no
        echo '
'
        echo "$_RECOVER_output"
        show_info "" "`/usr/sbin/mkrecover.sh -webgui`
"
        echo '
'
        show_tab_footer
        show_html_footer
    ;;
    mkrestore)
        show_html_header "$_RECOVER_title" "refresh=5;url=control_recover.cgi"
        show_tab_header "$_RECOVER_titledorest" no
        echo '
'
        echo "$_RECOVER_output"
        show_info "" "`/usr/sbin/mkrecover.sh -webgui -restore`
"
        echo '
'
        show_tab_footer
        show_html_footer    
    ;;
    view|$_RECOVER_no)
        show_html_header "$_RECOVER_title"
        show_tab_header "$_RECOVER_titleshow" no
        show_versions
        
        if grep -q "fli4l_mode=normal" /proc/cmdline
        then
            cat <	
  $_RECOVER_desc2   
EOF
        fi
        
        if grep -q "fli4l_mode=recover" /proc/cmdline
        then
            cat <
EOF
        fi
        show_tab_footer
## new section for switch version for next boot - boot with syslinux
        if [ -f /boot/ldlinux.sys ]
        then
            if grep -q "DEFAULT n" /boot/syslinux.cfg
            then
                next_boot=$_RECOVER_normal_version
            fi
            if grep -q "DEFAULT r" /boot/syslinux.cfg
            then
                next_boot=$_RECOVER_recover_version
            fi
            echo "
"
            show_tab_header "$_RECOVER_bootnext" no
            cat << EOF
     $_RECOVER_bootnext : $next_boot     
EOF
            show_tab_footer
        fi
## new section for switch version for next boot - boot with pygrub or grub
        if [ -f /boot/grub/menu.lst ]
        then
            if grep -q "default 0" /boot/grub/menu.lst
            then
                next_boot=`grep title /boot/grub/menu.lst | head -n 1 | cut -d " " -f 2-`
            fi
            if grep -q "default 1" /boot/grub/menu.lst
            then
                next_boot=`grep title /boot/grub/menu.lst | head -n 2 | tail -n1 | cut -d " " -f 2-`
            fi
            echo "
"
            show_tab_header "$_RECOVER_bootnext" no
            cat << EOF
     $_RECOVER_bootnext : $next_boot     
EOF
            show_tab_footer
        fi
        ## LOG of mkrecover.sh
        if [ -f /var/lib/persistent/hd/recover.log ]
        then
            echo "
"
            show_tab_header "${_RECOVER_title}-LOG (30 $_MN_rows)" no
            echo ''
            echo ""
            echo "| $_RECOVERLOG_date | "
            echo "$_RECOVERLOG_event | "
            echo "
"
            cat /var/lib/persistent/hd/recover.log | do_tail 30 | sed '1!G;h;$!d' | format_output
            echo "
"
            show_tab_footer
        fi
        
        show_html_footer
        ;;
    mkrecoverswitch)
        show_html_header "$_RECOVER_title" "refresh=5;url=control_recover.cgi"
        show_info "$_RECOVER_title" "$_RECOVER_switch"
        echo "
"
        err=0
        # check for /boot
        if ! grep -q /boot /proc/mounts
        then
            show_error "MOUNT-ERROR" "/boot is not mounted!
`mount`
"
            err=1
        fi
        # check mount state of boot-partition
        if ! grep /boot /proc/mounts | grep -q rw 
        then
            show_error "MOUNT-ERROR" "/boot is readonly!
`grep /boot /proc/mounts`
"
            err=1
        fi
        if [ $err != "1" ]
        then
            if [ -f /boot/ldlinux.sys ]
            then
                wert=`sed -n 's/^DEFAULT \([rn]\).*/\1/p' /boot/syslinux.cfg`
                case $wert in
                    n) sed -i 's/^DEFAULT [rn].*/DEFAULT r/' /boot/syslinux.cfg;;
                    *) sed -i 's/^DEFAULT [rn].*/DEFAULT n/' /boot/syslinux.cfg;;
                esac
            fi
            
            #todo - code to switch version for pygrub/grub based systems
            if [ -f /boot/grub/menu.lst ]
            then
                wert=`sed -n 's/^default \([01]\).*/\1/p' /boot/grub/menu.lst`
                case $wert in
                    0) sed -i 's/^default [01].*/default 1/' /boot/grub/menu.lst;;
                    1) sed -i 's/^default [01].*/default 0/' /boot/grub/menu.lst;;
                esac
            fi
        fi
        show_html_footer
        ;;
esac