#!/bin/ksh
#PHLOG WRITE V2.1
#Script to make phlog writing easier
#Parses today's date, calls up a text editor, once finished writing, updates gophermap and 
#then sets file permissions. Enables updates to be done later on the same day.
#Now updates the Gemini Capsule index.gmi file since V2.1

print "Welcome to the Phlog Writing Tool"

##############################################################

#Define Variables

DATE=$(date +%C%y%m%d) #sets date to YYYYMMDD
DATE_UK=$(date +%d%m%C%y) #sets date to DDMMYYYY 
DAY=$(date +%d) #determine day
MONTH=$(date +%m) #determine month
YEAR=$(date +%C%y) #determine year
PHLOGS=~/gopher/phlog/
PHLOGMAP=~/gopher/phlog/gophermap
GEMCAP=~/gopher/phlog/index.gmi

###############################################################

#Acquire a file description

echo What is the file description?
read DESCRIPT

###############################################################

#set nano to gopher writing mode

rm ~/.nanorc #remove old one
cp ~/scripts/nanorc_gopher ~/.nanorc 

###############################################################

#Create file, edit text

nano $PHLOGS$DATE.txt

###############################################################

#Sort the Gophermap file out

#print $MONTH
#Work out human readable title
case $MONTH in
		"01")
		 MONTH_TEXT="January"
			;;
		"02")
		MONTH_TEXT="February"
			;;
		"03")
		MONTH_TEXT="March"
			;;
		"04")
		MONTH_TEXT="April"
			;;
		"05")
		MONTH_TEXT="May"
			;;
		"06")
		MONTH_TEXT="June"
			;;
		"07")
		MONTH_TEXT="July"
			;;
		"08")
		MONTH_TEXT="August"
			;;
		"09")
		MONTH_TEXT="September"
			;;
		"10")
		MONTH_TEXT="October"
			;;
		"11")
		MONTH_TEXT="November"
			;;
		"12")
		MONTH_TEXT="December"
			;;
esac

#assemble the file name and titles from what has been entered
FILENAME=$DATE".txt"
HUMAN_TITLE="0"$DAY" "$MONTH_TEXT" "$YEAR" "$DESCRIPT
GEM_HUMAN_TITLE=$DAY" "$MONTH_TEXT" "$YEAR" "$DESCRIPT
TITLE=$HUMAN_TITLE'\t'$FILENAME
GEM_TITLE="=> "$FILENAME" "$GEM_HUMAN_TITLE

###############################################################

#Update gophermap

#Check for the title to avoid duplicates
grep $FILENAME -q ~/gopher/phlog/gophermap && FOUND="1" 
if [[ $FOUND = "1" ]];
then
	print "file already exists"
else
	#get text, print file contents, make file and then replace the old one
	(echo $TITLE && cat $PHLOGMAP) > test.txt && mv test.txt $PHLOGMAP
	#same again but for gemini capsule
	(echo $GEM_TITLE && cat $GEMCAP) > test1.txt && mv test1.txt $GEMCAP
fi

###############################################################

#Update main page gophermap to ensure we are shown as active

MAINGOPH=~/gopher/gophermap
(cat $MAINGOPH &&  echo ' ') > tea.txt && mv tea.txt $MAINGOPH

###############################################################

#go back to normal nano config

rm ~/.nanorc
cp ~/scripts/nanorc_text ~/.nanorc

###############################################################

#Set permissions

find ~/gopher/ -type f -print0 | xargs -0 chmod 644
find ~/gopher/ -type d -print0 | xargs -0 chmod 755

###############################################################

#This is the end, my only friend the end

print "phlog updated"

exit