#!/bin/bash
PLAT=`uname -p`
MAMEBIN=mame

function set_param {
   sed  "s/^$1 .*/$1                $2/" mame.ini > mame.ini1
   mv mame.ini1 mame.ini
}

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"
wget -q https://ftp.whtech.com/System%20ROMs/MAME/ti99_complete.zip 
cd roms
unzip -q ../ti99_complete.zip
cd ..
rm ti99_complete.zip

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

echo
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
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
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 "Creating config files"
test -s mame.ini || ./$MAMEBIN -createconfig

echo
echo "Editing the config file"
set_param readconfig "0"
set_param rompath "roms;carts"
set_param window "1"
set_param maximize "0"
set_param resolution "800x600"
set_param video "bgfx"

echo
echo "Preparing some launch scripts: ti99, ti99ea, ti99ex, geneve (and more)"
cat > ti99 << TI99
#!/bin/bash
./$MAMEBIN ti99_4a -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 -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 -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 -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 -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 -peb:slot5 speech -peb:slot8 bwg \$*
SGCPU
chmod 755 sgcpu

cat > geneve << GEN
#!/bin/bash
./$MAMEBIN geneve -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 -hexbus hx5102 \$*
T998
chmod 755 ti998

cat > ti992 << T992
#!/bin/bash
./$MAMEBIN ti99_232 -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 -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
