2017-07-17 04:56:17 +00:00
#!/bin/sh
2017-08-31 03:35:49 +00:00
if [ $# -lt 3 ]
2017-07-17 04:56:17 +00:00
then
2021-04-13 04:04:59 +00:00
echo USAGE: $0 diskimage bootimage file [bootdest]
2017-07-17 04:56:17 +00:00
exit 1
fi
DISKIMAGE="$1"
2017-08-31 03:35:49 +00:00
shift
2021-04-13 04:04:59 +00:00
DESTBOOTIMAGE="$1"
2017-08-31 03:35:49 +00:00
shift
2021-04-13 04:04:59 +00:00
FILE="$1"
2017-08-31 03:35:49 +00:00
shift
2021-04-13 04:04:59 +00:00
BOOTCOPYPATH="$1"
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"
2017-07-17 04:56:17 +00:00
2020-11-30 05:31:21 +00:00
# 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
2021-05-07 03:12:03 +00:00
printErrorAndExit()
2017-07-17 04:56:17 +00:00
{
2021-05-07 03:12:03 +00:00
echo "`pwd`/Makefile:0:0: error: $*" >&2
2017-07-17 04:56:17 +00:00
umount "$MOUNTDIR" 2> /dev/null
rm -f "$TMPDISKIMAGE" 2> /dev/null
2021-04-13 04:04:59 +00:00
rm -f "$TMPBOOTIMAGE" 2> /dev/null
rm -f "$TMPARCHIVE" 2> /dev/null
2017-07-17 04:56:17 +00:00
rm -f "$DISKIMAGE" 2> /dev/null
2021-04-13 04:04:59 +00:00
rm -f "$DESTBOOTIMAGE" 2> /dev/null
rm -rf "$TMPDIR" 2> /dev/null
2017-07-17 04:56:17 +00:00
exit 1
}
2021-05-07 03:12:03 +00:00
unmount()
{
RETRIES=0
while [ $RETRIES -lt 5 ]
do
umount "$1"
if [ $? -eq 0 ]
then
break
fi
RETRIES=`expr $RETRIES + 1`
sleep 1
done
if [ $RETRIES -ge 5 ]
then
printErrorAndExit "Unable to unmount the disk image."
fi
}
validateProDOSName()
{
NAME=`basename $1`
echo $NAME | egrep '^[a-zA-Z][a-zA-Z0-9.]{0,14}$' > /dev/null
}
mkdirProDOS()
{
validateProDOSName "$1"
if [ $? -ne 0 ]
then
printErrorAndExit "Invalid ProDOS name of directory `basename $1`. ProDOS names must be 1 to 15 characters, start with a letter and only letters, numbers and a period can be used in the name."
fi
mkdir -p "$1"
if [ $? -ne 0 ]
then
printErrorAndExit "Unable to create directory $1"
fi
}
cpProDOS()
{
validateProDOSName "$2"
if [ $? -ne 0 ]
then
printErrorAndExit "Invalid ProDOS name of file `basename $2`. ProDOS names must be 1 to 15 characters, start with a letter and only letters, numbers and a period can be used in the name."
fi
cp $CPARGS "$1" "$2"
if [ $? -ne 0 ]
then
printErrorAndExit "Unable to create directory $1"
fi
}
copyDirs()
{
OLDDIR=`pwd`
for COPYDIR in $*
do
cd "$COPYDIR"
if [ $? != 0 ]
then
printErrorAndExit "Unable to find $COPYDIR"
fi
find . -print | while read FILEORDIR
do
2021-07-05 21:37:31 +00:00
if [ "$FILEORDIR" = "." ]
then
continue
fi
2021-05-07 03:12:03 +00:00
if [ -d "$FILEORDIR" ]
then
mkdirProDOS "${MOUNTDIR}/$FILEORDIR"
elif [ -f "$FILEORDIR" ]
then
cpProDOS "$FILEORDIR" "${MOUNTDIR}/$FILEORDIR"
fi
done
cd "$OLDDIR"
done
}
2017-07-17 04:56:17 +00:00
if [ ! -f "$TEMPLATEDISKIMAGE" ]
then
2021-05-07 03:12:03 +00:00
printErrorAndExit "Unable to find the template disk image, $TEMPLATEDISKIMAGE"
2017-07-17 04:56:17 +00:00
fi
2021-04-13 04:04:59 +00:00
if [ ! -f "$TEMPLATEBOOTIMAGE" ]
then
2021-05-07 03:12:03 +00:00
printErrorAndExit "Unable to find the template boot image, $TEMPLATEBOOTIMAGE"
2021-04-13 04:04:59 +00:00
fi
mkdir "$TMPDIR"
2017-07-17 04:56:17 +00:00
if [ $? != 0 ]
then
2021-05-07 03:12:03 +00:00
printErrorAndExit "Unable to create the mount directory."
2017-07-17 04:56:17 +00:00
fi
mkdir "$MOUNTDIR"
if [ $? != 0 ]
then
2021-05-07 03:12:03 +00:00
printErrorAndExit "Unable to create the mount directory."
2017-07-17 04:56:17 +00:00
fi
2021-04-13 04:04:59 +00:00
cp "$TEMPLATEBOOTIMAGE" "$TMPBOOTIMAGE"
if [ $? != 0 ]
then
2021-05-07 03:12:03 +00:00
printErrorAndExit "Unable to copy template boot image."
2021-04-13 04:04:59 +00:00
fi
if [ ! -z "$COPYBOOTDIRS" ] || [ ! -z "BOOTCOPYPATH" ]
then
profuse -orw "$TMPBOOTIMAGE" "$MOUNTDIR"
if [ $? != 0 ]
then
2021-05-07 03:12:03 +00:00
printErrorAndExit "Unable to mount the boot image."
2021-04-13 04:04:59 +00:00
fi
if [ ! -z "$BOOTCOPYPATH" ]
then
2021-05-07 03:12:03 +00:00
cpProDOS "$FILE" "$MOUNTDIR/$BOOTCOPYPATH"
2021-04-13 04:04:59 +00:00
if [ $? != 0 ]
then
2021-05-07 03:12:03 +00:00
printErrorAndExit "Unable to copy the file to the boot image."
2021-04-13 04:04:59 +00:00
fi
fi
2021-05-07 03:12:03 +00:00
copyDirs $COPYBOOTDIRS
2021-04-13 04:04:59 +00:00
2021-05-07 03:12:03 +00:00
unmount "$MOUNTDIR"
2021-04-13 04:04:59 +00:00
fi
cp "$TEMPLATEDISKIMAGE" "$TMPDISKIMAGE"
if [ $? != 0 ]
then
2021-05-07 03:12:03 +00:00
printErrorAndExit "Unable to copy template disk image."
2021-04-13 04:04:59 +00:00
fi
2017-07-17 04:56:17 +00:00
profuse -orw "$TMPDISKIMAGE" "$MOUNTDIR"
if [ $? != 0 ]
then
2021-05-07 03:12:03 +00:00
printErrorAndExit "Unable to mount the disk image."
2017-07-17 04:56:17 +00:00
fi
2021-05-07 03:12:03 +00:00
cpProDOS "$FILE" "$MOUNTDIR"
2017-07-17 04:56:17 +00:00
if [ $? != 0 ]
then
2021-05-07 03:12:03 +00:00
printErrorAndExit "Unable to copy the file to the disk image."
2017-07-17 04:56:17 +00:00
fi
2021-05-07 03:12:03 +00:00
copyDirs $COPYDIRS
2017-08-31 03:35:49 +00:00
2021-05-07 03:12:03 +00:00
OLDDIR=`pwd`
2021-04-13 04:04:59 +00:00
cd "$TMPDIR"
$ORCA "$OLDDIR/make/tar" cf "$TMPARCHIVE" "$PROGRAM"
if [ $? != 0 ]
then
2021-05-07 03:12:03 +00:00
printErrorAndExit "Unable to create archive."
2021-04-13 04:04:59 +00:00
fi
cd "$OLDDIR"
2021-05-07 03:12:03 +00:00
unmount "$MOUNTDIR"
2017-07-17 04:56:17 +00:00
cp "$TMPDISKIMAGE" "$DISKIMAGE"
if [ $? != 0 ]
then
2021-05-07 03:12:03 +00:00
printErrorAndExit "Unable to copy the disk image to the destination."
2017-07-17 04:56:17 +00:00
fi
2021-04-13 04:04:59 +00:00
cp "$TMPBOOTIMAGE" "$DESTBOOTIMAGE"
if [ $? != 0 ]
then
2021-05-07 03:12:03 +00:00
printErrorAndExit "Unable to copy the boot image to the destination."
2021-04-13 04:04:59 +00:00
fi
cp "$TMPARCHIVE" "$ARCHIVE"
if [ $? != 0 ]
then
2021-05-07 03:12:03 +00:00
printErrorAndExit "Unable to copy the archive to the destination."
2021-04-13 04:04:59 +00:00
fi
2017-07-17 04:56:17 +00:00
rm -f "$TMPDISKIMAGE"
2021-04-13 04:04:59 +00:00
rm -f "$TMPBOOTIMAGE"
rm -f "$TMPARCHIVE"
rm -rf "$TMPDIR"
2017-07-17 04:56:17 +00:00
exit 0