#! /bin/sh
#
# Prepare a woody system for dist-upgrade to sarge.
#
# This script encodes the information given in
# http://www.debian.org/releases/testing/i386/release-notes/ch-upgrading.en.html
#
# Author: Vince McIntyre, ATNF/CSIRO
# License: GPL2 and higher
#
# $Id: upgrade-prep.sh,v 1.5 2005/04/14 02:41:09 mci156 Exp $
#

# Configuration ---------------------------------------------------------------
tmpdirparent="."
tmpdir="upgrade-prep"
backuplist="/etc /var/lib/dpkg"

PATH=/bin:/usr/bin:/sbin:/usr/sbin; export PATH

# Main ------------------------------------------------------------------------
Fail () { echo "$*"; exit 1; } ;

# check id is root, so we can back up all of /etc.
[ "0" = "`id -u`" ] || Fail "You are not root. Please login as root before running this program."

# Check aptitude is installed.
if ! dpkg -l aptitude 2>/dev/null | grep -q ^ii ; then
  Fail "
    The aptitude package is not installed. This is the recommended
    package for upgrading to Debian sarge, please install it.
"
fi

[ -w "$tmpdirparent" ] || \
  Fail "
    No write permission for directory $tmpdirparent.
    I need write permission, I'm going to store some info about your system
    there, in $tmpdirparent/$tmpdir.
"

if [ -e "$tmpdirparent/$tmpdir" ] ; then
   Fail "Output directory $tmpdirparent/$tmpdir exists. Please remove it."
else
   mkdir "$tmpdirparent/$tmpdir"
fi

set -e
cd "$tmpdirparent/$tmpdir"
echo "capturing basic system information"
infofile="system.info"
echo "date:"                                   >  "$infofile"
date                                           >> "$infofile"
echo "---"                                     >> "$infofile"
echo "current dir:"                            >> "$infofile"
pwd                                            >> "$infofile"
echo "---"                                     >> "$infofile"
echo "uname -a:"                               >> "$infofile"
uname -a 2>&1                                  >> "$infofile"
echo "---"                                     >> "$infofile"
echo "filesystems:"                            >> "$infofile"
mount 2>&1                                     >> "$infofile"
echo "---"                                     >> "$infofile"
echo "free space:"                             >> "$infofile"
df -k 2>&1                                     >> "$infofile"
echo "---"                                     >> "$infofile"
echo "network settings:"                       >> "$infofile"
ifconfig -a 2>&1                               >> "$infofile"
echo "---"                                     >> "$infofile"
echo "lspci info:"                             >> "$infofile"
(lspci;lspci -n;) 2>&1 |sort -n                >> "$infofile"
echo "---"                                     >> "$infofile"
echo "cpu info:"                               >> "$infofile"
cat /proc/cpuinfo                              >> "$infofile"
echo "---"                                     >> "$infofile"
echo "mem info:"                               >> "$infofile"
cat /proc/meminfo                              >> "$infofile"
echo "---"                                     >> "$infofile"

echo "Backup up the directories $backuplist"
tar zcpf backup.tar.gz $backuplist

echo "Backing up package selections"
dpkg --get-selections \* >package-selections.preupdate.txt 

echo "Checking package database is sane"
dpkg --audit 1>dpkg-audit.txt 2>&1

set +e   # we will be using grep return status, which is 'error' on no match

badpkgs=`wc -l dpkg-audit.txt 2>/dev/null |awk '{print $1}'`
if [ "0" != "$badpkgs" ] ; then
   #check for holds
   grep -w hold dpkg-audit.txt 1>/dev/null 2>&1
   if [ 0 -eq $? ] ; then
     echo "
    Warning: you have some packages with 'hold' status.
    Usually, these should be set to 'unhold', or they will not be upgraded
    (and this could break your system).

    The exception to this rule are packages you have compiled yourself.
    Often you will not want to upgrade them, so leave the 'hold' state alone.

    To unhold packages, run 'aptitude unhold <packagename>'.

    The list of packages is as follows:
"
      cat dpkg-audit.txt | pager
   fi
fi

# check for pinning
if [ -e /etc/apt/preferences ]; then
  if [ 0 -ne `grep -c Pin /etc/apt/preferences 2>/dev/null` ] ; then
     echo "
   Warning: you appear to have 'pinning' turned on in /etc/apt/preferences.
   Please turn off pinning (by editing /etc/apt/preferences) before trying
   to upgrade your system.
"
  fi
fi

# check for sanity in deb selections
lines=`grep -w deb /etc/apt/sources.list 2>/dev/null | \
       grep -v stable | grep -v \#`
if [ "X" != "X${lines}" ] ; then
  echo "
    Warning: you have some lines in /etc/apt/sources.list that are not
    suitable when upgrading your system. I recommend that you comment
    out the following lines, and rerun 'apt-get update':

$lines

    Essentially you should have only lines that refer to the 'stable'
    suite of packages. The same goes for deb-src lines.
"
  exit 0
fi

# misc checks
[ -d /etc/rcS.d ] || Fail "
   The /etc/rcS.d directory is missing. This must exist or the installation
   of the 'libc6' package will fail during upgrade.
"

[ -L /usr/share/doc ] && \
    Fail '
   The /usr/share/doc directory is a symbolic link. This should be a real
   directory, if it is not some packages break. You may make /usr/doc a
   symlink to /usr/share/doc, however. To summarise:
     /usr/share/doc -> <anything>    NOT allowed
     /usr/doc -> /usr/share/doc          allowed
' 

# our work here is done
cat >instructions.txt <<EOF

    Congratulations - it looks like your system is ready for upgrading.

    All the details of your existing system that I noted are stored in
      $tmpdirparent/$tmpdir.
    Please make sure to copy the files in there to a safe place.

    Warning: do not try to upgrade from an X session, or via remote
    connection (telnet, rlogin, rsh, ssh).

    Next steps:
      0. Log into a text mode console terminal on your machine,
	 e.g. by typing [ctrl][alt][F1]. (see warning above)
      1. script -a ~/upgrade-to-sarge.typescript
         (you may need to install 'script')
      2. aptitude update
      3. aptitude -f --with-recommends dist-upgrade 
      4. exit
         (to exit the script invocation)
      5. Before rebooting, be sure to read this file:
         /usr/share/doc/xfree86-common/README.Debian-upgrade

    For further information, consult http://www.debian.org/releases/testing/

    To report how your upgrade went, please send an email to
      submit@bugs.debian.org
    with the first line being:
      package: upgrade-reports

    Reports of success are just as welcome as problem reports.

    All this information is stored in the file $tmpdirparent/$tmpdir/instructions.txt

EOF

pager instructions.txt || Fail "Please read  $tmpdirparent/$tmpdir/instructions.txt"

#
