#!/bin/bash

function help {
	echo ""
        echo "Bash shell script that copies specific MAME rom files, based on a list of names in a text file."
        echo "This script's creation was based on the \"All Killer, No Filler\" rom list thread available at [http://forum.arcadecontrols.com/index.php?topic=149708.0]."
        echo ""
        echo "Parameters: "
        i_help
        echo "Usage: ./mame_aknf.sh -i [input_file]"
	echo ""
}

function i_help {
	echo "  -i [Required]		Input file. Path to a text file containing one rom name per line. The rom names must not"
	echo "			  have an archive extension (.zip, .7z, etc). Rom names starting with \"::\" will be ignored."
	echo ""
}


# If no arguments were passed, show help and exit
if [[ ${#@} -eq 0 ]]; then
	help
	exit 0
fi

while getopts :i:h option; do
	case "${option}" in
		i) 	input_file=${OPTARG}

			# If the input file does not exist
			if [[ ! -e "${input_file}" ]]; then
				echo "Input file \"${input_file}\" does not exist"
				echo ""
				exit 5
			fi
		;;

		h)
			help
			exit 0
		;;

		:) 	

			if [[ "$OPTARG" == "i" ]]; then
				echo "The \"-i\" option requires the path and name of the rom name list file."
				echo ""

				i_help
			fi

		   exit 1
		;;
		\?)
			#echo "Invalid option: -$OPTARG" >&2
			exit 1
		;;

	esac
done

#########################################################

# Set the source rom files directory
echo ""
read -p "ROM files source directory? [$(pwd)/]: " dir_rom

# If none entered, use the current working directory
if [[ -z ${dir_rom} ]]; then
	dir_rom=$(pwd)
else
	# Check if directory exist
	if [[ ! -d "${dir_rom}" ]]; then
		echo "Directory \"${dir_rom}\" does not exist."
		exit 1
	fi
fi

# Set the destination directory
echo ""
read -p "Destination directory? [$(pwd)/_NoFiller/]: " dir_nofiller

# If none entered, use the previously chosen directory plus "_NoFiller"
if [[ -z ${dir_nofiller} ]]; then
	dir_nofiller=$(pwd)/_NoFiller/
fi

# Check if directory exist
if [[ ! -d "${dir_nofiller}" ]]; then
	echo -n "Creating destination directory \"${dir_nofiller}\"... "
	mkdir -p ${dir_nofiller}

	if [[ $? == 0 ]]; then 
		echo "done"
	else
		echo "fail"
		exit 2
	fi
fi

# Get the file format
echo ""
read -p "Archive format? [a] .zip [b] .7z [c] other: " -n1 file_format

# Convert answer to lower case
file_format=$(echo ${file_format} | tr '[A-Z]' '[a-z'])

if [[ "${file_format}" == "a" || "${file_format}" == "b" || "${file_format}" == "c" ]]; then

	if [[ "${file_format}" == "a" ]]; then
		file_format="zip"

	elif [[ "${file_format}" == "b" ]]; then
		file_format="7z"

	else
		echo ""
		read -p "Please enter the archive format extension (without the dot): " file_format
	fi
else

	echo "Invalid archive format option"
	exit 3
fi


echo ""

# Array to keep the names of the not found ROM files, to be displayed at the end
declare -a files_not_found

while IFS='' read -r line || [[ -n "$line" ]]; do

	# Remove carriage return (\r) from string read from DOS txt file
	line=$(echo "$line" | tr -d '\r')

	echo -n "Searching ROM \"${dir_rom}/${line}.${file_format}\"... "

	if [[ "${line:0:2}" == "::" ]]; then
		echo "ignoring. Remove comment \"::\" to copy."
		continue
	fi

	# Copy the ROM to the nofiller directory
	if [[ -e "${dir_rom}"/"${line}"."${file_format}" ]]; then

		echo "found!"
		echo -n "Copying \"${line}.${file_format}\" to \"${dir_nofiller}\" directory... "
		cp "${dir_rom}"/"${line}"."${file_format}" "${dir_nofiller}"/"${line}"."${file_format}"

		if [[ $? == 0 ]]; then
			echo "done"
		else
			echo "fail"
			exit 4
		fi

		echo ""

	else
		echo "not found"
		files_not_found+=(${line})
	fi

done < "${input_file}"


echo ""
echo "The following ROMs were not found: "

for rom in "${files_not_found[@]}"; do
	echo -n ${rom} ""
done


echo ""
echo ""
read -p "Do you want to save the list of not found ROMs to a new file? [Y/n] " -n1 new_file_option

# Convert answer to lower case
new_file_option=$(echo ${new_file_option} | tr '[A-Z]' '[a-z'])

if [[ -z ${new_file_option} || "${new_file_option}" == "y" ]]; then

	read -p "Not found roms list's filename? [$(pwd)/aknf_notfound.txt]: " output_file

	# If empty, set default path/name
	if [[ -z ${output_file} ]]; then
		output_file=$(pwd)/aknf_notfound.txt
	fi

	echo "" > ${output_file}

	for filename in "${files_not_found[@]}"; do
		echo "${filename}" >> ${output_file}
	done

fi
