#!/bin/bash

#check if there is any parameter
if [ "$#" -ne 1 ]; then
    echo "You must enter a source file."
    exit 0
fi

#check if file exists
if ! test -f "$1"; then
    echo "$1 does not exists."
    exit 0
fi

#variables
hashDir=data
gameListDir=gamelists
systemName="$(basename "$1" .html)"
echo "Processing $systemName."
tempFile1="${systemName}-1.tmp"
tempFile2="${systemName}-2.tmp"
hashList="./${hashDir}/${systemName}.hashlist"

# extract hashes from files on data/system folder

# if file exists, delete
if test -f "${hashList}"; then
    rm "${hashList}"
fi

# for every file in folder, do
for i in ./${gameListDir}/$systemName/*; do
    # catch hash and append to file
    grep -E "[a-f0-9]{32}" -Eo $i >> ${hashList}
    echo  "Parsing ${i} ..."
done
echo "Parsed $(wc -l < ${hashList}) hashes for $(find ${gameListDir}/${systemName} -type f | wc -l) games."
