#!/bin/bash

RED="\033[01;31m"
GREEN="\033[01;32m"
YELLOW="\033[01;33m"
MAGENTA="\033[01;35m"
CYAN="\033[01;36m"
WHITE="\033[01;37m"
NORMAL="\033[00m"

print_usage () {
	echo -e "${CYAN}Usage:${NORMAL}"
	echo -e "\t${GREEN}--help (-h)${NORMAL}\n\t\tPrints this help message\n"
	echo -e "\t${GREEN}--no-color (-n)${NORMAL}\n\t\tDisables coloured text output\n"
	echo -e "\t${GREEN}--verbose (-v)${NORMAL}\n\t\tPrint more status information\n"
	exit 0;
}

# Read a variable with length less than or equal to given value
# $1 - Variable name to set upon exit
# $2 - Message to display upon input request
# $3 - minimum length of string
# $4 - message to display if user input too short
get_var_minlength () {
	local var
	while [ -z "${var}" ]; do
		echo -ne "${2} ${WHITE}"; read var; echo -ne "${NORMAL}"
		if [ ${#var} -lt $3 ]; then echo -e "${RED}${4}${NORMAL}"; unset var; fi
	done
	export $1="${var}"
}

# Read a variable with unconstrained length
# $1 - Variable name to set upon exit
# $2 - Message to display upon input request
get_var () {
	local var
	echo -ne "${2} ${WHITE}"; read var; echo -ne "${NORMAL}"
	export $1="${var}"
}

# $1 - Variable name to set upon exit
# $2 - Message to display upon input request
# $3 - default value for zero-length user input
get_var_default () {
	local var
	echo -ne "${2} [$3] ${WHITE}"; read var; echo -ne "${NORMAL}"
	[ -z "${var}" ] && var="${3}"
	export $1="${var}"
}

HELP=
VEBOSE=
until [ -z "$1" ]; do case "$1" in
	-h|--help) HELP="true" ;;
	-n|--no-color|--no-colour) unset RED GREEN YELLOW MAGENTA CYAN WHITE NORMAL ;;
	-v|--verbose) VERBOSE="true" ;;
	*) echo -e "Unrecognised command line option: $1"; HELP="yes";;
esac; shift; done
[ "${HELP}" ] && print_usage

