#!/bin/sh

cd "$(dirname "${0}")"
mkdir -p download/archive

set -e
readonly bmc64url='https://accentual.com/bmc64/'
readonly destdir="${1}"

for i in 7z bzip2 cp grep sed tar wget; do
   type 2>/dev/null >/dev/null "${i}" ||
      missing_tools="${missing_tools} ${i}"
done

if [ -z "${destdir}" ]; then
   cat <<EOF
usage: $0 <destdir> (gpio_config=<value>) (no_hdmi|no_composite)
EOF
   if [ -n "${missing_tools}" ]; then
      echo "These tools are missing:${missing_tools}"
   fi
   exit 0
fi
shift

if [ -n "${missing_tools}" ]; then
   echo "These tools are missing:${missing_tools}"
   exit 1
fi

if [ ! -d "${destdir}" ]; then
   echo "target directory '${destdir}' not found."
   exit 1
fi

gpio_config=""

while [ "${1}" != "" ]; do
case "${1}" in
gpio_config=*) gpio_config="${1#*=}" ;;
gpio_config) shift ; gpio_config="${1}" ;;
no_hdmi|nohdmi) no_video="HDMI" ;;
no_composite|nocomposite) no_video="Composite" ;;
esac
shift
done

readonly gpio_config

download()
(
#^ start subprocess, so cd does not affect whole program
   local targetdir="${1}"
   local url
   local filename
   shift
   cd "${targetdir}"
   if [ -n "$(type -p wget 2>/dev/null)" ]; then
      for url; do
         case "${url}" in
         *://sourceforge.net/settings/mirror_choices*)
            url="$(sfurl "${url}")"
            filename="${url##*/}"
            filename="${filename%\?*}"
            ;;
         *\|*) filename="${url%|*}" ; url="${url#*|}" ;;
         *)    filename="${url##*/}" ;;
         esac
         if [ ! -f "${filename}" ]; then
            wget -O "${filename}" "${url}"
         fi
      done
      return
   fi
   if [ -n "$(type -p curl 2>/dev/null)" ]; then
      for url; do
         case "${url}" in
         *\|*) filename="${url#*|}" ; url="${url%|*}" ;;
         *)    filename="${url##*/}" ;;
         esac
         if [ ! -f "${filename}" ]; then
            curl -o "${filename}" -L "${url}"
         fi
      done
      return
   fi
   echo "neither curl nor wget found"
   exit 1
)

sfurl()
{
   wget -O - "${1}" |
      sed -e 's|<a|\n|g' -e 's|</a>|\n|g' |
      grep 'direct link' |
      sed -e 's|.* href="\([^"]*\).*|\1|'
}

copyvice()
{
   local machine="${1}"
   shift
   for file; do
      cp -v "download/vice-3.3/data/${machine}/${file%:*}" "${destdir}/${machine}/${file#*:}"
      chmod 0644 "${destdir}/${machine}/${file#*:}" 2>/dev/null || true
   done
}

copyplus4emu()
{
   for file; do
      cp -v "download/plus4emu-1.2.9.2/roms/${file}" "${destdir}/PLUS4EMU/${file}"
      chmod 0644 "${destdir}/PLUS4EMU/${file}" 2>/dev/null || true
   done
}

copyrom()
{
   local machine="${1}"
   shift
   for file; do
      cp -v "download/${file%:*}" "${destdir}/${machine}/${file#*:}"
      chmod 0644 "${destdir}/${machine}/${file#*:}" 2>/dev/null || true
   done
}

copydata()
{
   local target="${1}"
   shift
   for file; do
      cp -v "download/${file%:*}" "${destdir}/${target}/${file#*:}"
      chmod 0644 "${destdir}/${target}/${file#*:}" 2>/dev/null || true
   done
}

bmc64zipurl="$( { wget -O - "${bmc64url}" || curl -L "${bmc64url}" ; } |
               grep '\.files\.zip"' | head -1 | cut -f2 -d\")"

download download/archive \
   ${bmc64url}/${bmc64zipurl} \
   "https://sourceforge.net/settings/mirror_choices?projectname=vice-emu&filename=releases/vice-3.3.tar.gz&selected=master" \
   "https://sourceforge.net/settings/mirror_choices?projectname=plus4emu&filename=plus4emu/plus4emu-1.2.9.2/plus4emu-1.2.9.2.tar.bz2&selected=master" \
   https://rr.pokefinder.org/rrwiki/images/1/11/Kernal_ROM_all_rr.c64.org_2021-02.rar \
   https://rr.pokefinder.org/rrwiki/images/d/d3/Action_Replay_binaries_rr.c64.org_2022-08.rar \
   https://rr.pokefinder.org/rrwiki/images/1/1e/Final_Cartridge_binaries_rr.c64.org_2021-02.rar

download download \
   http://www.zimmers.net/anonftp/pub/cbm/firmware/computers/c128/z80bios.bin \
   https://www.pagetable.com/docs/c16kbd/kernal.318004-05-kbd64.bin \
   https://www.pagetable.com/docs/c16kbd/kernal.318005-05-kbd64.bin \
   http://csdb.dk/getinternalfile.php/194624/PXLfont88665b-RF2.3-C64sys.bin

cd download
if [ ! -d "plus4emu-1.2.9.2/roms" ]; then
   tar xf archive/plus4emu-1.2.9.2.tar.bz2 plus4emu-1.2.9.2/roms
fi
if [ ! -d "vice-3.3/data" ]; then
   tar xf archive/vice-3.3.tar.gz vice-3.3/data
fi
if [ ! -d "Dolphin_DOS" ]; then
   7z x -y archive/Kernal_ROM_all_rr.c64.org_2021-02.rar Dolphin_DOS || { retval=$?; echo "did you install rar support for p7zip?" ; return ${retval} ; }
fi
if [ ! -d "Action_Replay" ]; then
   7z x -y archive/Action_Replay_binaries_rr.c64.org_2022-08.rar Action_Replay
fi
if [ ! -d "Final_Cartridge" ]; then
   7z x -y archive/Final_Cartridge_binaries_rr.c64.org_2021-02.rar Final_Cartridge
fi
cd ..

7z x -y -o"${destdir}" "download/archive/${bmc64zipurl##*/}"

if [ -n "${gpio_config}" ]; then
   for s in settings.txt settings-c128.txt settings-vic20.txt settings-plus4.txt settings-plus4emu.txt settings-pet.txt ; do
      if [ -f "${destdir}/${s}" ]; then
         sed -i -e '/^gpio_config=/d' "${destdir}/${s}"
      fi
      echo "gpio_config=${gpio_config}" >> "${destdir}/${s}"
   done
fi

if [ -n "${no_video}" ]; then
   sed -i -e "/${no_video}/,/^$/d" "${destdir}/machines.txt"
fi

copyvice C64 \
   kernal \
   chargen \
   basic \
   ../DRIVES/d1541II:d1541II \
   ../DRIVES/dos1581:dos1581

copyvice C128 \
   kernal \
   basichi \
   basiclo \
   ../C64/chargen:charg64 \
   chargde:chargen \
   kernal64 \
   basic64 \
   ../DRIVES/d1541II:d1541II \
   ../DRIVES/dos1571:dos1571 \
   ../DRIVES/dos1581:dos1581

copyrom C128 \
   z80bios.bin:z80bios

copyvice VIC20 \
   basic \
   chargen \
   kernal \
   ../DRIVES/d1541II:d1541II \
   ../DRIVES/dos1581:dos1581

copyvice PLUS4 \
   kernal \
   kernal.005 \
   kernal.232 \
   3plus1hi \
   3plus1lo \
   basic \
   c2lo.364 \
   ../DRIVES/d1541II:d1541II \
   ../DRIVES/d1541II:dos1551 \
   ../DRIVES/dos1581:dos1581

copyrom PLUS4 \
   kernal.318004-05-kbd64.bin \
   kernal.318005-05-kbd64.bin

copyplus4emu \
   3plus1.rom \
   dos1541.rom \
   dos1551.rom \
   p4_basic.rom \
   p4fileio.rom \
   p4kernal.rom \
   p4_ntsc.rom

copyrom PLUS4EMU \
   kernal.318004-05-kbd64.bin \
   kernal.318005-05-kbd64.bin

copyvice PET \
   basic1 \
   basic2 \
   basic4 \
   characters.901640-01.bin \
   chargen \
   edit1g \
   edit2b \
   edit2g \
   edit4b40 \
   edit4b80 \
   edit4g40 \
   hre-9000.324992-02.bin \
   hre-a000.324993-02.bin \
   kernal1 \
   kernal2 \
   kernal4 \
   waterloo-a000.901898-01.bin \
   waterloo-b000.901898-02.bin \
   waterloo-c000.901898-03.bin \
   waterloo-d000.901898-04.bin \
   waterloo-e000.901897-01.bin \
   waterloo-f000.901898-05.bin 

copyrom C64 \
   Dolphin_DOS/c1541-20-6.rom:dolphindos-1541-20-6.rom \
   Dolphin_DOS/c1541-20-8.rom:dolphindos-1541-20-8.rom \
   Dolphin_DOS/kernal-20-2.rom:dolphindos-c64-20-2.rom \
   Dolphin_DOS/kernal-20-3.rom:dolphindos-c64-20-3.rom \
   Dolphin_DOS/kernal-30.rom:dolphindos-c64-30.rom

copyrom C128 \
   Dolphin_DOS/c1541-20-6.rom:dolphindos-1541-20-6.rom \
   Dolphin_DOS/c1541-20-8.rom:dolphindos-1541-20-8.rom \
   Dolphin_DOS/kernal-20-2.rom:dolphindos-c64-20-2.rom \
   Dolphin_DOS/kernal-20-3.rom:dolphindos-c64-20-3.rom \
   Dolphin_DOS/kernal-30.rom:dolphindos-c64-30.rom \
   Dolphin_DOS/kernal-dolphin128.rom:dolphindos-c128.rom

copydata carts/C64 \
   Action_Replay/Action_Replay_V6.0_6.0_1989_PAL.crt:Action_Replay_V6.0_6.0_1989_PAL.crt \
   Action_Replay/Action_Replay_V5.0_5.0_1988_NTSC.crt:Action_Replay_V5.0_5.0_1988_NTSC.crt \
   Final_Cartridge/Final_Cartridge_3_1988-12.crt:Final_Cartridge_3_1988-12.crt \
   Final_Cartridge/Final_Cartridge_3_1988-13.crt:Final_Cartridge_3_1988-13.crt

