#!/bin/sh
#
# $Id: upgrade-openbsd.sh,v 1.1 2003/09/03 01:49:34 dope Exp dope $
# 
# Copyright by Christian Schneider <strcat@gmx.net>
# 
# Filename       : ~/bin/upgrade-openbsd
# Purpose        : Upgrading OpenBSD
# Author         : Christian Schneider <strcat@gmx.net>
# Latest release : <http://strcat.neessen.net/>
#
# Last modified: [ 2003-12-23 01:50:57 ]
# 
# Use it at your own risk. No support and no help from my side.

VERSION="3.4"
ARCH="`uname -m`"
KERNEL="/bsd"
URLARCH="ftp://ftp.openbsd.org/pub/OpenBSD/$VERSION/i386"
KERNELCFGPATH="/usr/src/sys/arch/$ARCH/conf"
KRNLCONFIG=PAINLESS
WGET=/usr/local/bin/wget 
MERGEMASTER=/usr/local/sbin/mergemaster
VERSSHRT=`echo $VERSION|tr -d '.'`
UPGDIR=/usr/upgrade
DDIRS=`echo "$URLARCH/" | tr -s / | tr -d -c /`
CUT_DIRS=`expr $DDIRS : '.*' - 2`
WGETOPTS="-q -nH --passive-ftp"
BADCFG=/tmp/badcfg

if [ "$1" = "first" ]; then 

mkdir /usr/upgrade
cd /usr/upgrade

# get arch dependent files, could be launched in bg (if you have fast connection)
echo "Download the install files"
$WGET --cut-dirs=$CUT_DIRS $WGETOPTS $URLARCH/base$VERSSHRT.tgz 
$WGET --cut-dirs=$CUT_DIRS $WGETOPTS $URLARCH/comp$VERSSHRT.tgz 
$WGET --cut-dirs=$CUT_DIRS $WGETOPTS $URLARCH/etc$VERSSHRT.tgz 
$WGET --cut-dirs=$CUT_DIRS $WGETOPTS $URLARCH/man$VERSSHRT.tgz 
$WGET --cut-dirs=$CUT_DIRS $WGETOPTS $URLARCH/misc$VERSSHRT.tgz 

# - if you do not want to install X, comment apropriate sections
$WGET --cut-dirs=$CUT_DIRS $WGETOPTS $URLARCH/xbase$VERSSHRT.tgz 
$WGET --cut-dirs=$CUT_DIRS $WGETOPTS $URLARCH/xfont$VERSSHRT.tgz 
$WGET --cut-dirs=$CUT_DIRS $WGETOPTS $URLARCH/xserv$VERSSHRT.tgz 
$WGET --cut-dirs=$CUT_DIRS $WGETOPTS $URLARCH/xshare$VERSSHRT.tgz 

# get kernel sources
echo "Download the kernel sources"
$WGET --cut-dirs=`expr $CUT_DIRS - 1` $WGETOPTS `echo $URLARCH|sed s\/$ARCH\/\/`srcsys.tar.gz 

# backup kernel
echo "Backup the old kernel"
echo "/bsd => /bsd.`date "+%Y-%m-%d"`"
cp $KERNEL $KERNEL.`date "+%Y-%m-%d"`

# Backup /etc
echo "Make a backup of /etc"
cp -Rp /etc /old.etc &
cp -Rp /etc /tmp/upg.etc &

# backup old src tree
echo "Make a backup of the source-tree"
mv /usr/src /usr/src.old
mkdir /usr/src

# install man pages
echo "Install the Manpages"
cd / && tar -xpfz $UPGDIR/man$VERSSHRT.tgz

# extract new kernel sources
echo "Extract the kernel sources"
cd /usr/src && tar -xpfz $UPGDIR/srcsys.tar.gz 

# preserve old kernel config (rename it to FQDN if necessary)
cp /usr/src.old/sys/arch/i386/conf/$KRNLCONFIG $KERNELCFGPATH/$KRNLCONFIG

# install compiler and related things
echo "Install Compiler and other shit"
cd / && tar -xpfz $UPGDIR/comp$VERSSHRT.tgz

echo "I'm removing things that changed in /etc"
cd /tmp/upg.etc/
for i in *; do if cmp /usr/share/examples/etc/$i $i > /dev/null 2>&1 ; then rm $i; fi; done

# install X
echo "Install XFree86 .. maybe .. "
cd / && tar -pxfz $UPGDIR/xbase$VERSSHRT.tgz && 
tar -pxfz $UPGDIR/xfont$VERSSHRT.tgz &&
tar -pxfz $UPGDIR/xserv$VERSSHRT.tgz &&
tar -pxfz $UPGDIR/xshare$VERSSHRT.tgz 

# copy files we want to keep back to /etc
cd /tmp/upg.etc
mv myname mygate rc.local rc.conf master.passwd group ipf.rules aliases fstab profile hosts resolv.conf shells /etc

# remove files we do not want to keep
echo "remove files we do not want to merge"
rm -ri *

# rebuild passwd
/usr/sbin/pwd_mkdb -p /etc/master.passwd
newaliases

# merge old config into new 
done=0
echo "Build /etc distribution from /usr/src/etc [1] or use etc$VERSSHRT.tgz [2] ?"
while [ "$done" = "0" ]; do
read resp
  if [ "$resp" = "1" ]; then
    mergemaster -v -D /etc/
    done=1
  elif [ "$resp" = "2" ]; then
    tar xpfz etc$VERSSHRT.tgz ./etc
    mv etc /tmp/upg.etc
    mergemaster -r -v -t /tmp/upg.etc/ -D /etc/
    done=1
  else
    echo "Skipping merging /etc"
    done=1
  fi
done

# install main binaries
cd / && tar -xpfz $UPGDIR/base$VERSSHRT.tgz

cat << __EOF__

1. Reboot
2. Process to the second stage (kernel compiling)

you may also want to
- reinstall ports tree
- rebuild / reinstall some packages

__EOF__


elif [ "$1" = "second" ]; then

# make new kernel
cd $KERNELCFGPATH
config $KRNLCONFIG || ( touch $BADCFG;
echo "Edit $KERNELCFGPATH/$KRNLCONFIG and start $0 again"; exit )
cd ../compile/$KRNLCONFIG

echo "Compiling / installing kernel"
make depend && make clean && make
if [ ! -f bsd ]; then
  echo "*** Compiling failed ***"; exit
fi
cp -f bsd $KERNEL

cd /dev
./MAKEDEV all

# clean up
rm -rf /usr/upgrade

cat << __EOD__

Kernel compiled and installed at $KERNEL
do reboot

__EOD__

else
  echo "usage: $0 <first|second>"
  exit
fi
