#!/bin/bash

cd ../
WIM=`pwd`
echo $WIM

if [[ ! -d "${WIM}/MEDIA/BIOS/" ]]; then mkdir -p ${WIM}/MEDIA/BIOS/ROMS/;fi

cd ${WIM}/BIN
./download_graphical.py BIOS ROMS BIOS



cd ${WIM}/MEDIA/BIOS/ROMS/
tar -jxf BIOS.tar.bz2
for EMU in `cat ${WIM}/ROM_CONFIG_FILES.csv  | cut -d"," -f2 | sort -u| grep -E "FBA|MAME|CPS3|GNGEO"`
	do
		echo "------------------------ $EMU ------------------------"
		for bios_file in  `find ${WIM}/MEDIA/BIOS/ROMS/ | grep "\.zip"`		
			do	
				bios=`basename $bios_file`
				if [[ ! -d "${WIM}/MEDIA/${EMU}/ROMS/" ]] ;then mkdir -p ${WIM}/MEDIA/${EMU}/ROMS/ ;fi
				if [[ -f ${WIM}/MEDIA/${EMU}/ROMS/${bios} ]];then rm -f ${WIM}/MEDIA/${EMU}/ROMS/${bios} ;fi
				ln -s ${WIM}/MEDIA/BIOS/ROMS/${bios} ${WIM}/MEDIA/${EMU}/ROMS/${bios}
			done

	done
