#!/bin/sh
#
# "cdromupgrade" is a shell script wrapper around the dist-upgrader
# to make it possible to put it onto the top-level dir of a CD and
# run it from there
# 
# Not that useful unfortunately when the CD is mounted "noexec".
#

# the codename is AUTO-GENERATED from the build-host relase codename
CODENAME=focal
UPGRADER_DIR=dists/$CODENAME/main/dist-upgrader/binary-all/

# this script can be called in three ways:
#   sh /cdrom/cdromugprade
#   sh ./cdromupgrade
#   sh cdromupgrade
# the sh below is needed to figure out the cdromdirname
case "$0" in
/*)  cddirname="${0%\/*}" ;;
*/*) cddirname="$(pwd)/${0%\/*}" ;;
*)   cddirname="$(pwd)/" ;;
esac

fullpath="$cddirname/$UPGRADER_DIR"

# extract the tar to a TMPDIR and run it from there
if [ ! -f "$fullpath/$CODENAME.tar.gz" ]; then
    echo "Could not find the upgrade application archive, exiting"
    exit 1
fi

TMPDIR=$(mktemp -d)
cd $TMPDIR
tar xzf "$fullpath/$CODENAME.tar.gz"
if [ ! -x $TMPDIR/$CODENAME ]; then
    echo "Could not find the upgrade application in the archive, exiting"
    exit 1
fi
$TMPDIR/$CODENAME --cdrom "$cddirname" $@
