#! /bin/sh

# upgrade a libc5 (bo) machine to libc6 (hamm).

# The cd version of this script is designed to be located in a top
# level directory of the Official Debian 2.0 CD.  It may be run from
# anywhere (with appropriate pathname) without requiring any input
# from the user except as required by dpkg installation scripts.

# $Id: cd_autoup.sh,v 2.3 1999/01/08 19:33:08 bob Exp bob $
# 
# derived from Craig Sanders' <cas@taz.net.au> autoup.sh script

# Original Author: Craig Sanders <cas@taz.net.au>
#         and many others.  see autoup.changelog for details.

# Modified for use on the Official CD by Bob Hilliard <hilliard@flinet.com>
# 
# Copyright Status: This script is hereby placed in the public domain
#
# Revision History:
#   see autoup.changelog

# 0.1 - 1988-06-29 02:53:18
#     - removed all ftp and most interactive stuff
#
# 0.2 - 1998/07/01 02:10:39
#     - Commented out debugging statments; typo in final message
#
# 0.3 - 1998/07/01 03:37:27
#     - minor changes in messages

# 0.4 - 1998/07/01 19:40
#     - added note about running from CD

# 0.5 - 1998/07/01 21:46:13
#     - updated changelog, minor changes in text

# 0.6 - 1998/08/20 19:47:05
#     - libstdc++2.8 has moved from libs/ to base/
#     - libnet-perl has moved from interpreters/ to base/
#     - fixed PKGS_NET problem (should have been $PKGS_NETBASE and 
#       $PKGS_NETSTD)
#     - changed "^ii" search pattern to "^.i" when searching for installed -dev
#       pkgs
#     - Added message about upgrading to bo version of dpkg.
#     - changed definition of DM so script may be called from any directory

# 0.7 - 1988/08/20 22:30
#     - Changed note about running from CD

# 0.8 - 1988/08/21 21:08


#     - fixed uncommented comment, added comment on packages on hold

# 0.9 - 1988/08/26 09:18
#     - Changed DPKG_ARGS to DPKG_ARGS="-iBE --force-overwrite --force-hold"
#     - Removed note about testing for packages on hold

# 1.0 - 1998/09/05 10:50
#     - Commented out rev 0.9 description

# 2.0 - 1998/12/17
#     - hard code package source as .../upgrade/dev_files
#     - modified messages

# 2.1 - 1998/12/25
#     - minor editing of coments

# 2.2 - 1998/12/25
#     - added changelog entries for 2.1 and 2.2

# 2.3 - 1999/01/08
#     - Changed filename to "$RMFILE" in final message

# misc tweakable variables

DPKG="$(which dpkg)"
LDCONFIG=$(which ldconfig)

# uncomment for debugging
# set -x
# DPKG="echo dpkg"
# LDCONFIG="echo ldconfig"

DPKG_ARGS="-iBE --force-overwrite --force-hold"
DATE=$(date +%m%d-%T)
ARCH="binary-$(dpkg --print-installation-architecture)"


# package variables

PKGS_LDSO="`dirname $0`/deb_files/ldso_*.deb"
PKGS_LIBC5="`dirname $0`/deb_files/libc5_*.deb"
PKGS_LIBC6="`dirname $0`/deb_files/libc6_*.deb `dirname $0`/deb_files/timezones_*.deb `dirname $0`/deb_files/locales_*.deb"
PKGS_NCURSES="`dirname $0`/deb_files/ncurses3.0_*.deb `dirname $0`/deb_files/ncurses3.4_*.deb"
PKGS_LIBRL="`dirname $0`/deb_files/libreadline2_*.deb"
PKGS_LIBRLG="`dirname $0`/deb_files/libreadlineg2_*.deb"
PKGS_BASH="`dirname $0`/deb_files/bash_*.deb"
PKGS_LIBGPP="`dirname $0`/deb_files/libg++27_*.deb `dirname $0`/deb_files/libg++272_*.deb `dirname $0`/deb_files/libstdc++2.8_*.deb"
PKGS_DPKG="`dirname $0`/deb_files/dpkg_*.deb `dirname $0`/deb_files/dpkg-dev_*.deb"
PKGS_SLANG="`dirname $0`/deb_files/slang0.99.34_*.deb `dirname $0`/deb_files/slang0.99.38_*.deb"
PKGS_LIBGDBM="`dirname $0`/deb_files/libgdbm1_*.deb `dirname $0`/deb_files/libgdbmg1_*.deb"
PKGS_PERLBASE="`dirname $0`/deb_files/perl-base_*.deb"
PKGS_PERL="`dirname $0`/deb_files/perl_*.deb"
PKGS_MOREDPKG="`dirname $0`/deb_files/data-dumper_*.deb `dirname $0`/deb_files/libnet-perl_*.deb `dirname $0`/deb_files/dpkg-ftp_*.deb `dirname $0`/deb_files/dpkg-mountable_*.deb"
PKGS_NETBASE="`dirname $0`/deb_files/netbase_*.deb"
PKGS_NETSTD="`dirname $0`/deb_files/netstd_*.deb"

ALLPKGS="$PKGS_LDSO $PKGS_LIBC5 $PKGS_LIBC6 $PKGS_NCURSES $PKGS_LIBRL
         $PKGS_LIBRLG $PKGS_BASH $PKGS_LIBGPP $PKGS_DPKG $PKGS_SLANG 
         $PKGS_LIBGDBM $PKGS_PERLBASE $PKGS_PERL $PKGS_MOREDPKG 
         $PKGS_NETBASE $PKGS_NETSTD"

# sanity check that we can find the packages
# We don't need this if deb_files are included in thei directory
# echo $ALLPKGS
# 
# for i in $ALLPKGS ; do
#     echo -n "$(basename $i) "
#     if [ ! -f $i ] ; then
#         echo 
#         echo "Can't find $i!"
#         echo aborting upgrade.
#         exit 100
#     fi
# done
# 
# echo 
# echo 
# echo "all needed files found." 
# echo

#
# libc5
#
echo "installing libc5."

$DPKG $DPKG_ARGS $PKGS_LIBC5 || exit 2

#
# if this is a buzz system, then first upgrade dpkg to version 1.4.0.8 
#
DPKG_VER=$(dpkg -s dpkg | grep Version: | awk '{print $2}')
DPKG_MINOR=$(echo $DPKG_VER | awk -F"." '{print $2}')

if [ $DPKG_MINOR -lt 4 ] ; then
    echo "The version of dpkg on this system cannot be used to upgrade to hamm"
    echo "Please upgrade dpkg to ver. 1.4.0.8 as described in README-upgrade"
    echo "After upgrading dpkg, re-run this script."
    exit 2
fi


RMFILE="/root/removed-$DATE"

# Now we start the install

# First get rid of essential package timezone to permit installation of
# replacement timezones.
#
# I don't like forcing dpkg, but I can see no alternative. timezone is
# required, but shouldn't really be "essential", and this was corrected
# in bo.

$DPKG -l timezone | grep -q "^.i" && TIMEZONE=1

if [ "$TIMEZONE" = "1" ] ; then 
    echo
    echo "Removing package timezone to permit replacing it with timezones."
    echo

    $DPKG --force-remove-essential -r timezone 
fi

# Now build list of incompatible packages to be removed if installed.

RMPKGS="xmanpages perl-suid perl-debug wg15-locale libpthread0 xslib 
        splay boot-floppies localebin"

RMGREP=$(echo "$RMPKGS" | xargs echo | sed -e 's/ /\\\|/g')

PKGS_RM=$(dpkg -l | grep "^.i" | grep -w "$RMGREP" | awk '{print $2}')

# build up a list of installed -dev packages so that we can remove them.
#
# this is necessary even on machines which aren't doing libc6
# development because libc5 can't be upgraded to latest version without
# removal of libc5-dev which also necessitates removal of other -dev
# packages like libdb1-dev and libdl1-dev if they are installed.

DEVPACKAGES=$( dpkg --get-selections | 
    grep -v "dpkg-dev\|deinstall" | 
    cut -f1 |
    grep -- "-dev$\|-pic$\|-dbg$" )

# don't bother running 'dpkg -r' if there's nothing to remove.
RM_LIST="$DEVPACKAGES $PKGS_RM"

if [ ! "$RM_LIST" = " " ] ; then
    # log what gets removed.
    echo "Packages Removed by autopup script on $DATE :" > $RMFILE
    echo "Development packages: " >> $RMFILE
    echo "$DEVPACKAGES" >> $RMFILE
    echo "Other packages that were incompatible with the upgrade:" >> $RMFILE
    echo -n "timezone " >> $RMFILE
    echo $PKGS_RM >> $RMFILE

    # and finally remove them.
    echo 
    echo "removing incompatible and development packages."

    $DPKG --remove -B $RM_LIST || exit 1
fi 


# now install the new versions of things.  Just the bare minimum to let
# the user safely run dselect for the rest of the upgrade.

echo 
echo "installing packages."

# libc
#
$DPKG $DPKG_ARGS $PKGS_LDSO || exit 2
$DPKG $DPKG_ARGS $PKGS_LIBC6 || exit 2

# libreadline, ncurses, and bash
#
$DPKG $DPKG_ARGS $PKGS_NCURSES || exit 3
$DPKG $DPKG_ARGS $PKGS_LIBRL || exit 4
$DPKG $DPKG_ARGS $PKGS_LIBRLG || exit 5

# paranoia says run ldconfig NOW. don't laugh, i've needed to do this on
# some libc5-libc6 upgrades. i know that the postinst scripts for the
# libs are supposed to do it but ....
$LDCONFIG
$DPKG $DPKG_ARGS $PKGS_BASH || exit 6

# new dpkg
#
$DPKG $DPKG_ARGS $PKGS_LIBGPP || exit 7
$DPKG $DPKG_ARGS $PKGS_DPKG

# slang
$DPKG $DPKG_ARGS $PKGS_SLANG || exit 7

# perl
#
$DPKG $DPKG_ARGS $PKGS_LIBGDBM || exit 8
# paranoia says "run ldconfig now".
$LDCONFIG

# Unlike a bo installation, in hamm, dpkg removes perl when installing
# perl-base, and must de-configure many packages (9 on my system)
# first.  Since these packages can not be re-configured until perl is
# installed and configured, the following line will always cause an
# error, therefore the "|| exit 9" is commented out in this line.

$DPKG $DPKG_ARGS $PKGS_PERLBASE # || exit 9

[ -e /usr/lib/perl5/i486-linux/5.003/auto/Mail/.packlist ] && rm -f /usr/lib/perl5/i486-linux/5.003/auto/Mail/.packlist

$DPKG $DPKG_ARGS $PKGS_PERL # || exit 9

# When perl is setup, it should(?) configure the de-configured packages.
# However, paranoia says to comment out the "|| exit 9" and run
# configure. exit at this point if there are packages which failed to
# configure.

$DPKG --configure --pending || exit 9 

cat<<EOF 

I have tried to remove any packages that would be broken by the
upgrade, but I may have missed some.  Some "Errors were encountered
while processing:" messages were displayed earlier; they were
harmless.  If any such messages are visible now, make a note of the
package names, since they must be removed or replaced with their
equivalent from hamm.

press [ENTER] to continue"

EOF
read 

# strictly speaking, dpkg-ftp and dpkg-mountable are not essential to
# upgrade right now but they're both very useful.
$DPKG $DPKG_ARGS $PKGS_MOREDPKG

# and now netbase and netstd
$DPKG $DPKG_ARGS $PKGS_NETBASE
$DPKG $DPKG_ARGS $PKGS_NETSTD

# paranoia says to run this at the end
$DPKG --configure --pending

# paranoia says: "run sync", so lets do it :-)
sync ; sync ; sync 


# FINISHED!

# the user can now run dselect and select any -dev packages they want
# (and other packages too, of course :-)


more <<__EOF__


     At this point your system is stable, with most or all of the
packages upgraded to libc6.  You may now run dselect, using apt as the
access method, to upgrade any packages that are still libc5, and
install new ones if desired.

All development packages (-dev, -dbg, and -pic) and a number of other
incompatible packages have been removed during this upgrade procedure
due to conflicts between libc5 and libc6 versions.  A list of the
removed packages are in the file "$RMFILE".  You 
will have to install the libc6 equivalents of any of these packages
that you need.  If you will be doing any libc5 development work, you
will also want to re-install (from the old-libs directory) any of
these libc5 packages you need.

      Finally, remember to fix up wtmp and utmp, otherwise last and
who and sac, etc. won't work. Here's what Miquel van Smoorenburg
<miquels@cistron.nl>, the maintainer of sysvinit, recommended:

     1. You need to update ALL your packages to hamm
     2. Reboot if you haven't done that already
     3. You need to move the wtmp file and truncate the utmp file:
        cd /var/log
        mv wtmp wtmp.libc5
        touch wtmp
        cd /var/run
        cp /dev/null utmp
     4. You might want to reboot again to make sure

     This is because the "struct utmp" and thus the utmp and wtmp
"databases" are different between libc5 and libc6

__EOF__
