#!/bin/sh
#
# The updates2iso utility for creating apt-getable ISO image
# from ALTLinux updates or backports mirror
#
# Copyright (C) 2005-2006  Artem Zolochevskiy <az@zolochevskie.net>
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
#

# here we can set publusher for using in mkisofs
PUBLISHER=""

### ---------------------------------- ###
### --  don't edit below this line  -- ###
### ---------------------------------- ###

#---------------------------------------------------------------

# program name and version
PROG="$(basename $0)"
VERSION="0.2"

#---------------------------------------------------------------

# used functions

# usage message
usage()
{
	echo "usage: $PROG /path/to/mirror/ [/path/to/update_kernel_script]"
	echo "* /path/to/mirror/ is a path to directory containing i586 directory"
	echo "* [update_kernel_script] might be optionally added to ISO image"
	echo "  Some examples: http://wiki.sisyphus.ru/changes/kernel/"
	echo "Creates APT-GETable ISO image from local ALTLinux updates or backports mirror"
}


# check needed tools
prog_check()
{
	if [ "$(which $1 2> /dev/null)" = "" ]
	then
	        echo "$PROG: $1 not found. not installed?" >&2
        	exit 1
	fi
}

#---------------------------------------------------------------

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

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

#---------------------------------------------------------------

# check needed tools
# sh (bash)
# which (which)
# basesystem:   basename, test, cut, cat, tr, whoami, mkdir, rm (coreutils)
#               grep (grep)
#               sed (sed)
# mktemp (mktemp)
# mkisofs (mkisofs)

USED_COMMANDS="grep sed mktemp mkisofs"
for i in $USED_COMMANDS; do
	prog_check $i
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?"
	exit 1
fi

#---------------------------------------------------------------

# check path to update_kernel_script
if [ ! -f $2 ]
then
	echo "$PROG: no update_kernel_script found. wrong path to the script?"
	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 " " .)"
DESCRIPTION_PLUS_DATE="$DESCRIPTION ($DATE)"
ISO_IMAGE="$(echo "$DESCRIPTION-$DATE.iso" | tr [:upper:] [:lower:] | tr " " _)"

#---------------------------------------------------------------

# display what we are going to do
echo " >>>"
echo -n " >>> "
echo "creating \"$DESCRIPTION_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
$DESCRIPTION_PLUS_DATE
EOF

cat <<EOF >$TMP_DIR/README.txt
$DESCRIPTION_PLUS_DATE
To add this CDROM to APTs list of available sources:
apt-cdrom add

Install updates with:
apt-get upgrade
EOF

#---------------------------------------------------------------

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

#---------------------------------------------------------------

# function to 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 -n " >>> "
	echo "\"$DESCRIPTION_PLUS_DATE\" ISO built successful!"
	echo -n " >>> "
	echo "file: $ISO_IMAGE"
	echo " >>>"
else
	rm -r -f $TMP_DIR 2>/dev/null
	rm $ISO_IMAGE 2>/dev/null
	echo " >>>" >&2
	echo -n " >>> " >&2
	echo "$PROG: creating \"$DESCRIPTION_PLUS_DATE\" ISO failed :( !" >&2
	echo " >>>" >&2
	exit 1
fi

exit 0

