# Author: Theodore Zacharia
# V0.1 - 06/10/2020 - needed to generate some addresses to fix an association issue
# possible performance improvement - preshuffle the large address template file

SRCADDRESSFILE=data/ukaddresses_template.csv
SRCTOWNFILE=data/ukaddresses_town.csv
SRCPOSTCODEFILE=data/ukaddresses_postcode.csv

if [ $# -lt 1 ]
then
	echo "usage: $0 count"
	exit 1
fi

MAXADDCT=$1
ADDCT=0

while [ $ADDCT -lt $MAXADDCT ]
do

	# get a random number within a range for street number
	ADDNUMBERPART=`shuf -i 1-1000 -n 1`
	# read random line from addresses

	# first part of the address can be empty
	ADDRESSIN_1=`shuf -n 1 $SRCADDRESSFILE`
	ADDPART1=`echo $ADDRESSIN_1 | cut -d, -f1`

	# middle part MUST have something
	ADDPART2=""
	while [ ! -n "$ADDPART2" ]
	do 
		ADDRESSIN_2=`shuf -n 1 $SRCADDRESSFILE`
		ADDPART2=`echo $ADDRESSIN_2 | cut -d, -f2`
	done

	# last part of the address can be empty for a % of the time
	ADDRESSIN_3=`shuf -n 1 $SRCADDRESSFILE`
	ADDPART3=`echo $ADDRESSIN_3 | cut -d, -f3`
	while [ ! -n "$ADDPART3" ]
	do 
		ADDRESSIN_3=`shuf -n 1 $SRCADDRESSFILE`
		ADDPART3=`echo $ADDRESSIN_3 | cut -d, -f3`
	done

	TOWN_1=`shuf -n 1 $SRCTOWNFILE`
	ADDTOWNPART=`echo $TOWN_1 | cut -d, -f1`

	POSTCODE_1=`shuf -n 1 $SRCPOSTCODEFILE`
	POSTCODE_2=`shuf -i 1-99 -n 1`
	POSTCODE_3=`shuf -i 1-9 -n 1`
	POSTCODE_4=`shuf -n 1 $SRCPOSTCODEFILE`
	ADDPOSTCODEPART="${POSTCODE_1}${POSTCODE_2} ${POSTCODE_3}${POSTCODE_4}"

	if [ -n "$ADDPART1" ]
	then
		echo "$ADDNUMBERPART $ADDPART1 $ADDPART2 $ADDPART3,$ADDTOWNPART,$ADDPOSTCODEPART"
	else
		echo "$ADDNUMBERPART $ADDPART2 $ADDPART3,$ADDTOWNPART,$ADDPOSTCODEPART"
	fi	

	# make up new address
	ADDCT=`expr $ADDCT + 1`
done

	
