#!/bin/sh
set -e

if [ $# != 1 ]; then
	echo "Usage: $0 size" 1>&2
	exit 1
fi

size="$1"
mdir=./tmpmnt
img=img${size}Atari

# make zeroed file of appropriate size
dd if=/dev/zero of=$img bs=1k count=$size 2>&1 | grep -v records || true

# make a MSDOS fs on it
mkdosfs -r 112 -F 12 $img $size >/dev/null

# make image file accessible as loop device
losetup -d /dev/loop6 >/dev/null 2>&1 || true # fails if setup didn't exist
losetup /dev/loop6 $img

# make mountpoint
[ -d $mdir ] || mkdir $mdir

# mount it, FAT size on disks is always 12, which is not autodetected!
mount -tmsdos -ofat=12 /dev/loop6 $mdir

# make auto folder for autostart
mkdir $mdir/auto

# copy ataboot binary to it; extension must be .prg for autostart
cp ataboot $mdir/auto/bootstra.prg

# make a default bootargs
echo "-s root=/dev/unknown" >$mdir/bootargs

# umount it again
umount $mdir
# and remove the mountpoint dir
rmdir $mdir
# and remove the loop setup
losetup -d /dev/loop6

exit 0
