#!/bin/bash
#Global config
BIOSLIST="/usr/share/emulation/config/biospath.conf"
#Force creation of user settings dir
mkdir -p "$HOME/.local/share/emulation"
#Test local config
test -f "$HOME/.local/share/emulation/biospath.conf" && BIOSLIST="$HOME/.local/share/emulation/biospath.conf"
#Source path-infor for bios-images
source $BIOSLIST
###
#Global bios directory
export BIOSGLOBAL="$bios_mess"

#Local bios directory
export BIOSLOCAL="$HOME/.xmess/bios"

#URL to download the files
case `expr $RANDOM % 3` in
	0)	export BIOSURL="http://www.slor.net/mess" ;;
	1)	export BIOSURL="http://www.geocities.com/mess_bioses/BIOS" ;;
	2)	export BIOSURL="http://www.freewarelive.com/soft/rombios/" ;;
esac
#Domain where bios-images are downloaded
BIOSPROVIDER="`echo $BIOSURL | cut -d'/' -f 3`"

#Disclaimer
emuhelper disclaimer

#Installation preperation
mkdir -p $HOME/.xmess/bios

#Bios to dowload
EMUSYSTEM="$1"
test -z "$EMUSYSTEM" && export EMUSYSTEM="`emuhelper getsys_xmess`"
test -z "$EMUSYSTEM" && echo "Usage: kbiosget_xmess [System]" && exit

#Passive information
test -n "$DISPLAY" && kdialog --passivepopup "Downloading $EMUSYSTEM.zip from $BIOSPROVIDER" 3 &

#Download to local folder
if test -n "$DISPLAY" ; then
	#kde-mode
	kfmclient copy $BIOSURL/$EMUSYSTEM.zip $BIOSLOCAL/$EMUSYSTEM.zip
	if test -f "$BIOSLOCAL/$EMUSYSTEM.zip"
		then	echo "Biosset is available"
			kdesu --title "Enter root-password to install bios system-wide" cp $BIOSLOCAL/$EMUSYSTEM.zip $BIOSGLOBAL/$EMUSYSTEM.zip
		else	emuhelper warn_xmess_dl
	fi
else
	#Bash mode
	wget -O "$BIOSLOCAL/$EMUSYSTEM.zip" "$BIOSURL/$EMUSYSTEM.zip" || rm "$BIOSLOCAL/$EMUSYSTEM.zip"
	if test -f "$BIOSLOCAL/$EMUSYSTEM.zip"
		then	echo "Biosset is available - Enter root.password for global install!"
			sudo cp $BIOSLOCAL/$EMUSYSTEM.zip $BIOSGLOBAL/$EMUSYSTEM.zip
		else	echo "Biosset is unavailable"
	fi
fi
