#!/bin/sh

# Author: Rick Balocca
# Date: 2007
# Description:


install_virt_pkgs() {
  # if running kernel is a virt kernel; install virt packages
  # explicitly since they aren't listed in deb-versions.txt
  virt_packages=""
  uname -r | grep -q virt
  is_virt_kern=`echo $?`
  if [ "$is_virt_kern" = "0" ]; then
    virt_suffix='virt'

    virt_kern=`grep linux-image /opt/vyatta/etc/deb-versions.txt \
| awk '{print $1}'`

    ovm_mod=`grep linux-image /opt/vyatta/etc/deb-versions.txt \
| sed 's/linux\-image/open\-vm\-modules/' | awk '{print $1}'`

    virt_kern="$virt_kern-$virt_suffix"
    ovm_mod="$ovm_mod-$virt_suffix"
    virt_packages=$virt_kern" "$ovm_mod" "open-vm-tools
  fi
  echo "$virt_packages"
}

stars="*******************************************************************************"

df=noninteractive
export DEBIAN_FRONTEND="$df"

#
# On image booted machines, we need to mount /boot from the image-specific
# boot directory so that kernel package installation will put the
# files in the right place.  We also have to mount /boot/grub from the
# system-wide grub directory so that tools that edit the grub.cfg
# file will find it in the expected location.  This is done automatically at
# boot time in Larkspur and later.
#
if [ -e /live/image/boot ]; then
    image_name=`cat /proc/cmdline | sed -e s+^.*vyatta-union=/boot/++ | sed -e 's/ .*$//'`
    mounted=`mount | grep '^/live/image/boot'`
    if [ -n "$image_name" -a -z "$mounted" ]; then
        # Not much we can do if this fails, so we discard any
        # error messages so as not to distract the user
        mount --bind /live/image/boot/$image_name /boot > /dev/null 2>&1
        if [ ! -d /boot/grub ]; then
            mkdir /boot/grub
        fi
        mount --bind /live/image/boot/grub /boot/grub > /dev/null 2>&1
    fi
fi

# Find all vyatta packages to remove
cd /tmp
sed '3icase "$1" in upgrade) exit 0;; esac' -i /var/lib/dpkg/info/vyatta-quagga.prerm 2>/dev/null || true

echo -e "$stars\nVyatta full-upgrade status - Configuring unpacked packages"
dpkg --configure -a
echo -e "$stars\nVyatta full-upgrade status - Fixing any broken dependencies"
apt-get -f install
echo -e "$stars\nVyatta full-upgrade status - Removing no longer needed dependencies"
apt-get -f autoremove

install_list=`awk '{print $1}' /opt/vyatta/etc/deb-versions.txt|tr '\n' ' '`
if [ ! "$install_list" ]
then
	echo "Version package not installed correctly.  Exiting."
	exit 1
fi

virt_pkgs=$(install_virt_pkgs)
install_list=$install_list" "$virt_pkgs
echo -e "$stars\nVyatta full-upgrade status - Install retrieved packages"
apt-get -f install $install_list
echo -e "$stars\nVyatta full-upgrade status - Upgrade packages and handle dependencies"
apt-get dist-upgrade
echo -e "$stars\nVyatta full-upgrade status - Installed packages from new version"
echo -e "$stars\nVyatta full-upgrade status - Fixing any broken dependencies"
apt-get -f install
echo -e "$stars\nVyatta full-upgrade status - Removing no longer needed dependencies"
apt-get -f autoremove
echo -e "$stars\nVyatta full-upgrade status - Cleaning local repository of retrieved package files"
apt-get clean	# remove the downloads in case we are close to full on root

echo -e "$stars\nVyatta full-upgrade status - Updating grub config file"
/opt/vyatta/sbin/vyatta-update-grub.pl  || echo WARNING: grub config file not properly updated.
echo -e "$stars\nVyatta full-upgrade status - Completed grub setup"
