#!/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

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 "--dpkg - only perform actions if dpkg mode is enabled"
    echo "--apt - run using apt mode"
}

# 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

function conf-tiger-cronrc () {
    cronrc=/etc/tiger/cronrc
    if test ! -f $cronrc ; then
	echo "$cronrc no found, tiger not installed?"
	return 1
    fi

    CronrcUsers="10_tiger_cronrc_users_v00"
    CronrcFiles="10_tiger_cronrc_files_v01 10_tiger_cronrc_files_v00"
    # Decoment latter when there are more patches
    #deapply-patches $CronrcUsers $CronrcFiles
    case $TpConfSrvTigerNetUsers in
	Yes|yes|YES)
	    update-clean-patches 10_tiger_cronrc_users_v00
	    ;;
	*)
	    deapply-patches 10_tiger_cronrc_users_v00
    esac
    case $TpConfSrvTigerNetFiles in
	Yes|yes|YES)
	    update-clean-patches $CronrcFiles
	    ;;
	*)
	    deapply-patches $CronrcFiles
    esac
}

function conf-sysklog () {
    SysklogdDailyPatches="10_sysklogd_cron.daily_wheezy_v00\
        10_sysklogd_cron.daily_lenny_v00\
        10_sysklogd_cron.daily_v02\
	10_sysklogd_cron.daily_v01 10_sysklogd_cron.daily_v00"
    SysklogdWeeklyPatches="10_sysklogd_cron.weekly_wheezy_v00\
        10_sysklogd_cron.weekly_lenny_v00\
        10_sysklogd_cron.weekly_v02\
	10_sysklogd_cron.weekly_v01 10_sysklogd_cron.weekly_v00"
# Need original cron.*/sysklogd files to detect the proper versions
    deapply-patches $SysklogdDailyPatches $SysklogdWeeklyPatches
    if egrep 'USER=\$\(ps -C syslogd -o user= \| head -n 1\)' /etc/cron.daily/sysklogd > /dev/null ; then
	echo "sysklogd from wheezy"
	update-clean-patches 10_sysklogd_cron.daily_wheezy_v00\
	    $SysklogdDailyPatches
	update-clean-patches 10_sysklogd_cron.weekly_wheezy_v00\
	    $SysklogdWeeklyPatches
    fi
    if egrep '^logs=\$\(syslogd-listfiles\)$' /etc/cron.daily/sysklogd > /dev/null ; then
	echo "sysklogd from lenny"
	update-clean-patches 10_sysklogd_cron.daily_lenny_v00\
	    $SysklogdDailyPatches
	update-clean-patches 10_sysklogd_cron.weekly_lenny_v00\
	    $SysklogdWeeklyPatches
    fi
    if egrep '^for LOG in `syslogd-listfiles`$' /etc/cron.daily/sysklogd > /dev/null ; then
	echo "sysklogd from etch or older"
	update-clean-patches 10_sysklogd_cron.daily_v02\
	    $SysklogdDailyPatches
	update-clean-patches 10_sysklogd_cron.weekly_v02.dpatch\
	    $SysklogdWeeklyPatches
    fi
}

function conf-rsyslog () {
    RsyslogPatches="10_rsyslog_logrotate_v00"
    update-clean-patches $RsyslogPatches
}

function conf-sysctl_conf () {
    SysctlConf="10_sysctl.conf_v01 10_sysctl.conf_v00 \
        10_sysctl.conf_routers_small_v01 10_sysctl.conf_routers_small_v00 \
        10_sysctl.conf_routers_normal_v01 10_sysctl.conf_routers_normal_v00 \
        10_sysctl.conf_routers_big_v01 10_sysctl.conf_routers_big_v00"

    case $TpSrvConfNetOptionsRouter in
	BIG)
	    update-clean-patches 10_sysctl.conf_routers_big_v01 \
		$SysctlConf
	    ;;
	Yes|yes|YES|NORMAL)
	    update-clean-patches 10_sysctl.conf_routers_normal_v01 \
		$SysctlConf
	    ;;
	SMALL)
	    update-clean-patches 10_sysctl.conf_routers_small_v01 \
		$SysctlConf
	    ;;
	*)
	    deapply-patches 10_sysctl.conf_v01 \
		$SysctlConf
    esac
}

function conf-security-groups () {
    #Patching /etc/security/groups
    case $TpConfSrvConsolePermissions in
	Y*|y*)
	    update-clean-patches 10_security_group.conf_v01 \
		10_security_group.conf_v00
	    update-clean-patches 10_dbus-1_system.d_hal.conf_v00
	    ;;
	*)
	    deapply-patches 10_security_group.conf_v01 \
		10_security_group.conf_v00
	    deapply-patches 10_dbus-1_system.d_hal.conf_v00
    esac

    case $TpConfSrvSshdPermissions in
	Y*|y*)
	    update-clean-patches 10_security_group.conf_sshd_v00
	    ;;
	*)
	    deapply-patches 10_security_group.conf_sshd_v00
    esac
}

# 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
}

# Defines insserv concurrency by configuring /etc/default/rcS
function conf-default-rcS () {
    defrcS=/etc/default/rcS
    if test ! -f $defrcS ; then
	echo "$defrcS not found, very strange"
	return 1
    fi

    if ! grep "^CONCURRENCY=" $defrcS > /dev/null ; then
	cat <<EOF >> "$defrcS"
# Configures boot parallelism of insserv
# makefile - parallel boot
# startpar, shell - deprecated on squeeze use makefile instead
# none - traditional sequential boot
CONCURRENCY=none
EOF
    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
	    ;;
	--dpkg)
	    DPKGOPT=yes
	    shift
	    ;;
	--apt)
	    APTMODE=yes
	    echo "Running in dpkg mode"
	    shift
	    ;;
	help)
	    usage
	    shift
	    exit 0
	    ;;
	*)
	    usage
	    CLEAN=no
	    RESTART=yes
	    shift
	    exit 1
    esac
done

if [ $DPKGOPT = "yes" ] ; then
    case $TpConfSrvDpkgMode in
	N*|n*)
	    if [ $SILENT = "no" ] ; then
		echo "dpkg mode disabled"
	    fi
	    exit 0
	    ;;
    esac
    if [ $CLEAN = "yes" ] ; then
	touch $DPKGSTATEFILE
    else
	rm -f $DPKGSTATEFILE
    fi
fi

case $TpConfSrvAptMode in
    Y*|y*)
	echo "Running in apt mode"
	if [ $APTMODE = "yes" ] ; then
	    cat > /tmp/$PACKNAME.apt-mode
	    exit 0
	fi
    ;;
    *)
	if [ $APTMODE = "yes" ] ; then
	    if [ $SILENT = "no" ] ; then
		echo "apt mode disabled"
	    fi
	    exit 0
	fi
esac

######################################################################
# Show warnings about this update script
if [ $SILENT = "no" ] ; then
    ${SHAREDIR}/update-tripwire --warning
    echo "Press enter to continue"
    read
fi


######################################################################
# Doing updates
if [ -r /etc/cron.daily/acct ] ; then
    deapply-patches 10_acct_stretch_cron.daily_v00 10_acct_cron.daily_v00
    if grep -q "invoke-rc.d" /etc/cron.daily/acct ; then
        update-clean-patches 10_acct_stretch_cron.daily_v00
    else
        update-clean-patches 10_acct_cron.daily_v00
    fi
fi


#Patching ldap.conf moved to tp-conf-ldap-client
if [ -r /etc/ldap/ldap.conf ] ; then
    deapply-patches 10_ldap_client_tagus_v00 10_ldap_client_alameda_v00
fi

#Patching policy of rotating logs for more than 1 year
update-clean-patches 10_logrotate.conf_v01 10_logrotate.conf_v00 
if test -r /etc/cron.daily/sysklogd ; then
    conf-sysklog
fi
if test -r /etc/logrotate.d/rsyslog ; then
    conf-rsyslog
fi

NtpEtchPatches="10_ntp.conf_etch_alameda_v01 10_ntp.conf_etch_alameda_v00\
    10_ntp.conf_etch_tagus_v01 10_ntp.conf_etch_tagus_v00\
    10_ntp.conf_etch_reference_v00\
    10_ntp.conf_etch_reference_v01\
"
NtpdateEtchPatches="10_ntpdate_alameda_v00 10_ntpdate_alameda_v01\
    10_ntpdate_tagus_v02\
    10_ntpdate_tagus_v01 10_ntpdate_tagus_v00\
"
if egrep "server 0.debian.pool.ntp.org iburst$" /etc/ntp.conf > /dev/null ; then
    echo "ntp.conf from etch or squeeze"
if egrep "# pool.ntp.org maps to more than 300 low-stratum NTP servers.$" /etc/ntp.conf > /dev/null ; then
    echo "ntp.conf from etch"
    case $TpSrvConfNtpRef in
	tagus)
	    deapply-patches 10_ntp.conf_etch_alameda_v01 10_ntp.conf_etch_alameda_v00
	    #deapply-patches 10_ntpdate_alameda_v00
	    update-clean-patches 10_ntp.conf_etch_tagus_v01 10_ntp.conf_etch_tagus_v00
	    if ! egrep "NTPDATE_USE_NTP_CONF=yes" /etc/default/ntpdate > /dev/null ; then
		update-clean-patches 10_ntpdate_tagus_v00 10_ntpdate_tagus_v01 10_ntpdate_tagus_v02
	    fi
	    ;;
	alameda)
	    deapply-patches 10_ntp.conf_etch_tagus_v01 10_ntp.conf_etch_tagus_v00 
	    #deapply-patches 10_ntpdate_tagus_v01 10_ntpdate_tagus_v00 
	    update-clean-patches 10_ntp.conf_etch_alameda_v01 10_ntp.conf_etch_alameda_v00
	    if ! egrep "NTPDATE_USE_NTP_CONF=yes" /etc/default/ntpdate > /dev/null ; then
		update-clean-patches 10_ntpdate_alameda_v00 10_ntpdate_alameda_v01
	    fi
	    ;;
	reference)
	    deapply-patches \
		10_ntp.conf_etch_tagus_v01 10_ntp.conf_etch_tagus_v00 
	    deapply-patches \
		10_ntp.conf_etch_alameda_v01 10_ntp.conf_etch_alameda_v00
	    deapply-patches \
		10_ntpdate_tagus_v00 10_ntpdate_tagus_v01 10_ntpdate_tagus_v02\
		10_ntpdate_alameda_v00 10_ntpdate_alameda_v01
	    update-clean-patches \
		10_ntp.conf_etch_reference_v01
	    ;;
	pool)
	    deapply-patches 10_ntp.conf_etch_tagus_v01 10_ntp.conf_etch_tagus_v00 
	    #deapply-patches 10_ntpdate_tagus_v01 10_ntpdate_tagus_v00 
	    ;;
	*)
	    echo "Unknown NTP Reference $TpSrvConfNtpRef"
	    echo "Use only [reference|tagus|alameda|pool]"
    esac
fi
fi
NtpLennyPatches="10_ntp.conf_lenny_alameda_v00\
    10_ntp.conf_lenny_tagus_v00\
    10_ntp.conf_lenny_reference_v00.dpatch\
    10_ntp.conf_lenny_reference_v01.dpatch\
"
NtpSqueezePatches="10_ntp.conf_squeeze_alameda_v00\
    10_ntp.conf_squeeze_tagus_v00\
    10_ntp.conf_squeeze_reference_v00\
"
if egrep "^# pool.ntp.org maps to about 1000 low-stratum NTP servers" /etc/ntp.conf > /dev/null ; then
    echo "ntp.conf from lenny or squeeze"

if egrep "server 0.debian.pool.ntp.org iburst dynamic$" /etc/ntp.conf > /dev/null ; then
    echo "ntp.conf from lenny"
    case $TpSrvConfNtpRef in
	tagus)
	    deapply-patches ${NtpLennyPatches} \
		${NtpEtchPatches} ${NtpSargePatches} 
	    deapply-patches ${NtpdateEtchPatches} ${NtpdateSargePatches}
	    update-clean-patches \
		10_ntp.conf_lenny_tagus_v00
	    ;;
	alameda)
	    deapply-patches ${NtpLennyPatches} \
		${NtpEtchPatches} ${NtpSargePatches} 
	    deapply-patches ${NtpdateEtchPatches} ${NtpdateSargePatches}
	    update-clean-patches \
		10_ntp.conf_lenny_alameda_v00
	    ;;
	reference)
	    deapply-patches ${NtpLennyPatches} \
		${NtpEtchPatches} ${NtpSargePatches} 
	    deapply-patches ${NtpdateEtchPatches} ${NtpdateSargePatches}
	    update-clean-patches \
		10_ntp.conf_lenny_reference_v01
	    ;;
	pool)
	    deapply-patches ${NtpLennyPatches} \
		${NtpEtchPatches} ${NtpSargePatches} 
	    deapply-patches ${NtpdateEtchPatches} ${NtpdateSargePatches}
	    ;;
	*)
	    echo "Unknown NTP Reference $TpSrvConfNtpRef"
	    echo "Use only [reference|tagus|alameda|pool]"
    esac
fi
if egrep "server 0.debian.pool.ntp.org iburst$" /etc/ntp.conf > /dev/null ; then
    echo "ntp.conf squeeze"
    case $TpSrvConfNtpRef in
	tagus)
	    deapply-patches ${NtpSqueezePatches} ${NtpLennyPatches} \
		${NtpEtchPatches} ${NtpSqueezePatches} 
	    deapply-patches ${NtpdateEtchPatches} ${NtpdateSargePatches}
	    update-clean-patches \
		10_ntp.conf_squeeze_tagus_v00
	    ;;
	alameda)
	    deapply-patches ${NtpSqueezePatches} ${NtpLennyPatches} \
		${NtpEtchPatches} ${NtpSargePatches} 
	    deapply-patches ${NtpdateEtchPatches} ${NtpdateSargePatches}
	    update-clean-patches \
		10_ntp.conf_squeeze_alameda_v00
	    ;;
	reference)
	    deapply-patches ${NtpSqueezePatches} ${NtpLennyPatches} \
		${NtpEtchPatches} ${NtpSargePatches} 
	    deapply-patches ${NtpdateEtchPatches} ${NtpdateSargePatches}
	    update-clean-patches \
		10_ntp.conf_squeeze_reference_v00
	    ;;
	pool)
	    deapply-patches ${NtpSqueezePatches} ${NtpLennyPatches} \
		${NtpEtchPatches} ${NtpSargePatches} 
	    deapply-patches ${NtpdateEtchPatches} ${NtpdateSargePatches}
	    ;;
	*)
	    echo "Unknown NTP Reference $TpSrvConfNtpRef"
	    echo "Use only [reference|tagus|alameda|pool]"
    esac
fi
fi

# on sarge
if [ ${RESTART} = "yes" -a -x /etc/init.d/ntp-server ] ; then
    invoke-rc.d ntp-server restart
fi
# on etch and lenny
if [ ${RESTART} = "yes" -a -x /etc/init.d/ntp ] ; then
    invoke-rc.d ntp restart
fi

conf-tiger-cronrc

# patching nrpe server
if [ -f /etc/nagios/nrpe.cfg ] ; then
    case $TpNrpeSetup in
	# Yes|yes|YES)
	#     update-clean-patches \
	# 	10_nrpe_setup_v00
	#     cp -p /etc/sudoers /etc/sudoers.old
	#     append-sudoers "nagios ALL=(ALL) NOPASSWD: /usr/bin/arrayprobe" \
	# 	"# Nagios NRPE privilege specification for check_hp_raid"
	#     append-sudoers "nagios ALL=(ALL) NOPASSWD: /usr/lib/tp-nagios-plugins/check_3ware-raid -m" \
	# 	"# Nagios NRPE privilege specification for check_3ware_raid"
	#     append-sudoers "nagios ALL=(ALL) NOPASSWD: /usr/sbin/smartctl" \
	# 	"# Nagios NRPE privilege specification for SMART checks"
	#     ;;
	*)
	    deapply-patches \
		10_nrpe_setup_v00
    esac
fi

if [ ${RESTART} = "yes" -a -x /etc/init.d/nagios-nrpe-server ] ; then
    invoke-rc.d nagios-nrpe-server restart
fi


# openssh-server on etch don't need patch
deapply-patches 10_sshd_config_v00 10_sshd_config
if egrep "[[:space:]]*X11Forwarding[[:space:]]+yes" /etc/ssh/sshd_config > /dev/null ; then
    echo "openssh-server on etch don't need patch"
else
    echo "Patching /etc/ssh/sshd_config is obsolete, please update"
    update-clean-patches 10_sshd_config_v00 10_sshd_config 
    if [ ${RESTART} = "yes" ] ; then
	invoke-rc.d ssh reload
    fi
fi

TripwirePolPatches="10_tripwire_twpoltxt_v01 10_tripwire_twpoltxt_v00\
    10_tripwire_v01 10_tripwire_v00 10_tripwire"
TripwireCfgPatches="10_tripwire_twcfgtxt_squeeze_v00\
    10_tripwire_twcfgtxt_v01 10_tripwire_twcfgtxt_v00\
    10_tripwire_v01 10_tripwire_v00 10_tripwire"

# Patching tripwire
if  ( applied-patch 10_tripwire || applied-patch 10_tripwire_v00 ||
	applied-patch 10_tripwire_v01 || 
	applied-patch 10_tripwire_twpoltxt_v00 || 
	applied-patch 10_tripwire_twpoltxt_v01 ) ; then 
    ${SHAREDIR}/update-tripwire --silent --clean
fi
update-clean-patches ${TripwirePolPatches}
if ( applied-patch 10_tripwire_twpoltxt_v01 ) ; then
    ${SHAREDIR}/update-tripwire --silent
fi
deapply-patches ${TripwireCfgPatches}
if egrep "/usr/bin/editor" /etc/tripwire/twcfg.txt > /dev/null ; then
    update-clean-patches 10_tripwire_twcfgtxt_squeeze_v00
else
    update-clean-patches 10_tripwire_twcfgtxt_v01
fi

update-clean-patches 10_tigerrc_v06 10_tigerrc 10_tigerrc_v00 10_tigerrc_v01 10_tigerrc_v02 10_tigerrc_v03 10_tigerrc_v04 10_tigerrc_v05

conf-sysctl_conf
if [ ${RESTART} = "yes" ] ; then
    if test -x /etc/init.d/procps ; then
	invoke-rc.d procps start
    else
	sysctl -p
    fi
fi

conf-security-groups

#Patching /etc/modules
ModulesAllPatches="20_modules_firewalls_v01 20_modules_firewalls_v00 \
20_modules_routers_v00 \
20_modules_tagus_policy_v01 20_modules_tagus_policy_v00 \
10_modules_prerequisites_v00 \
15_modules_prerequisites_loop_v00 10_modules_prerequisites_loop_v00"
deapply-patches $ModulesAllPatches
if egrep "^loop$" /etc/modules > /dev/null ; then
    if [ `wc -l /etc/modules | cut -f 1 -d ' '` = "6" ] ; then
	update-clean-patches 10_modules_prerequisites_loop_v00
    fi
    update-clean-patches 15_modules_prerequisites_loop_v00
else
    update-clean-patches 10_modules_prerequisites_v00
fi
update-clean-patches 20_modules_tagus_policy_v01 20_modules_tagus_policy_v00 
case $TpSrvConfNetOptionsRouter in
    Yes|yes|YES|SMALL|BIG|NORMAL)
	update-clean-patches 20_modules_routers_v00
	;;
esac

case $TpSrvConfNetOptionsFirewall in
    Yes|yes|YES)
	update-clean-patches 20_modules_firewalls_v01 
	;;
    *)
esac

if test -w /etc/default/snmpd ; then
    case $TpConfSrvSnmpdNetEnabled in
	Y*)
            if egrep "^# This file controls the activity of snmpd$" /etc/default/snmpd > /dev/null ; then
                update-clean-patches 10_default_snmpd_jessie_v00
            else
	        if egrep "127\.0\.0\.1" /etc/default/snmpd > /dev/null ; then
		    update-clean-patches 10_default_snmpd_v00 10_default_snmpd_squeeze_v00
	        else
		    update-clean-patches 10_default_snmpd_squeeze_v00 10_default_snmpd_v00 
	        fi
            fi
	    ;;
	N*)
	    deapply-patches 10_default_snmpd_v00 10_default_snmpd_squeeze_v00 10_default_snmpd_jessie_v00
	    ;;
	*)
	    echo "Unknown option TpConfSrvSnmpdNetEnabled=$TpConfSrvSnmpdNetEnabled"
    esac
fi

if test -w /etc/snmp/snmpd.conf ; then
    case $TpConfSrvSnmpdAuth in
	Y*)
	    deapply-patches 10_snmpd_jessie_base_v00
            if egrep "^ rocommunity6 public  default   -V systemonly$" /etc/snmp/snmpd.conf > /dev/null ; then
                update-clean-patches 10_snmpd_jessie_base_v00
            else
                echo "PLEASE upgrade config of snmpd to jessie"
            fi
            ;;
	N*)
	    deapply-patches 10_snmpd_jessie_base_v00
	    ;;
	*)
	    echo "Unknown option TpConfSrvSnmpdNetEnabled=$TpConfSrvSnmpdNetEnabled"
    esac
fi

if [ ${RESTART} = "yes" -a -x /etc/init.d/snmpd ] ; then
    invoke-rc.d snmpd restart
fi


if [ -w /etc/iceweasel/pref/iceweasel.js ] ; then
    case $TpConfSrvIceweaselCache in
	Y*)
	    deapply-patches  10_iceweasel_cache_v00
	    ;;
	N*)
	    update-clean-patches 10_iceweasel_cache_v00
	    ;;
	*)
	    echo "Unknow option TpConfSrvIceweaselCache=$TpConfSrvIceweaselCache"
    esac
fi

if [ -w /etc/iceape/pref/base.js ] ; then
    case $TpConfSrvIceapeCache in
	Y*)
	    deapply-patches  10_iceape_cache_v00
	    ;;
	N*)
	    update-clean-patches 10_iceape_cache_v00
	    ;;
	*)
	    echo "Unknow option TpConfSrvIceapeCache=$TpConfSrvIceapeCache"
    esac
fi

if which update-gconf-defaults ; then
    case $TpConfSrvGaleonCache in
	Y*)
	    > /usr/share/gconf/defaults/20_galeon_cache
	    update-gconf-defaults
	    ;;
	N*)
	    echo "/apps/galeon/Advanced/Network/disk_cache_size 0" \
		> /usr/share/gconf/defaults/20_galeon_cache
	    update-gconf-defaults
	    ;;
	*)
	    echo "Unknown option TpConfSrvGaleonCache=$TpConfSrvGaleonCache"
    esac
fi

TpConfJavaAlternative=${TpConfJavaAlternative:-none}
case $TpConfJavaAlternative in
    auto)
	update-java-alternatives -a
	;;
    none)
	echo -n
	;;
    *)
	update-java-alternatives -s $TpConfJavaAlternative
esac

conf-grub2
conf-default-rcS

if [ $RESTART = no ] ; then
    echo "You may need to restart the following services:"
    echo "    ntp ssh"
fi
