#!/bin/sh

echo "Update the current installation."
pacman -Syu

echo
echo "Install the git, make, and unzip commands. Git is used to manage the source code, for example, allowing you to download the latest changes."
pacman -S git make unzip

echo
echo "Install the compiler, libraries, and the Python interpreter."
pacman -S mingw-w64-x86_64-gcc mingw-w64-x86_64-libc++ mingw-w64-x86_64-python

cd $HOME
curl -O "https://repo.mamedev.org/x86_64/mame-essentials-1.0.6-1-x86_64.pkg.tar.xz"
pacman -U mame-essentials-1.0.6-1-x86_64.pkg.tar.xz
echo -e '\n[mame]\nInclude = /etc/pacman.d/mirrorlist.mame\nSigLevel = Never' >> /etc/pacman.conf
pacman -Syu
echo "export MINGW64=/mingw64 MINGW32=" >> .bashrc

if [ ! -d mamegit ]
then
   mkdir mamegit
fi

cd mamegit

if [ ! -f maketi ]
then
   curl -O "https://www.mizapf.eu/files/mame/winscripts.tar.gz"
   tar xvzf winscripts.tar.gz
fi

MAMEDIR=`cygpath -w $PWD`
echo "Cloning the MAME source repository into $MAMEDIR/mame"

if [ ! -d mame ]
then
   git clone https://github.com/mamedev/mame.git
fi

echo "Done. Please close this window and open it again to make the changes effective."

echo "For building, open this window again (search for MINGW64),"
echo "enter the build directory with 'cd mamegit' and then"
echo
echo "Build MAME: ./maketi"
echo "Install newly built  mame.exe: ./mameinst"
