#!/bin/bash -e

# Part 2 of debathena-livecd-convert, run inside the chroot
#bash

function cleanup {
    #bash
    umount -l /proc
    umount -l /sys
    umount -l /lib/modules/*/volatile || :
}
trap cleanup EXIT SIGINT SIGTERM

EXTRA_PACKAGES='
    lynx
    build-essential
    enscript
    psutils
    vgrind
    emacs emacs-intl-fonts
    athena-jot
    hesiod
    ispell iamerican
    aspell aspell-en
    pidgin
    pidgin-encryption
    libsasl2-modules-gssapi-mit
    texlive-base ~ntexlive.*recommended$
    gv
    krb5-clients'

PURGE='
    ^diveintopython$
    ~nbogofilter
    ~ngnome-games
    ^ekiga$
    ^tomboy$
    ^f-spot$
    ~nopenoffice.org
    ~ngimp-help
    ^thunderbird-locale-en-gb$
    ^python-uno$'

PURGE_MINUS=""
for p in $PURGE ; do
    PURGE_MINUS="$PURGE_MINUS ${p}~i_"
done

# Mount special filesystems
mount -t proc none /proc
mount -t sysfs none /sys

# Programs like these to be set
export HOME=/root
export LC_ALL=C

# Mod sources.list
echo "Modifying sources.list."
sed -ri 's/^\#\s*(deb.+universe)$/\1/' /etc/apt/sources.list
# Add updates
if [[ $(lsb_release -sc) = 'hardy' ]] ; then
    cat >> /etc/apt/sources.list <<EOF

## Major bug fix updates produced after the final release of the
## distribution.
deb http://us.archive.ubuntu.com/ubuntu/ hardy-updates main restricted
deb-src http://us.archive.ubuntu.com/ubuntu/ hardy-updates main restricted
EOF
fi
# Add Debathena
cat > /etc/apt/sources.list.d/debathena.list <<EOF
# Debathena
deb http://debathena.mit.edu/apt `lsb_release -sc` debathena debathena-config debathena-system openafs
deb-src http://debathena.mit.edu/apt `lsb_release -sc` debathena debathena-config debathena-system openafs
EOF

# Get public key
apt-key add /tmp/debathena-archive-keyring.gpg

# Hack because polkit-auth doesn't work in the chroot
dpkg-divert --divert /usr/bin/polkit-auth.real --rename /usr/bin/polkit-auth
echo "#!/bin/sh" > /usr/bin/polkit-auth
chmod a+rx /usr/bin/polkit-auth

export DEBIAN_FRONTEND=noninteractive

# Up{date,grade}
echo "Updating..."
aptitude update

# Install some stuff
echo "Installing Debathena and removing unnecessary packages to free space..."

aptitude install -Ry debathena-workstation \
    openafs-modules-generic \
    ubuntu-desktop \
    linux-generic \
    $PURGE_MINUS \
    $EXTRA_PACKAGES

echo "Upgrading stuff..."
aptitude -y full-upgrade

rm /usr/bin/polkit-auth
dpkg-divert --rename --remove /usr/bin/polkit-auth

# Mod openafs to use memcache
cat >>/etc/openafs/afs.conf <<EOF
# If our filesystem's not ext2/ext3, use memcache
if [ "\`stat -f -c %t /var/cache/openafs\`" != ef53 ] ; then
    OPTIONS="\$OPTIONS -memcache"
fi
EOF
# And make it not so big
sed -ri 's/^\/afs\:([^:]+)\:.*$/\/afs:\1:25000/' /etc/openafs/cacheinfo

# Change the default firefox homepage
cat >>/etc/firefox-3.0/pref/firefox.js <<EOF

// Override default homepage to something nonbroken
pref("browser.startup.homepage", "http://debathena.mit.edu/");
EOF

# Set timezone
rm /etc/localtime
cp -f /usr/share/zoneinfo/America/New_York /etc/localtime
echo "America/New_York" > /etc/timezone

aptitude clean

# From here, it exits the chroot and goes back to the debathena-livecd-convert script
