#!/bin/sh
## Create apt-getable ISO image
## from ALT Linux updates/backports mirror
## Artem Zolochevskiy <az@zolochevskie.net>, 2006

PUBLISHER=""

### ---------------------------------- ###
### --  don't edit below this line  -- ###
### ---------------------------------- ###
PROG="$(basename $0)"
VERSION="0.1"

usage() {
	echo "usage: $PROG /path/to/mirror/ [/path/to/update_kernel_script]"
}

# check arguments
if [ $# = 0 ]
then
	usage
	exit 1
fi

for arg in $*; do
    case $arg in
        --version|-V|-v)
	    echo "$PROG $VERSION"
	    exit 0
	    ;;
	--help|-h)
            usage
	    exit 0
	    ;;
	-*)
	    echo "$PROG: $arg: unknown option" >&2
	    exit 1
    esac
done


# check path to updates mirror
if [ ! -f $1/i586/base/release ]
then
	echo "$PROG: no release file found. wrong path to updates/backports mirror?" >&2
	exit 1
fi

# check path to update_kernel_script
if [ ! -f $2 ]
then
	echo "$PROG: no update_kernel_script found. wrong path?" >&2
	exit 1
fi

# updates or backports
if grep -s -q  "^Description: Not Available" $1/i586/base/release
then
	DESCRIPTION="ALT Linux $(basename $1) backports"
else
	DESCRIPTION="$(grep ^Description: $1/i586/base/release | sed 's/^Description: //')"
fi

# some vars
DATE="$(grep ^Date: $1/i586/base/release | cut -d" " -f3,4,5 | tr " " .)"
DESCRIPRION_PLUS_DATE="$DESCRIPTION ($DATE)"
ISO_IMAGE="$(echo "$DESCRIPTION-$DATE.iso" | tr [:upper:] [:lower:] | tr " " _)"


# display what we are going to do
echo " >>>"
echo " >>> creating \"$DESCRIPRION_PLUS_DATE\" iso"
echo " >>>"

# create temporary .disk/info and README.txt files
TMP_DIR="$(mktemp -d)"
mkdir $TMP_DIR/.disk/

cat <<EOF >$TMP_DIR/.disk/info
$DESCRIPRION_PLUS_DATE
EOF

cat <<EOF >$TMP_DIR/README.txt
$DESCRIPRION_PLUS_DATE
To add this disk do as root:
apt-cdrom add
EOF

# set publisher and preparer for mkisofs
if [ -z "$PUBLISHER" ]
then
	PUBLISHER="$(whoami)"
fi
PREPARER="$PUBLISHER"

# create iso image
makeiso() {
	mkisofs -J -r -volid "$DESCRIPTION" \
	--publisher $PUBLISHER \
	--preparer $PREPARER \
	--iso-level=4 --joliet-long \
	-o $ISO_IMAGE \
	$TMP_DIR \
	$1 \
	$2
}

# display result
if makeiso $1 $2
then
	rm -r -f $TMP_DIR
	echo " >>>"
	echo " >>> \"$DESCRIPRION_PLUS_DATE\" ISO gotov!"
	echo " >>> file: $ISO_IMAGE"
	echo " >>>"
else
	rm -r -f $TMP_DIR
	echo "$PROG: creating \"$DESCRIPRION_PLUS_DATE\" ISO failed!"
	exit 1
fi

exit 0
