#!/bin/bash
MAMEBIN=mame
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"

# Video driver
VIDEO="bgfx"

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

ROMPATH="$SYSROMS"

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

GENPARAMS="-rompath \"$ROMPATH\" -video $VIDEO"
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

NOTFOUND=`ldd $MAMEBIN | grep found`

if [ $? -eq 0 ]
then 
   echo Libraries are missing. Please install them and run this script again.
   echo
   echo $NOTFOUND | sed -e "s/found /found@/g" | tr '@' '\n'
   echo
   exit
fi

which wget > /dev/null 2>&1

if [ $? -eq 1 ]
then 
    echo "Error: Please install 'wget'."
    exit
fi

which unzip > /dev/null 2>&1
if [ $? -eq 1 ]
then 
    echo "Error: Please install 'unzip'."
    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 << TI99
#!/bin/bash
./$MAMEBIN ti99_4a $GENPARAMS -ioport peb -ioport:peb:slot2 32kmem -ioport:peb:slot3 $SPEECH -ioport:peb:slot8 hfdc \$*
TI99
chmod 755 ti99

cat > ti99ea << TI99EA
#!/bin/bash
./$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 \$*
TI99EA
chmod 755 ti99ea

cat > ti99ev << TI99EV
#!/bin/bash
echo Change screen aspect using the PAL/NTSC DIP switch of the EVPC
echo
./$MAMEBIN ti99_4ev $GENPARAMS -cart editass -ioport peb -ioport:peb:slot2 evpc -ioport:peb:slot3 32kmem -ioport:peb:slot8 bwg \$*
TI99EV
chmod 755 ti99ev

cat > ti99evh << TI99EVH
#!/bin/bash
echo Change screen aspect using the PAL/NTSC DIP switch of the EVPC
echo
./$MAMEBIN ti99_4ev -ioport peb -ioport:peb:slot2 evpc -ioport:peb:slot3 32kmem -ioport:peb:slot5 hsgpl -ioport:peb:slot8 bwg \$*
TI99EVH
chmod 755 ti99evh

cat > ti99h << TI99H
#!/bin/bash
./$MAMEBIN ti99_4a $GENPARAMS -ioport peb -ioport:peb:slot2 32kmem -ioport:peb:slot5 hsgpl -ioport:peb:slot8 bwg \$*
TI99H
chmod 755 ti99h

cat > ti99ex << TI99EX
#!/bin/bash
./$MAMEBIN ti99_4a $GENPARAMS -cart exbasic -ioport peb -ioport:peb:slot2 32kmem -ioport:peb:slot3 $SPEECH -ioport:peb:slot8 hfdc \$*
TI99EX
chmod 755 ti99ex

cat > sgcpu << SGCPU
#!/bin/bash
./$MAMEBIN ti99_4p $GENPARAMS -peb:slot5 $SPEECH -peb:slot8 bwg \$*
SGCPU
chmod 755 sgcpu

cat > geneve << GEN
#!/bin/bash
./$MAMEBIN geneve $GENPARAMS -peb:slot5 $SPEECH -peb:slot6 tirs232 -peb:slot8 hfdc -peb:slot8:hfdc:h1 generic -hard1 disks/genhd01.hd \$*
GEN
chmod 755 geneve

cat > ti998 << T998
#!/bin/bash
./$MAMEBIN ti99_8 $GENPARAMS -hexbus hx5102 \$*
T998
chmod 755 ti998

cat > ti992 << T992
#!/bin/bash
./$MAMEBIN ti99_232 $GENPARAMS -hexbus hx5102 \$*
T992
chmod 755 ti992

cat > ucsd_editor << UCSDE
#!/bin/bash
echo Remember to activate the UCSD card by its DIP switch
echo 
./$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 \$*
UCSDE
chmod 755 ucsd_editor
