#!/bin/bash
MAMEBIN=mame.exe
SPEECH=speechadapter


# ----- Configuration ----------------

# Pick one of the languages in the "language" folder, e.g. German, French, Italian,...
LANGUAGE=English

# Window size
RESOLUTION=800x600

# Set to 0 for fullscreen
WINDOW=1

# Where the system ROMs go to
SYSROMS="roms"

# Where the cartridge ROMs go to (ZIP cartridges)
CARTROMS="carts"

# ------------------------------------

ROMPATH="$SYSROMS"

if [ "$SYSROMS" != "$CARTROMS" ]
then
   ROMPATH="$ROMPATH;$CARTROMS"
fi

GENPARAMS="-rompath $ROMPATH"
if [ ! -z "$LANGUAGE" ]
then
   GENPARAMS="$GENPARAMS -language $LANGUAGE"
fi

if [ a$WINDOW = "a1" ]
then
   GENPARAMS="$GENPARAMS -window"
   if [ ! -z "$RESOLUTION" ]
   then
      GENPARAMS="$GENPARAMS -resolution $RESOLUTION"
    fi
else
   GENPARAMS="$GENPARAMS -nowindow"
fi

if [ ! -f $MAMEBIN ]
then
   echo
   echo Error: No MAME executable found. Please go to the MAME installation folder and run this script from there.
   echo
   exit
fi

DESKTOP=`cygpath -D`
MAMEDIR=`cygpath -w $PWD`
DRIVE=`echo $MAMEDIR | sed "s/\:.*$/:/"`
MAMED=`echo $MAMEDIR | sed "s/^.*\://"`

which wget > /dev/null 2>&1

if [ $? -eq 1 ]
then 
    echo "Error: Please install 'wget' into MSYS2 (using the pacman command)."
    exit
fi

which unzip > /dev/null 2>&1
if [ $? -eq 1 ]
then 
    echo "Error: Please install 'unzip' into MSYS2 (using the pacman command)."
    exit
fi

echo
echo "Installing system ROMs"
test -d "$SYSROMS" || mkdir "$SYSROMS"
wget -q https://ftp.whtech.com/System%20ROMs/MAME/ti99_complete.zip 
cd "$SYSROMS"
unzip -q ../ti99_complete.zip
cd ..
rm ti99_complete.zip

echo "Installing cartridges"
test -d "$CARTROMS" || mkdir "$CARTROMS"
wget -q https://ftp.whtech.com/Cartridges/MAME/all_carts.zip 
cd "$CARTROMS"
unzip -q ../all_carts.zip
cd ..
rm all_carts.zip

echo "Downloading a boot hard disk for Geneve"
test -d disks || mkdir disks
wget -q https://ftp.whtech.com/emulators/MAME/div/genhd01.hd
mv genhd01.hd disks/

echo "Downloading UCSD Pascal disks"
wget -q https://ftp.whtech.com/Diskettes/UCSD_Pascal/ucsd_pascal_assembler_linker.dsk
wget -q https://ftp.whtech.com/Diskettes/UCSD_Pascal/ucsd_pascal_compiler.dsk
wget -q https://ftp.whtech.com/Diskettes/UCSD_Pascal/ucsd_pascal_editor_filer_1.dsk
wget -q https://ftp.whtech.com/Diskettes/UCSD_Pascal/ucsd_pascal_editor_filer_2.dsk
mv ucsd*.dsk disks/

echo "Downloading Editor/Assembler disks"
wget -q https://ftp.whtech.com/Diskettes/Cartridge_Disks/Editor_Assembler/Editor_Assembler_Part_A.dsk
mv Editor_Assembler*.dsk disks/

echo "Downloading a basic installation for HSGPL"
test -d nvram || mkdir nvram
test -d nvram/ti99_4a || mkdir nvram/ti99_4a
test -d nvram/ti99_4ev || mkdir nvram/ti99_4ev
test -d nvram/ti99_4p || mkdir nvram/ti99_4p
wget -q https://ftp.whtech.com/emulators/MAME/div/hsgpl40.zip
wget -q https://ftp.whtech.com/emulators/MAME/div/hsgpl80.zip
cd nvram/ti99_4a
unzip -q ../../hsgpl40.zip
cd ../ti99_4ev
unzip -q ../../hsgpl80.zip
cd ../ti99_4p
unzip -q ../../hsgpl80.zip
for file in *slot5_hsgpl*
do
   target=`echo $file | sed -e "s/ioport_peb_slot5/peb_slot3/"`
   mv $file $target
done
cd ../..
rm hsgpl40.zip hsgpl80.zip

echo "Preparing some launch scripts: ti99, ti99ea, ti99ex, geneve (and more)"
cat > ti99.bat << TI99
@echo off
$DRIVE
cd $MAMED
$MAMEBIN ti99_4a $GENPARAMS -ioport peb -ioport:peb:slot2 32kmem -ioport:peb:slot3 $SPEECH -ioport:peb:slot8 hfdc %*
pause
TI99
chmod 755 ti99.bat

cat > ti99ea.bat << TI99EA
@echo off
$DRIVE
cd $MAMED
$MAMEBIN ti99_4a $GENPARAMS -cart editass -ioport peb -ioport:peb:slot2 32kmem -ioport:peb:slot3 $SPEECH -ioport:peb:slot8 hfdc -flop1 disks/Editor_Assembler_Part_A.dsk %*
pause
TI99EA
chmod 755 ti99ea.bat

cat > ti99ev.bat << TI99EV
@echo off
echo Change screen aspect using the PAL/NTSC DIP switch of the EVPC
echo.
$DRIVE
cd $MAMED
$MAMEBIN ti99_4ev $GENPARAMS -cart editass -ioport peb -ioport:peb:slot2 evpc -ioport:peb:slot3 32kmem -ioport:peb:slot8 bwg %*
pause
TI99EV
chmod 755 ti99ev.bat

cat > ti99evh.bat << TI99EVH
@echo off
echo Change screen aspect using the PAL/NTSC DIP switch of the EVPC
echo.
$DRIVE
cd $MAMED
$MAMEBIN ti99_4ev $GENPARAMS -ioport peb -ioport:peb:slot2 evpc -ioport:peb:slot3 32kmem -ioport:peb:slot5 hsgpl -ioport:peb:slot8 bwg %*
pause
TI99EVH
chmod 755 ti99evh.bat

cat > ti99h.bat << TI99H
@echo off
echo.
$DRIVE
cd $MAMED
$MAMEBIN ti99_4a $GENPARAMS -ioport peb -ioport:peb:slot2 32kmem -ioport:peb:slot5 hsgpl -ioport:peb:slot8 bwg %*
pause
TI99H
chmod 755 ti99h.bat

cat > ti99ex.bat << TI99EX
@echo off
$DRIVE
cd $MAMED
$MAMEBIN ti99_4a $GENPARAMS -cart exbasic -ioport peb -ioport:peb:slot2 32kmem -ioport:peb:slot3 $SPEECH -ioport:peb:slot8 hfdc %*
pause
TI99EX
chmod 755 ti99ex.bat

cat > sgcpu.bat << SGCPU
@echo off
echo Change screen aspect using the PAL/NTSC DIP switch of the EVPC
echo.
$DRIVE
cd $MAMED
$MAMEBIN ti99_4p $GENPARAMS -peb:slot5 $SPEECH -peb:slot8 bwg %*
pause
SGCPU
chmod 755 sgcpu.bat

cat > geneve.bat << GEN
@echo off
$DRIVE
cd $MAMED
$MAMEBIN geneve $GENPARAMS -peb:slot5 $SPEECH -peb:slot6 tirs232 -peb:slot8 hfdc -peb:slot8:hfdc:h1 generic -hard1 disks\genhd01.hd %*
pause
GEN
chmod 755 geneve.bat

cat > ti998.bat << T998
@echo off
$DRIVE
cd $MAMED
$MAMEBIN ti99_8 $GENPARAMS -hexbus hx5102 %*
pause
T998
chmod 755 ti998.bat

cat > ti992.bat << T992
@echo off
$DRIVE
cd $MAMED
$MAMEBIN ti99_232 $GENPARAMS -hexbus hx5102 %*
pause
T992
chmod 755 ti992.bat

cat > ucsd_editor.bat << UCSDE
@echo off
echo Remember to activate the UCSD card by its DIP switch
echo. 
$DRIVE
cd $MAMED
$MAMEBIN ti99_4a $GENPARAMS -ioport peb -ioport:peb:slot2 32kmem -ioport:peb:slot4 pcode -ioport:peb:slot8 hfdc -flop1 disks/ucsd_pascal_editor_filer_1.dsk %*
pause
UCSDE
chmod 755 ucsd_editor.bat

echo "Installing into desktop folder 'MAME Launchers'"
test -d "$DESKTOP/MAME Launchers" || mkdir "$DESKTOP/MAME Launchers"
mv *.bat "$DESKTOP/MAME Launchers"/
