#!/bin/bash
[ "$2" == "" ] && exec echo "Usage: $0 <ENVIRONMENT> <LANGUAGE>"

ENVIRONMENT=$1
LANG=$2
U_LANG="${LANG^^}"
L_LANG="${LANG,,}"

OUTPUT_FILE="/tmp/L_${ENVIRONMENT}_${L_LANG}_md5"
CHECK_FILE_LIST="/tmp/localize_${ENVIRONMENT}_filelist"
LOCALIZE_DIRECTORY="/home/translate/localize_${L_LANG}"
CLIENT_DIRECTORY="/home/fndev/www/FN-TEST"
REMOTE_DIRECTORY="/home/fndev/www/FN-TEST_SYNCTOLIVE"

[ -f "${OUTPUT_FILE}" ] && rm -rf ${OUTPUT_FILE}
[ -f "${CHECK_FILE_LIST}" ] && rm -rf ${CHECK_FILE_LIST}

cd ${LOCALIZE_DIRECTORY}; ls nfs/*/* >> ${CHECK_FILE_LIST}

if [ "${ENVIRONMENT}" == "LOCALIZE" ]; then
	md5sum ${LOCALIZE_DIRECTORY}/GameDataTranslateFileList_${L_LANG}.txt | sed "s|${LOCALIZE_DIRECTORY}/||" >> ${OUTPUT_FILE}
	cd ${LOCALIZE_DIRECTORY}
	for FILE in $(cat ${CHECK_FILE_LIST} | sort); do md5sum ${FILE} >> ${OUTPUT_FILE} ; done
elif [ "${ENVIRONMENT}" == "CLIENT" ]; then
	md5sum ${CLIENT_DIRECTORY}/GameDataTranslateFileList_${L_LANG}.txt | sed "s|${CLIENT_DIRECTORY}/||" >> ${OUTPUT_FILE}
	cd ${CLIENT_DIRECTORY}
	for FILE in $(cat ${CHECK_FILE_LIST} | sort); do md5sum ${FILE} >> ${OUTPUT_FILE} ; done
elif [ "${ENVIRONMENT}" == "REMOTE" ]; then
	NFS_EXTENSION=$(cat ${REMOTE_DIRECTORY}/Setup.txt | grep NFSExtension | sed 's/NFSExtension=//')
	md5sum ${REMOTE_DIRECTORY}/GameDataTranslateFileList_${L_LANG}.txt${NFS_EXTENSION} | sed "s|${REMOTE_DIRECTORY}/||" >> ${OUTPUT_FILE}
	cd ${REMOTE_DIRECTORY}
	for FILE in $(cat ${CHECK_FILE_LIST} | sort); do md5sum ${FILE}${NFS_EXTENSION} >> ${OUTPUT_FILE} ; done
fi

cp -arf ${OUTPUT_FILE} /home/fndev/www/LOCALIZE/
