#!/bin/bash
set -e

package_name()
{
    case $(uname -s) in
	GNU)
	    echo libc0.2
	;;
	Linux)
		case $(uname -m) in
		    alpha*)
			    echo libc6.1
		    ;;
			*)
			    echo libc6
		    ;;
		esac
	;;
    esac
}

zone_banner() {
    extra_info=
    for i in 1 2 3 4 5 6 7 8
    do
	TZdate=$(LANG=C TZ="$timezone" date)
	UTdate=$(LANG=C TZ=UTC0 date)
	TZsec=$(expr "$TZdate" : '.*:\([0-5][0-9]\)')
	UTsec=$(expr "$UTdate" : '.*:\([0-5][0-9]\)')
	case $TZsec in
	    $UTsec)
		    extra_info="
   Local time is now:      $TZdate.
   Universal Time is now:  $UTdate."
		    break
	    esac
    done
    echo "Current default timezone: '$timezone'.$extra_info"
    echo "Run 'tzconfig' if you wish to change it."
}

set_timezone() {
    echo "Running 'tzconfig' to set this system's timezone."
    /usr/sbin/tzconfig
}

# Borrowed from sysvinit's postinst.
#	Function like update-rc.d but simpler & faster.
#	Usage: updatercd basename start|stop NN runlevel .
#
#	Uses the real update-rc.d if file-rc is detected.
#
updatercd() {

	if [ -d /usr/lib/file-rc/. ]
	then
		update-rc.d "$@" > /dev/null
		return $?
	fi

	base=$1
	shift
	while [ "$1" != "" ]
	do
		if [ "$1" = stop ]
		then
			tlet=K
		else
			tlet=S
		fi
		lev=$2
		shift 2
		while [ "$1" != "." ]
		do
			cd /etc/rc$1.d
			tmp="`echo $tlet??$base`"
			case "$tmp" in
				"$tlet??$base")
					# Not present yet.
					ln -s ../init.d/$base $tlet$lev$base
					;;
				*)
					# Already present.
					;;
			esac
			shift
		done
		shift
	done
}

realpath()
{
    fname=${1%/} # strips trailing '/'
    while [ -L "$fname" ]; do
	oldfname="$fname"
	fname="$(command ls -l $fname)"
	fname="${fname#*\> }"
	if [ "$fname" = . ] ; then
	    fname="$(dirname $oldfname)"
	elif echo $fname | grep -vq '^/' - ; then
	    fname="$(dirname $oldfname)/$fname"
	fi
    done
    pushd $(dirname $fname) > /dev/null
    fname=$(pwd -P)/$(basename $fname)
    popd > /dev/null
    echo $fname
}

if [ "$1" = "configure" ]
then
    ldconfig

    if [ -d /usr/doc ]; then
	package=$(package_name)
	if [ ! -e /usr/doc/$package ]; then
	    (cd /usr/doc;
		if [ -d ../share/doc/$package ]; then
		    ln -s ../share/doc/$package $package ;
		fi
            )
	fi
    fi

    if [ ! -z $2 ]; then
	if [ ! -d /var/mail ] && [ ! -L /var/mail ]; then
	    ln -sf spool/mail /var/mail
	fi
	if dpkg --compare-versions $2 lt 2.1.1-0pre1.3; then
	    echo "Name Service Switch has changed in the C Library."
	    echo "Running programs may not be able to do NSS lookups until they are"
	    echo "restarted (for services such as ssh, this can affect your ability to"
	    echo "login)."
	    echo "Note: restarting sshd should not affect any existing connections."
	    echo
	    echo "The services restarted (if present) are:"
	    echo "smail, sendmail, exim, ssh, netbase, apache, boa, nis, and proftpd."
	    echo "If other services begin to fail mysteriously after this upgrade,"
	    echo "it may be necessary to restart them too."
	    echo -n "Do you wish to Restart Services? [Y/n] "
		read answer
		case $answer in
		    Y|y) answer=yes ;;
		    N|n) answer=no ;;
		    *) answer=yes ;;
		esac
		if [ "$answer" = yes ]; then
		    echo -n "Restarting services possibly affected by the upgrade:"
		    for service in nis smail sendmail exim ssh netbase apache proftpd boa;
		    do
			status=$(dpkg -s ${service} | awk '/^Status:/ {print $4}')
			if [ "$status" = installed ] && [ -x /etc/init.d/${service} ];
			then
			    echo -n " $service"
			    /etc/init.d/$service restart > /dev/null 2>&1 || true
			fi
		    done
		    echo "."
		fi
	    if [ -f /var/db/passwd.db ]; then
		rm -rf /var/db
		ln -sf /var/lib/misc /var/db
		cd /var/lib/misc && make
	    fi
	fi # end upgrading and $2 lt 2.1.1-0pre1.3
	if dpkg --compare-versions $2 lt 2.1.1-12 && \
	   dpkg --compare-versions $2 gt 2.1.1-0pre1.3
	then
	    if [ -f /var/db/passwd.db ]
	    then
		mv /var/db /var/db.old
		ln -sf /var/lib/misc /var/db
		cp -a /var/db.old/*.db /var/lib/misc/.
		rm -rf /var/db.old
	    elif [ -f /var/state/glibc/passwd.db ]
	    then
		mv /var/state/glibc /var/state/glibc.old
		ln -sf /var/lib/misc /var/state/glibc
		cp -a /var/state/glibc.old/*.db /var/lib/misc/.
		rm -rf /var/state/glibc.old
	    fi
	fi # end upgrading and $2 lt 2.1.1-12 && gt 2.1.1-0pre1.3
    fi # Upgrading
    if [ -f /etc/timezone.save ]; then
	mv -fb /etc/timezone.save /etc/timezone
    fi
    if [ -f /etc/timezone ]; then
	timezone=$(cat /etc/timezone)
    else
	timezone=Factory
    fi
    if [ "$timezone" = Factory ]; then
	if [ -L /etc/localtime ]; then
	    localtime_link=$(realpath /etc/localtime)
	    if [ -f "$localtime_link" ]; then
		link_not_dangling=true
	    fi
	    if [ "$link_not_dangling" = true ]; then
		timezone=$(echo $localtime_link | sed 's%^/usr/share/zoneinfo/%%')
	    fi
	fi
    fi
    if [ -f /usr/share/zoneinfo/$timezone ] && [ "$timezone" != Factory ]
    then
#	zic -l $timezone
	rm -f /etc/localtime && \
	ln -sf /usr/share/zoneinfo/$timezone /etc/localtime
	zone_banner
    # Handle problem caused by lame old tzconfig.
    elif [ "$timezone" = "US/Pacific-New" ]
    then
	echo "US/Pacific" > /etc/timezone
#	zic -l US/Pacific
	rm -f /etc/localtime && \
	ln -sf /usr/share/zoneinfo/US/Pacific /etc/localtime
	zone_banner
    else
	set_timezone
    fi
    if [ "$(date +%Z)" = "/etc/localtime" ]; then
	set_timezone
    fi
    # DO NOT FOLLOW THIS EXAMPLE IN OTHER PACKAGES
    updatercd devpts.sh start 35 S .
    /etc/init.d/devpts.sh 2>/dev/null || true
fi

if [ "`uname -s`" = Linux ]; then
#
#       Upgrade init if possible. There was a bug in all versions
#       up to and including 2.75-4, which didn't affect i386 but
#       did affect most other architectures.
#
sysvinitver="`dpkg -s sysvinit | grep ^Version: | sed -e 's/^Version: *//'`"
case "`uname -m`" in
        i?86)
                badsysvinitver="2.[0-6]*|2.7[0-3]*"
                ;;
        *)
                badsysvinitver="2.[0-6]*|2.7[0-4]*|2.75-*"
                ;;
esac
eval "case \"$sysvinitver\" in
        $badsysvinitver)
                ;;
        *)
                init u
                sleep 1
esac"
fi

exit 0
