#!/bin/sh
#

echo Creating ISO IMAGES ...

if [ -z "$MEDIA" ] || 
   [ -z "$RELEASE_ISOLINUX" ]; then
   echo "Missing configuration"
   exit 1 
fi

## Mount the MEDIA
vcmount $MEDIA $MEDIA_DEV || exit 1

## Copy isolinux
echo Copying $RELEASE_ISOLINUX
cp -au $RELEASE_ISOLINUX $MEDIA

## Copy the kernel
if [ "$KERNEL_IDE" ] && [ -f $KERNEL_IDE ]; then
    echo "Copying $KERNEL_IDE"
    cp -up $KERNEL_IDE $MEDIA_KERNEL/ide
fi

if [ ! -f $MEDIA_KERNEL/ide ]; then
    echo "ERROR, NO KERNEL IDE !"
    exit 1
fi

## Copy the kernel
if [ "$KERNEL_SCSI" ] && [ -f $KERNEL_SCSI ]; then
    echo "Copying $KERNEL_SCSI"
    cp -up $KERNEL_SCSI $MEDIA_KERNEL/scsi
fi

if [ ! -f $MEDIA/isolinux/kernel/scsi ]; then
    echo "ERROR, NO KERNEL SCSI !"
    exit 1
fi

## Copy other directory from RELEASE
for RR in $RELEASE_INCLUDED; do
    if [ -d $RELEASE/$RR ]; then
	echo Copying $RELEASE/$RR
	cp -au $RELEASE/$RR $MEDIA
    else
	echo $RELEASE/$RR not found
	exit 1
    fi
done

## Copy files from FLAVOUR
for RR in $FLAVOUR_INCLUDED; do
    if [ -f $RELEASE_ISO/$RR ]; then
	echo Copying $RELEASE_ISO/$RR
	cp -au $RELEASE_ISO/$RR $MEDIA
    else
	echo $RELEASE_ISO/$RR not found
	exit 1
    fi
done

mkdir -p $RELEASE_ISO
ISO_TARGET=$RELEASE_ISO/$ISO_FILE
(
# clean up
cd $MEDIA/veclinux
rm -f *.bak

# make pkglist
cd $MEDIA/packages
pkglist > PKGLIST.TXT

cd $MEDIA
echo Making $ISO_FILE from `pwd` 
sleep 3

mkisofs -o $ISO_TARGET -r -R -J -V "$ISO_VOLUME" -v -d -N -no-emul-boot \
-boot-load-size 4 -boot-info-table \
-b isolinux/isolinux.bin -c isolinux/isolinux.boot -A "$ISO_APPID"  .
)

if [ $? = 0 ]; then
    md5sum $ISO_TARGET > $ISO_TARGET.md5
    echo DONE ......
    echo The ISO image is ready at $RELEASE_ISO/$ISO_FILE
else
    echo ERROR: Cannot make ISO image
    exit 1
fi
