#!/bin/sh -
#                               -*- Mode: Sh -*-
# example.posthook.sh ---
# Author           : Manoj Srivastava ( srivasta@glaurung.green-gryphon.com )
# Created On       : Mon Aug 11 14:00:58 2003
# Created On Node  : glaurung.green-gryphon.com
# Last Modified By : Manoj Srivastava
# Last Modified On : Sun Apr 12 17:16:52 2009
# Last Machine Used: anzu.internal.golden-gryphon.com
# Update Count     : 10
# Status           : Unknown, Use with caution!
# HISTORY          :
# Description      :
#
#
# This is an example of a script that can be run as a postinst hook,
# and manages the symbolic links in a manner similar to the kernel
# image default behaviour, except that the latest two versions (as
# determined by ls -lct) are kept. You can modify this script
#
# Copyright 2003, 2004, 2005, 2006, 2007, 2008, 2009 Manoj Srivastava
# Copyright 2009 Darren Salt

set -e

# The dir where symlinks are managed
SYMLINKDIR=/

if [ $# -ne 2 ]; then
    echo Usage: $0 version location
    exit 2
fi

version="$1"
vmlinuz_location="$2"
vmlinuz_dir="$(dirname "$2")"

cd $SYMLINKDIR || exit 1

if [ -n "$DEB_MAINT_PARAMS" ]; then
    eval set -- "$DEB_MAINT_PARAMS"
    if [ -z "$1" ] || [ "$1" != "configure" -a "$1" != "remove" ]; then
        exit 0;
    fi
fi


# Create a temporary file safely
if [ -x /bin/tempfile ]; then
    outfile=$(tempfile -p outp -m 0600);
else
    set -e
    mkdir /tmp/kernel-image-$version-$$
    outfile=/tmp/kernel-image-$version-$$/output
fi

(cd "$vmlinuz_dir" && ls -ct vmlinuz-*) > $outfile

STD="$(head -n 1 $outfile |             sed 's/vmlinuz-//')"
OLD="$(head -n 2 $outfile | tail -n 1 | sed 's/vmlinuz-//')"

if [ "X$STD" = "X" ]; then
    exit 0;
fi

# If you want version-specific links, here's how to start
#STD30="$(grep vmlinuz-3.0 $outfile | head -n 1 | sed 's/vmlinuz-//')" || true
#OLD30="$(grep vmlinuz-3.0 $outfile | head -n 1 | tail -n 1 | sed 's/vmlinuz-//')" || true

#STD31="$(grep vmlinuz-3.1 $outfile | head -n 1 | sed 's/vmlinuz-//')" || true
#OLD31="$(grep vmlinuz-3.1 $outfile | head -n 1 | tail -n 1 | sed 's/vmlinuz-//')" || true

echo Booting $STD, old is $OLD

if [ -f "$vmlinuz_dir/"initrd.img-$STD ] ; then
   rm -f  initrd.img vmlinuz-rd
   ln -s "$vmlinuz_dir/"initrd.img-$STD initrd.img
   ln -s "$vmlinuz_dir/"vmlinuz-$STD vmlinuz-rd
else
   rm -f vmlinuz
   ln -s "$vmlinuz_dir/"vmlinuz-$STD vmlinuz
fi

# Don't create old link if STD and OLD are the same
if [ "X$OLD" != "X" -a "$STD" != "$OLD" ]; then
    if [ -f "$vmlinuz_dir/"initrd.img-$OLD ] ; then
        rm -f vmlinuz-rd.old initrd.img.old
	ln -s "$vmlinuz_dir/"initrd.img-$OLD initrd.img.old
	ln -s "$vmlinuz_dir/"vmlinuz-$OLD vmlinuz-rd.old
    else
        rm -f vmlinuz.old
	ln -s "$vmlinuz_dir/"vmlinuz-$OLD vmlinuz.old
    fi
fi

# if [ "X$STD30" != "X" ]; then
#     if [ -f "$vmlinuz_dir/"initrd.img-$STD30 ] ; then
# 	ln -s "$vmlinuz_dir/"initrd.img-$STD30 initrd30.img
# 	ln -s "$vmlinuz_dir/"vmlinuz-$STD30 vmlinuz30-rd
#     else
# 	ln -s "$vmlinuz_dir/"vmlinuz-$STD30 vmlinuz30
#     fi
# fi
# if [ "X$OLD30" != "X" ]; then
#     if [ -f "$vmlinuz_dir/"initrd.img-$OLD30 ] ; then
# 	ln -s "$vmlinuz_dir/"initrd.img-$OLD30 initrd30.img.old
# 	ln -s "$vmlinuz_dir/"vmlinuz-$OLD vmlinuz30-rd.old
#     else
# 	ln -s "$vmlinuz_dir/"vmlinuz-$OLD vmlinuz30.old
#     fi
# fi

# FIXNE -- is lilo the correct boot loader?
# lilo

rm -f $outfile
if [ -d /tmp/kernel-image-$version-$$ ]; then
    rmdir /tmp/kernel-image-$version-$$
fi

exit 0
