#!/bin/bash

#   FILE: mount_images -- 
# AUTHOR: W. Michael Petullo <mike@flyn.org>
#   DATE: 25 November 2001
#
# Copyright (C) 2001 W. Michael Petullo <mike@flyn.org>
# All rights reserved.
#
# 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 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

IN=/tmp/CDrec.iso

USAGE="[OPTION]...

  -h, -?   print this message
  -i path  set the input path   [ $IN ]"

IMG_1=`mktemp /tmp/img.XXXXXX`
IMG_2=`mktemp /tmp/img.XXXXXX`
DIR_1=`mktemp -d /tmp/dir.XXXXXX`
DIR_2=`mktemp -d /tmp/dir.XXXXXX`
DIR_3=`mktemp -d /tmp/dir.XXXXXX`
DIR_4=`mktemp -d /tmp/dir.XXXXXX`

trap 'rm -f $IMG_1 $IMG_2' SIGHUP SIGINT SIGTERM EXIT

while :;
	do case "$1" in
		-h | "-?" )	
			echo -e usage: ${0##*/} "$USAGE" >&2
			exit 1 ;;
		-i )
			IN=$2
			shift ;;
		-?* )
			echo "${0##*/}: unrecognised option: $1" >&2
			exit 1 ;;
		* )
			break ;;
	esac
	shift
done

if [ ! -f $IN ]; then
        echo "${0##*/}: $IN does not exist" >&2
	exit 1
fi

echo mounting $IN at $DIR_1...
mount -o loop -t iso9660 $IN $DIR_1 || exit 1
echo mounting bootflop.img at $DIR_2...
mount -o loop -t msdos $DIR_1/bootflop.img $DIR_2 || exit 1
gunzip $DIR_2/initrd.gz -c > $IMG_1
echo mounting initrd at $DIR_3...
mount -o loop -t ext2 $IMG_1 $DIR_3 || exit 1
bunzip2 $DIR_1/rd-base.img.bz2 -c > $IMG_2
echo mounting rd-base.img at $DIR_4...
mount -o loop $IMG_2 $DIR_4
