#!/bin/csh -f
# The shebang line, above

# --- syntax check --- #
if ($#argv != 1) then
	echo usage: `basename $0` \<file\>
	echo \(Generate admission emails\)
	exit 0
endif

# --- we will use a temporary file for each line of the data --- #
/bin/rm -rf /tmp/tmpline

#The following do the same thing
#cat admissions.csv | cut -d\, -f1
#cut -d\, -f1 admissions.csv

foreach x (`cat $1`)
	# --- get each line of the file into a temporary file --- #
	# --- cut works on a file, and not on a string! --- #
	echo $x > /tmp/tmpline
	set s_no = "`cut -d\, -f1 /tmp/tmpline`"
	# --- create a file for each candidate's letter, s_no.txt --- #
	/bin/rm -rf $s_no.txt
	/usr/bin/touch $s_no.txt
	# --- get the name --- #
	set name = "`cut -d\, -f2 /tmp/tmpline`"
	echo $name
	# --- get the category, decide on the DD amount --- #
	set category = "`cut -d\, -f3 /tmp/tmpline`"
	if ($category == "GEN") then
		set amount = "Rs.1000/-"
		echo $category "General" $amount
	else if ($category == RES) then
		set amount = "Rs.500/-"
		echo $category "Reserved" $amount
	endif
	# --- get the gender, decide on the hostel --- #
	set gender = "`cut -d\, -f4 /tmp/tmpline`"
	if ($gender == "M") then
		set salutation = "Mr."
		set hostel = "Girnar"
	else if ($gender == F) then
		set salutation = "Ms."
		set hostel = "Himadri"
	endif
	# --- get the email ID --- #
	set email_id = "`cut -d\, -f5 /tmp/tmpline`"

	# --- get data into the admisison letter file --- #
	echo "Dear" $salutation$name"," >> $s_no.txt
	echo "You will be accommodated in $hostel hostel" >> $s_no.txt
	echo "Please get a crossed DD for $amount" >> $s_no.txt
#	elm -s "Interview Letter" $email_id < $s_no.txt
	/bin/rm -rf /tmp/tmpline
end

