#!/bin/bash

# An application used to replace all instances of <tt>, <code>, and <console> in a wiki page
# with their corresponding updated tags.

FILE_TO_CONVERT="toConvert"

ERR_CANNOT_EXEC=1

while getopts :c: option; do
	case ${option} in
		c)		FILE_TO_CONVERT="${OPTARG}";;
	    \?)		echo "Invalid option: '${OPTARG}'"; exit ${ERR_CANNOT_EXEC};;
		:)		echo "The option '${OPTARG}' requires an argument."; exit ${ERR_CANNOT_EXEC};;
	esac
done

replaceCharacters() {
	echo "Replacing all instances of <tt> and </tt> with {{c| and }}, respectively."
	sed -i -e 's;<tt>;{{c|;g' -e 's;</tt>;}};g' ${FILE_TO_CONVERT}

	echo "Replacing all instances of <code> and </code> with {{c| and }}, also."
	sed -i -e 's;<code>;{{c|;g' -e 's;</code>;}};g' ${FILE_TO_CONVERT}

	echo "Replacing all instances of <console> and </console> with {{console|body= and }}."
	sed -i -e 's;<console>;{{console|body\=;g' -e 's;</console>;}};g' ${FILE_TO_CONVERT}

	echo "Replacing all instances of '# ##i##' with '###i## '."
	sed -i -e 's;# ##i##;###i## ;g' ${FILE_TO_CONVERT}

	echo "Replacing all instances of  '$ ##i##' with '$##bl## '."
	sed -i -e 's;$ ##i##;$##bl## ;g' ${FILE_TO_CONVERT}
}

# Run the functions
if [ -r "${FILE_TO_CONVERT}" ]; then
	replaceCharacters
else
	echo "The file that you entered for the '-c' argument does not exist."
	exit ${ERR_CANNOT_EXEC}
fi
