#!/bin/sh

if [ $# -lt 3 ]
then
    echo USAGE: $0 diskimage bootimage file [bootdest]
    exit 1
fi

DISKIMAGE="$1"
shift

DESTBOOTIMAGE="$1"
shift

FILE="$1"
shift

BOOTCOPYPATH=""

PROGRAM=`basename "$FILE"`
TMPDIR=/tmp/a2gs_mount.$$
MOUNTDIR="${TMPDIR}/$PROGRAM"
TMPDISKIMAGE=/tmp/a2gs_diskimage_$$.2mg
TMPBOOTIMAGE=/tmp/a2gs_bootimage_$$.2mg
TMPARCHIVE=/tmp/s2gs_archive_$$.shk
TEMPLATEDISKIMAGE="make/empty.2mg"
TEMPLATEBOOTIMAGE="make/$BOOTIMAGE"
ARCHIVE=`dirname "$DISKIMAGE"`/"${PROGRAM}.shk"

# It looks like on Linux, the cp command needs a special argument to preserve the resource fork.  This isn't ideal
# but for now, if uname is Darwin, then this is MacOS and we don't need any cp args.  If not Darwin, then assume
# this is Linux and ask for extended attributes to be preserved through the copy.
#
# Ultimately, it could be that other platforms (BSD, does Solaris still exist?) or even other Linux versions or
# distributions need different arguments for this rather special thing.  If true, this may need to be a build time
# option which can be set.
if [ "`uname`" = Darwin ]
then
    CPARGS=""
else
    CPARGS="--preserve=xattr"
fi

unmount()
{
    RETRIES=0
    while [ $RETRIES -lt 5 ]
    do
        umount "$1"
        if [ $? -eq 0 ]
        then
            break
        fi

        RETRIES=`expr $RETRIES + 1`
        sleep 1
    done
}

cleanupAndExit()
{
    unmount "$MOUNTDIR" 2> /dev/null
    rm -f "$TMPDISKIMAGE" 2> /dev/null
    rm -f "$TMPBOOTIMAGE" 2> /dev/null
    rm -f "$TMPARCHIVE" 2> /dev/null
    rm -f "$DISKIMAGE" 2> /dev/null
    rm -f "$DESTBOOTIMAGE" 2> /dev/null
    rm -rf "$TMPDIR" 2> /dev/null
    exit 1
}

if [ ! -f "$TEMPLATEDISKIMAGE" ]
then
    echo Unable to find the template disk image, $TEMPLATEDISKIMAGE
    cleanupAndExit
fi

if [ ! -f "$TEMPLATEBOOTIMAGE" ]
then
    echo Unable to find the template boot image, $TEMPLATEBOOTIMAGE
    cleanupAndExit
fi

mkdir "$TMPDIR"
if [ $? != 0 ]
then
    echo Unable to create the mount directory.
    cleanupAndExit
fi

mkdir "$MOUNTDIR"
if [ $? != 0 ]
then
    echo Unable to create the mount directory.
    cleanupAndExit
fi

cp "$TEMPLATEBOOTIMAGE" "$TMPBOOTIMAGE"
if [ $? != 0 ]
then
    echo Unable to copy template boot image.
    cleanupAndExit
fi
if [ ! -z "$COPYBOOTDIRS" ] || [ ! -z "BOOTCOPYPATH" ]
then
    /usr/local/bin/profuse -orw "$TMPBOOTIMAGE" "$MOUNTDIR"
    if [ $? != 0 ]
    then
        echo Unable to mount the boot image.
        cleanupAndExit
    fi

    if [ ! -z "$BOOTCOPYPATH" ]
    then
        cp $CPARGS "$FILE" "$MOUNTDIR/$BOOTCOPYPATH"
        if [ $? != 0 ]
        then
            echo Unable to copy the file to the boot image.
            cleanupAndExit
        fi
    fi

    OLDDIR=`pwd`
    for COPYDIR in $COPYBOOTDIRS
    do
        cd "$COPYDIR"
        if [ $? != 0 ]
        then
            echo Unable to find $COPYDIR
            cleanupAndExit
        fi

        find . -print | while read FILEORDIR
        do
            if [ -d "$FILEORDIR" ]
            then
                mkdir -p "${MOUNTDIR}/$FILEORDIR"
            elif [ -f "$FILEORDIR" ]
            then
                cp $CPARGS "$FILEORDIR" "${MOUNTDIR}/$FILEORDIR"
            fi
        done
        cd "$OLDDIR"
    done

    unmount "$MOUNTDIR"

    if [ $RETRIES -ge 5 ]
    then
        echo Unable to unmount the boot image.
        cleanupAndExit
    fi
fi

cp "$TEMPLATEDISKIMAGE" "$TMPDISKIMAGE"
if [ $? != 0 ]
then
    echo Unable to copy template disk image.
    cleanupAndExit
fi

/usr/local/bin/profuse -orw "$TMPDISKIMAGE" "$MOUNTDIR"
if [ $? != 0 ]
then
    echo Unable to mount the disk image.
    cleanupAndExit
fi

cp $CPARGS "$FILE" "$MOUNTDIR"
if [ $? != 0 ]
then
    echo Unable to copy the file to the disk image.
    cleanupAndExit
fi

if [ ! -z "$1" ]
then
    cp $CPARGS "$@" "$MOUNTDIR"
    if [ $? != 0 ]
    then
        echo Unable to copy extra files to the disk image.
        cleanupAndExit
    fi
fi

OLDDIR=`pwd`
for COPYDIR in $COPYDIRS
do
    cd "$COPYDIR"
    if [ $? != 0 ]
    then
        echo Unable to find $COPYDIR
        cleanupAndExit
    fi

    find . -print | while read FILEORDIR
    do
        if [ -d "$FILEORDIR" ]
        then
            mkdir -p "${MOUNTDIR}/$FILEORDIR"
        elif [ -f "$FILEORDIR" ]
        then
            cp $CPARGS "$FILEORDIR" "${MOUNTDIR}/$FILEORDIR"
        fi
    done
    cd "$OLDDIR"
done

cd "$TMPDIR"
$ORCA "$OLDDIR/make/tar" cf "$TMPARCHIVE" "$PROGRAM"
if [ $? != 0 ]
then
    echo Unable to create archive.
    cleanupAndExit
fi
cd "$OLDDIR"

unmount "$MOUNTDIR"

if [ $RETRIES -ge 5 ]
then
    echo Unable to unmount the disk image.
    cleanupAndExit
fi

cp "$TMPDISKIMAGE" "$DISKIMAGE"
if [ $? != 0 ]
then
    echo Unable to copy the disk image to the destination.
    cleanupAndExit
fi

cp "$TMPBOOTIMAGE" "$DESTBOOTIMAGE"
if [ $? != 0 ]
then
    echo Unable to copy the boot image to the destination.
    cleanupAndExit
fi

cp "$TMPARCHIVE" "$ARCHIVE"
if [ $? != 0 ]
then
    echo Unable to copy the archive to the destination.
    cleanupAndExit
fi

rm -f "$TMPDISKIMAGE"
rm -f "$TMPBOOTIMAGE"
rm -f "$TMPARCHIVE"
rm -rf "$TMPDIR"
exit 0
