#!/bin/sh
#
# --------------------------------------------------------------------------
# Copyright notice
# --------------------------------------------------------------------------
# Copyright: Rene Mayrhofer, Mar. 2000
#
# This program 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, 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; see the file COPYING.  If not, write to
# the Free Software Foundation, 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.
# On Debian GNU/Linux systems, the complete text of the GNU General
# Public License can be found in `/usr/share/common-licenses/GPL'.
# --------------------------------------------------------------------------
#

VERSION_TEMPLATE="_VERSION_"

mntpoint=`mktemp -d`
copy_files_from=/usr/share/mkinitrd-cd

if [ $# -lt 4 ] || ( [ "$1" != "1440" ] && [ "$1" != "2880" ] ); then
  echo "Usage: $0 <image size> <linux kernel> <initrd image> <created image file> [<version>]"
  echo "where <image size> can be either 1440 or 2880 (in kB)"
  echo
  echo "If <version> is specified, it will replace any occurance of the string"
  echo "'$VERSION_TEMPLATE' in the installed boot loader message. Therefore it can be"
  echo "used to automatically modify any version string that will be shown in"
  echo "the syslinux boot loader message (template taken from"
  echo "/etc/mkinitrd-cd/bootmsg.txt)."
  exit 1
fi

size_kb=$1

if [ `id -u` -ne 0 ]; then
  echo "This script must be run as root."
  exit 1
fi

umask 077
set -e

echo -n "Creating image file with FAT file system ($size_kb kB)... "
dd if=/dev/zero of=$4 bs=1024 count=$size_kb >/dev/null 2>/dev/null
mkdosfs $4 >/dev/null 2>/dev/null
syslinux $4
echo "done"

echo -n "Mounting ... "
mount -o loop $4 $mntpoint
echo "done"
echo -n "Copying linux kernel ... "
cp $2 $mntpoint/linux
echo "done"
echo -n "Copying initrd image ... "
cp $3 $mntpoint/initrd.bin
echo "done"
echo -n "Copying configuration ... "
cp $copy_files_from/syslinux.cfg $mntpoint
if [ -z "$5" ]; then
  cp /etc/mkinitrd-cd/bootmsg.txt $mntpoint
else
  sed "s/${VERSION_TEMPLATE}/$5/" < /etc/mkinitrd-cd/bootmsg.txt > $mntpoint/bootmsg.txt
fi
echo "done"
umount $mntpoint
rm -rf $mntpoint
