#!/bin/bash
#Force creation of user settings dir
mkdir -p "$HOME/.local/share/emulation"
#Url to download
BIOSURL="ftp://ftp.zimmers.net/pub/cbm/crossplatform/emulators/VICE/old/vice-1.5-roms.tar.gz"
#Install-Directory
BIOSDIR="/usr/lib/vice/"
#Disclaimer
emuhelper disclaimer
#Prepare, Download, Extract, Install
mkdir -p /tmp/vice /usr/lib/vice
#Download
if test -z "$DISPLAY" ; then
	wget "$BIOSURL" -O /tmp/vice/vice-bios.tar.gz
else
	kfmclient copy "$BIOSURL" file:/tmp/vice/vice-bios.tar.gz
fi
#Warn and exit if bios-package wasn´t found
test -f /tmp/vice/vice-bios.tar.gz || echo "Error - Bios-Packge not found"
test -f /tmp/vice/vice-bios.tar.gz || exit
#Unpack and prepare
gzip -f -d /tmp/vice/vice-bios.tar.gz
tar -C /tmp/vice/ -xf /tmp/vice/vice-bios.tar
#Find files to install
PATH_C64="`find /tmp/vice/ -type d -name C64 | head -1`"
PATH_PET="`find /tmp/vice/ -type d -name PET | head -1`"
PATH_C128="`find /tmp/vice/ -type d -name C128 | head -1`"
PATH_VIC20="`find /tmp/vice/ -type d -name VIC20 | head -1`"
PATH_CBMII="`find /tmp/vice/ -type d -name CBM-II | head -1`"
PATH_DRIVES="`find /tmp/vice/ -type d -name DRIVES | head -1`"
#Warn if c64-bios directory was not found
test -d "$PATH_C64" || echo "Error - Bios-Files not found"
test -d "$PATH_C64" || exit
#Copy
if test -z "$DISPLAY" ; then
	cp -R $PATH_C64 $BIOSDIR/
        cp -R $PATH_PET $BIOSDIR/
        cp -R $PATH_C128 $BIOSDIR/
        cp -R $PATH_VIC20 $BIOSDIR/
        cp -R $PATH_CBMII $BIOSDIR/
        cp -R $PATH_DRIVES $BIOSDIR/
else
	kfmclient copy "file:$PATH_C64" "file:$BIOSDIR/" 2> /dev/null
	kfmclient copy "file:$PATH_PET" "file:$BIOSDIR/" 2> /dev/null
	kfmclient copy "file:$PATH_C128" "file:$BIOSDIR/" 2> /dev/null
	kfmclient copy "file:$PATH_VIC20" "file:$BIOSDIR/" 2> /dev/null
	kfmclient copy "file:$PATH_CBMII" "file:$BIOSDIR/" 2> /dev/null
	kfmclient copy "file:$PATH_DRIVES" "file:$BIOSDIR/" 2> /dev/null
fi
#Cleanup
rm -r /tmp/vice
