#!/bin/bash

ROM_ARG=$1

usage() {

echo "$0 : 	Recupere les medias de type information / snapshoot / wheel selon votre fichier ROM_CONFIG_FILES.csv"
echo "		Recupere aussi les ROMS MAME / FBA / GNGEO si elles existent sur le serveur"	
echo ""	
echo "OPTION : "
echo "		$0 --> recupere tout les medias"
echo "		$0 \"[rom],[EMU]\" --> recupere tout les medias d'une rom particuliere en mode FORCE (ecrase l'existant)"
exit

}

test_arg() {
EMU=`echo $ROM_ARG | cut -d"," -f2`
ROM=`echo $ROM_ARG | cut -d"," -f1`

if [[ -z $EMU ]] || [[ -z $ROM ]] || [[ -z `echo $ROM_ARG | grep ","` ]]
	then
		usage

fi

}

TEST_REP() {

if [[ ! -z $ROM_ARG ]]
	then 
		for REP in WHEEL SNAP VIDEO ROMS DOCS
			do 
				if [[ ! -d ../MEDIA/${EMU}/$REP ]]
					then
						echo "Creation rep IMG de ${EMU} : ../MEDIA/${EMU}/${REP}"
						mkdir -p ../MEDIA/${EMU}/${REP}
				fi
		done
	else

		for EMU_DIR in `cat ../ROM_CONFIG_FILES.csv | cut -d"," -f2 | sort -u`
			do
				for REP in WHEEL SNAP VIDEO ROMS DOCS
					do 
						if [[ ! -d ../MEDIA/${EMU_DIR}/$REP ]]
							then
								echo "Creation rep IMG de ${EMU_DIR} : ../MEDIA/${EMU_DIR}/${REP}"
								mkdir -p ../MEDIA/${EMU_DIR}/${REP}
						fi
				done

			done
fi

}


get_docs() {
	if [[ "${EMU}" == "MAME" ]] || [[ "${EMU}" == "FBA" ]] || [[ "${EMU}" == "GNGEO" ]] 
		then
			EMU_DL=ARCADE
		else
			EMU_DL=$EMU	
		
		fi
	wget -c -q -nv "http://media.pyrharckade.io/${EMU_DL}/DOCS/${ROM}.txt" -O ../MEDIA/${EMU}/DOCS/${ROM}.txt
	RES=$?
	if [[ "$RES" != "0" ]]
		then
			rm -f ../MEDIA/${EMU}/DOCS/${ROM}.txt
			echo -e " -> DOCS \033[31m FAILED \033[0m"
		else
			echo -e " -> DOCS \033[32m SUCCESS \033[0m"

	fi

}

get_snap() {
	if [[ "${EMU}" == "MAME" ]] || [[ "${EMU}" == "FBA" ]] || [[ "${EMU}" == "GNGEO" ]] 
		then
			EMU_DL=ARCADE
		else
			EMU_DL=$EMU	
	
		fi
	wget -c -q -nv "http://media.pyrharckade.io/${EMU_DL}/SNAP/${ROM}.png" -O ../MEDIA/${EMU}/SNAP/${ROM}.png
	RES=$?
	if [[ "$RES" != "0" ]]
		then
			rm -f ../MEDIA/${EMU}/SNAP/${ROM}.png
			echo -e " -> SNAP \033[31m FAILED \033[0m"
		else
			echo -e " -> SNAP \033[32m SUCCESS \033[0m"
	fi

}

get_wheel() {
	if [[ "${EMU}" == "MAME" ]] || [[ "${EMU_DL}" == "FBA" ]] || [[ "${EMU}" == "GNGEO" ]] 
		then
			EMU_DL=ARCADE
		else
			EMU_DL=$EMU	
	
		fi

	wget -c -q -nv "http://media.pyrharckade.io/${EMU}/WHEEL/${ROM}.png" -O ../MEDIA/${EMU}/WHEEL/${ROM}.png
	RES=$?
	if [[ "$RES" != "0" ]]
		then
			rm -f ../MEDIA/${EMU}/WHEEL/${ROM}.png
			echo -e " -> WHEEL \033[31m FAILED \033[0m"
		else
			echo -e " -> WHEEL \033[32m SUCCESS \033[0m"
	fi

}
get_video() {
	if [[ "${ROM}" == 001_* ]]
		then
			echo -e " -> VIDEO \033[30m NA \033[0m" 

		else

			if [[ "${EMU}" == "MAME" ]] || [[ "${EMU}" == "FBA" ]] || [[ "${EMU}" == "GNGEO" ]] 
				then
					EMU_DL=ARCADE
				else
					EMU_DL=$EMU
			fi
			wget -c -q -nv "http://media.pyrharckade.io/${EMU_DL}/VIDEO/${ROM}.mpg" -O ../MEDIA/${EMU}/VIDEO/${ROM}.mpg
			RES=$?
			if [[ "$RES" != "0" ]]
				then
					rm -f ../MEDIA/${EMU}/VIDEO/${ROM}.mpg
					echo -e " -> VIDEO \033[31m FAILED \033[0m"
				else
					echo -e " -> VIDEO \033[32m SUCCESS \033[0m"
			fi
	fi

}

get_bios() {
	echo "CHECK BIOS FILES"
	if [[ -f ~/pyrharckade/MEDIA/BIOS/bios_pack ]]
		then
			echo -e " -> BIOS PACK \033[32m SUCCESS \033[0m"
		else
			mkdir ~/pyrharckade/MEDIA/BIOS/
			wget -c -q -nv "http://media.pyrharckade.io/BIOS/bios_pack.zip" -O ~/pyrharckade/MEDIA/BIOS/bios_pack.zip
			cd ~/pyrharckade/MEDIA/BIOS/
			unzip -e bios_pack.zip > /dev/null
			for EMU in `cat ~/pyrharckade/ROM_CONFIG_FILES.csv|cut -d',' -f2|sort -u| grep -E "MAME|FBA|GNGEO"`
				do
					for bios in `ls -1| grep zip$| grep -v bios_pack.zip`
						do
							ln -s ~/pyrharckade/MEDIA/BIOS/$bios ~/pyrharckade/MEDIA/${EMU}/ROMS/$bios
						done
				done
			touch bios_pack
			cd -
			echo -e " -> BIOS PACK \033[32m SUCCESS \033[0m"
	fi

}



get_roms() {

	if [[ "${ROM}" == 001_* ]]
		then
			echo -e " -> VIDEO \033[30m NA \033[0m" 

	else
		EMU_URL=$EMU

		if [[ "${EMU}" == "MAME" ]] || [[ "${EMU}" == "FBA" ]] || [[ "${EMU}" == "GNGEO" ]] 
			then
				EMU_URL=ARCADE
				EXT_ROM=".zip"
		fi
                if [[ "${EMU}" == "GAMEBOY" ]]
                        then
                                EXT_ROM=".gb"
		fi
                if [[ "${EMU}" == "N64" ]]
                        then
				EXT_ROM=".z64"
		fi
                if [[ "${EMU}" == "NES" ]]
                        then
				EXT_ROM=".nes"
		fi



		wget -c -q -nv "http://media.pyrharckade.io/${EMU_URL}/ROMS/${ROM}${EXT_ROM}" -O ../MEDIA/${EMU}/ROMS/${ROM}${EXT_ROM}
		RES=$?
		if [[ "$RES" != "0" ]]
			then
				rm -f  ../MEDIA/${EMU}/ROMS/${ROM}${EXT_ROM}
				echo -e " -> ROMS ${EMU} \033[31m FAILED \033[0m"
			else
				echo -e " -> ROMS ${EMU} \033[32m SUCCESS \033[0m"
		fi
	fi

}



if [[ -z $ROM_ARG ]]
	then
	TEST_REP
fi


if [[ ! -z $ROM_ARG ]]
	then
		test_arg
		TEST_REP
		get_snap
		get_wheel
		get_video
		get_docs
		get_roms
		get_bios
		exit 0
fi

export DISPLAY=0:0
python ../CONF/download.py &

while read line 
	do
		if [[ ! -z `echo $line | grep ','` ]]
			then	
				echo $line | while IFS=',' read ROM EMU
				do
					echo -e "----- \033[36m $ROM \033[0m -----"
					get_snap
					get_wheel
					get_video
					get_docs
					get_roms	
				done
		fi
done < ../ROM_CONFIG_FILES.csv 
get_bios
sudo killall -9 python
