#!/bin/bash

PACKNAME=tp-conf-srv
CONFIG=/etc/${PACKNAME}/${PACKNAME}.conf
source ${CONFIG}

SHAREDIR=/usr/share/${PACKNAME}
SPATCH=${SHAREDIR}/spatch
DPKGSTATEFILE=/var/run/${PACKNAME}.dpkg

CLEAN=no
RESTART=yes
SILENT=no
VERBOSE=no
DPKGOPT=no
APTMODE=no
KERNEL=no

function usage () {
    echo "$0: [--upgrade|--update||--clean|--revert] [--silent|--verbose] [--restart|--dont-restart] [--dpkg]"
    echo "--upgrade - updates configuration and restart the services"
    echo "--update - updates configuration but don't restart the services"
    echo "--clean - try to remove configuration and restart services"
    echo "--revert - remove the patchs for easier updates, so don't restart ther services"
    echo "--restart - restart of the services"
    echo "--dont-restart - do not restart the services"
    echo "--kernel - running inside kernel install hooks"
}

# Tests if the patch is applied
function applied-patch () {
    if ${SPATCH} status $1 | grep -q " not applied " ; then
	# False
	return 1;
    else
	# True
	return 0;
    fi
}

# Deapply all the listed patches
function deapply-patches () {
    # Nao esta a funcionar
    for patch in $* ; do
	if applied-patch $patch ; then
	    ${SPATCH} deapply $patch
	fi
    done
    #${SPATCH} deapply $*
}

# Update to the most recent patch
function update-patches () {
    deapply-patches $*
    ${SPATCH} apply $1
}

# Condicionally update to the last patch
function update-clean-patches () {
    deapply-patches $*
    if [ $CLEAN = no ] ; then
	${SPATCH} apply $1
    fi
}

# Experimental function for adding line to sudoers
function append-sudoers () {
    if ! test -x /usr/sbin/visudo ; then
	return
    fi
    if ! fgrep -q "$1" /etc/sudoers ; then
	cp -p /etc/sudoers /etc/newsudoers
	echo >> /etc/newsudoers
	echo "$2" >> /etc/newsudoers
	echo "$1" >> /etc/newsudoers
	if visudo -q -c -s -f /etc/newsudoers ; then
	    echo 
	    echo "new sudoers generated with success and will be updated"
	    echo "Please check /etc/sudoers for possible errors"
	    mv /etc/newsudoers /etc/sudoers || return 1
	    echo "/etc/sudoers updated with success"
	else
	    visudo -c -s -f /etc/newsudoers 
	    echo
	    echo "new sudoers file /etc/newsudoers is broken"
	    echo "please report this bug to the package maintainer"
	    echo "The old /etc/sudoers is still in place"
	fi
    fi
}

######################################################################
# Funtions to configure packages

# Configures grub2 by manipulating /etc/default/grub file
function conf-grub2 () {
    defgrub=/etc/default/grub
    if test ! -f $defgrub ; then
	echo "$defgrub not found, grub-pc or grub-efi not installed?"
	return 1
    fi

    Grub210="10_default_grub_print.k_v00 10_default_grub_timeout_v00\
        10_default_grub_text_console_v00"
    Grub220="20_default_grub_savedefault_v00"
    deapply-patches $Grub220 $Grub210
    case $TpSrvGrubTimeOut in
	YES|Yes|yes)
	    update-clean-patches 10_default_grub_timeout_v00
	    ;;
	*)
	    deapply-patches 10_default_grub_timeout_v00
    esac
    case $TpSrvGrubTextConsole in
	YES|Yes|yes)
	    update-clean-patches 10_default_grub_text_console_v00
	    ;;
	*)
	    deapply-patches 10_default_grub_text_console_v00
    esac
    case $TpSrvGrubPrintkTimeN in
	YES|Yes|yes)
	    if ! egrep "^GRUB_CMDLINE_LINUX=.*printk\.time=n" $defgrub > /dev/null ; then
		
		update-clean-patches 10_default_grub_print.k_v00
	    else
		echo "Not applying patch because was found printk.time=n in kernel options"
	    fi
	    ;;
	*)
	    deapply-patches 10_default_grub_print.k_v00
    esac
    case $TpSrvGrubSaveDefault in
	YES|Yes|yes)
	    update-clean-patches 20_default_grub_savedefault_v00
	    ;;
	*)
	    deapply-patches 20_default_grub_savedefault_v00
    esac
    if [ ${RESTART} = "yes" -a -x /usr/sbin/update-grub ] ; then
	update-grub
    fi
}

######################################################################
# Parsing options
while [ $# -ge 1 ] ; do
    case $1 in
	--upgrade)
	    CLEAN=no
	    RESTART=yes
	    shift
	    ;;
	--update)
	    CLEAN=no
	    RESTART=no
	    shift
	    ;;
	--clean)
	    CLEAN=yes
	    RESTART=yes
	    shift
	    ;;
	--revert)
	    CLEAN=yes
	    RESTART=no
	    shift
	    ;;
	--silent)
	    SILENT=yes
	    VERBOSE=no
	    shift
	    ;;
	--verbose)
	    SILENT=no
	    VERBOSE=yes
	    shift
	    ;;
	--restart)
	    RESTART=yes
	    shift
	    ;;
	--dont-restart)
	    RESTART=no
	    shift
	    ;;
	--kernel)
	    KERNEL=yes
	    shift
	    ;;
	help)
	    usage
	    shift
	    exit 0
	    ;;
	*)
	    usage
	    CLEAN=no
	    RESTART=yes
	    shift
	    exit 1
    esac
done

if [ $KERNEL = "yes" ] ; then
    case $TpConfSrvDpkgMode in
	N*|n*)
	    if [ $SILENT = "no" ] ; then
		echo "dpkg mode disabled"
	    fi
	    exit 0
	    ;;
    esac
    if [ ! -e $DPKGSTATEFILE ] ; then
	if [ $SILENT = "no" ] ; then
	    echo "dpkg mode is not active"
	fi
	exit 0
    fi
fi


######################################################################
# Doing updates

conf-grub2
