#!/bin/sh --
if [ -f cdrom.inf ]; then
  VERSION=`cat cdrom.inf | cut -f 3 -d " "`
else
  VERSION="2.2-RELENG"
fi

echo "This script will upgrade your FreeBSD-2.x to $VERSION"
echo "This script is written by jdli@csie.nctu.edu.tw, use it on your own risk"
echo "Hit Enter to go, or Ctrl-C to abort."
read ready

if [ "`id -u`" != "0" ]; then
        echo "Sorry, you are not root."
        exit 1
fi

echo "Changing flags ...."
chflags -R noschg /kernel.GENERIC /lkm /bin /sbin /usr/bin /usr/sbin /usr/lib /usr/libexec /usr/share

if [ -f bin/bin.aa ]; then
  echo -n "Extracting bin ... "
  cat bin/bin.*|gunzip|tar --unlink -xpf - -C / bin sbin usr var/yp dev lkm kernel.GENERIC
  cat bin/bin.*|gunzip|tar --unlink -xpf - -C /tmp etc
  /bin/cp /tmp/etc/mtree/* /etc/mtree
  if [ -f /usr/local/bin/top ]; then
    /bin/rm -f /usr/local/bin/top
  fi
  if [ -f sysutils/xsysinfo ]; then
    if [ -f /usr/X11R6/bin/xsysinfo ]; then
      install -c -s -m 2555 -o bin -g kmem sysutils/xsysinfo /usr/X11R6/bin/
    fi
  fi
  if [ -f sysutils/identd ]; then
    if [ -f /usr/local/sbin/identd ]; then
      install -c -s -m 2555 -o bin -g kmem sysutils/identd /usr/local/sbin/
    fi
  fi
  echo " done."
else
  echo "Error....dists not found."
  exit
fi

echo "Do you want to install dict (y/n) ?"
read ans
if [ ."$ans" = ."y" ]; then
if [ -f dict/dict.aa ]; then
  echo -n "Extracting dict ... "
  cat dict/dict.*|gunzip|tar --unlink -xpf - -C /
  echo " done."
fi
fi

echo "Do you want to install games (y/n) ?"
read ans
if [ ."$ans" = ."y" ]; then
if [ -f games/games.aa ]; then
  echo -n "Extracting games ... "
  cat games/games.*|gunzip|tar --unlink -xpf - -C /
  echo " done."
fi
fi

echo "Do you want to install info (y/n) ?"
read ans
if [ ."$ans" = ."y" ]; then
if [ -f info/info.aa ]; then
  echo -n "Extracting info ... "
  cat info/info.*|gunzip|tar --unlink -xpf - -C /
  echo " done."
fi
fi

echo "Do you want to install doc (y/n) ?"
read ans
if [ ."$ans" = ."y" ]; then
if [ -f doc/doc.aa ]; then
  echo -n "Extracting doc ... "
  cat doc/doc.*|gunzip|tar --unlink -xpf - -C /
  echo " done."
fi
fi

echo "Do you want to install manpages (y/n) ?"
read ans
if [ ."$ans" = ."y" ]; then
if [ -f manpages/manpages.aa ]; then
  echo -n "Extracting manpages ... "
  cat manpages/manpages.*|gunzip|tar --unlink -xpf - -C /
  echo " done."
fi
fi

echo "Do you want to install catpages (y/n) ?"
read ans
if [ ."$ans" = ."y" ]; then
if [ -f catpages/catpages.aa ]; then
  echo -n "Extracting catpages ... "
  cat catpages/catpages.*|gunzip|tar --unlink -xpf - -C /
  echo " done."
fi
fi

echo "Do you want to install proflibs (y/n) ?"
read ans
if [ ."$ans" = ."y" ]; then
if [ -f proflibs/proflibs.aa ]; then
  echo -n "Extracting proflibs ... "
  cat proflibs/proflibs.*|gunzip|tar --unlink -xpf - -C /
  echo " done."
fi
fi

echo "Do you want to install DES (y/n) ?"
read ans
if [ ."$ans" = ."y" ]; then
if [ -f des/des.aa ]; then
  echo -n "Extracting des ... "
  cat des/des.*|gunzip|tar --unlink -xpf - -C /
  echo " done."
fi
fi

if [ -f sysutils/telnet-8bit-2.2.tgz ]; then
echo "Do you want to install 8bit telnet/telnetd (y/n) ?"
read ans
if [ ."$ans" = ."y" ]; then 
  cat sysutils/telnet-8bit-2.2.tgz|gunzip|tar --unlink -xpf - -C /
fi
fi

echo "Do you want to install kernel source to /usr/src (y/n) ?"
read ans
if [ ."$ans" = ."y" ]; then
if [ -f src/ssys.aa ]; then
  echo -n "Extracting kernel src ... "
  mkdir -p /usr/src
  /bin/rm -rf /usr/src/sys
  cat src/ssys.*|gunzip|tar --unlink -xpf - -C /usr/src
  echo " done."
fi
fi

echo "fixup directory permissions ..."
if [ -f /etc/mtree/BSD.usr.dist ]; then
  mtree -deU -f /etc/mtree/BSD.usr.dist -p /usr
fi
if [ -f /etc/mtree/BSD.usr.dist ]; then
  mtree -deU -f /etc/mtree/BSD.var.dist -p /var
fi
if [ -f /etc/mtree/BSD.usr.dist ]; then
  mtree -deU -f /etc/mtree/BSD.include.dist -p /usr/include
fi

echo "Now you might have to upgrade your /etc yourself"
echo "New etc-files go to /tmp/etc, compare and upgrade with your /etc"
echo "You should at least upgrade rc*, sysconfig, services, inetd.conf"
echo "  netstart, daily, weekly, monthly...."
echo "Finally make a new kernel, Good Luck !"
echo "All Done !"
