#!/bin/sh

root=`dirname $0`
root=`dirname $root`
root=`readlink -f $root`

tmpdir=$(mktemp -d)
cd $tmpdir

wget http://ftp.whtech.com/System%20ROMs/MAME/old/ti99_complete.zip
wget http://ftp.whtech.com/emulators/mess/mess_modules.zip
wget http://ftp.whtech.com/Cartridges/MAME/old/rpk.old/mg_gramkracker.rpk

unzip -oq ti99_complete ti99_4a.zip
unzip -oq ti99_complete ti99_4qi.zip
unzip -oq ti99_complete ti99_fdc.zip
unzip -oq ti99_complete ti99_pcode.zip
unzip -oq ti99_complete ti99_speech.zip

cp $root/roms/cf7a+.bin .

unzip -oq ti99_4a.zip '*.bin'
unzip -oq ti99_4qi.zip '*.bin'
unzip -oq ti99_fdc.zip '*.bin'
unzip -oq ti99_pcode.zip '*.bin'
unzip -oq ti99_speech.zip '*.bin'
unzip -oq mg_gramkracker.rpk '*.bin'

$root/bin/convert-ctg $root/roms/cf7+.dat
$root/bin/convert-ctg $root/roms/gram-kracker.dat
$root/bin/convert-ctg $root/roms/ti-994a.dat
$root/bin/convert-ctg $root/roms/ti-994a-qi.dat
$root/bin/convert-ctg $root/roms/ti-disk.dat
$root/bin/convert-ctg $root/roms/ti-pcard.dat

mkdir -p ~/.ti99sim/console
mkdir -p ~/.ti99sim/cartridges
mkdir -p ~/.ti99sim/disks

if [ ! -f ~/.ti99sim/disks/image.cf7 ]; then
	tmpdisk=$(mktemp -uq)
	$root/bin/disk --create=sssd $tmpdisk > /dev/null
	$root/bin/disk --output=cf7+ --filename=image.cf7#32 $tmpdisk > /dev/null
	rm $tmpdisk
	mv image.cf7 ~/.ti99sim/disks
fi

mv *ctg ~/.ti99sim/console
mv spchrom.bin ~/.ti99sim/console

unzip -oq mess_modules -d roms
mv roms/minimem*.bin .

$root/bin/convert-ctg $root/roms/mini-memory.dat
$root/bin/mkcart roms

mv *ctg ~/.ti99sim/cartridges

cd -

rm -Rf $tmpdir
