#!/bin/sh
#
#  weblink.sh, a bash script by duus made on Tue Jun 26 20:51:13 EDT 2007 
#  lives in: ~/Documents/Software/unixscripts//web/weblink.sh 
#  run binedit weblink to edit.  see binedit help for more information
#
_weblinkdir=~/Documents/Software/unixscripts/web/weblink/
_SCRIPT_NAME=weblink       
_a2folder=~/Sites/a2/main_dir
_upa2=main
_linxfile=mylinx.html
##################################################### VERSION INFO
#v simple weblink page maker, v 0.1, by duus
#v
#v 	  type <weblink help> for more information
#v
###################################################### INSTALL INFO
#i How to install weblink v. 0.1 by duus
#i  1. don't.
##################################################### SETTINGS
# Please enter the directory where you are keeping the weblink script here:
_SCRIPT_LOCATION=~/bin/
#
#
##################################################### HELP/USAGE
#h Usage of weblink cli v. 0.1 by duus
#h  type weblink <args> at the prompt
#h  where args are:
#
#
_getnext=0
_make=0
_up=0
_fire=0
_skip=0
_rm=0
_edit=0
_update=1
# if no arguments:
if [ "$1" = "" ]
	then
	weblink help
	_skip=1
fi
#
##### main loop
# change to _grabnext
#  if [ grabnext = 1 ]; then ; __next = ${arg}; <insert what to do with it> ; else <everything else>

for arg in "$@"
	do

if [ ${_getnext} -eq 1 ]
		then
		_nextword=${arg}
		case "${_mode}" in
		     'link')
 				echo "${_mode} = ${_nextword}"
				_link=${_nextword}
				;;
			 'title')
 				echo "${_mode} = ${_nextword}"
				_title=${_nextword}
				;;
			'phrase')
 				echo "${_mode} = ${_nextword}"
				_phrase=${_nextword}
				;;
			'a2folder')
				 echo "${_mode} = ${_nextword}"
				 _a2folder=${_nextword}
				;;
			'upa2')
				 echo "${_mode} = ${_nextword}"
				 _upa2=${_nextword}
				;;
			esac
		_getnext=0
		else
#h	
if [ "$arg" = "l" -o "$arg" = "link"  ]
 		then
#h    <l>ink X will make X into the link (default=clipboard)
		_mode=link
		_getnext=1
		fi
	
if [ "$arg" = "t" -o "$arg" = "title"   ]
 		then
#h    <t>itle Y  will set the title to Y (required)
		_mode=title
		_getnext=1
		fi

if [ "$arg" = "p" -o "$arg" = "phrase"   ]
 		then
#h    <p>hrase Z  will set the phrase to Z (default=title)
		_mode=phrase
		_getnext=1
		fi

if [ "$arg" = "a" -o "$arg" = "a2folder" ]
 		then
#h    <a>2folder X will specify a non-default a2folder
		_mode=a2folder
		_getnext=1
		fi


if [ "$arg" = "s" -o "$arg" = "skip"   ]
 		then
#h    <s>kip will skip making the page
		_skip=1
		_update=0
		fi


if [ "$arg" = "u" -o "$arg" = "up"   ]
 		then
#h    <u>p will run "a2out main up" afterwards
		_up=1
		fi

if [ "$arg" = "upa2" -o "$arg" = "upa2tag" ]
 		then
#h    <upa2>ommand X will specify a non-default a2out <X> command
		_mode=upa2
		_up=1
		_getnext=1
		fi		

if [ "$arg" = "f" -o "$arg" = "firefox" ]
 		then
#h    <f>irefox will open the site
		_fire=1
		fi
				
if [ "$arg" = "r" -o "$arg" = "remove" -o "$arg" = "rm" ]
 		then
#h    <r>emove will remove the site
		_rm=1
		_skip=1 
		_update=0
		#also skip making it...
		fi
		
if [ "$arg" = "u" -o "$arg" = "update" ]
 		then
#h    <u>pdate will only update the linx page
		_skip=1
		_update=1 
		#also skip making it...
		fi				



if [ "$arg" = "e" -o "$arg" = "edit" ]
 		then
#h    <e>dit will edit the page in textmate
		_edit=1 
		fi
################################################# information commands
		
_info=0
#h 
#h _weblink information_		

		if [ "$arg" = "v" -o "$arg" = "version" ]
			then
#h    <(v)ersion> displays version information
			grep "^#v" $_SCRIPT_LOCATION/${_SCRIPT_NAME} | more
			_info=1
			_skip=1
			_update=0
		fi
		if [ "$arg" = "h" -o "$arg" = "help" ]
			then
#h    <(h)elp> displays this help file
			grep "^#h" $_SCRIPT_LOCATION/${_SCRIPT_NAME} | more
			_info=1
			_skip=1
			_update=0
		fi
		if [ "$arg" = "src" -o "$arg" = "source" -o "$arg" = "code" ]
			then
#h    <src> displays the source code
			more $_SCRIPT_LOCATION/${_SCRIPT_NAME}
			_info=1
			_skip=1
			_update=0
		fi

	fi # getnext ender
done

## now make page

if [ "${_title}" = "" -a ${_info} -ne 1 -a ${_skip} = 0 ]
	then
	echo "error: no title"
	echo " "
	weblink help
	exit 1;
fi

if [ "${_link}" = "" ]
	then
	#if no link then take from clipboard
	_tmp=`pbpaste`
	_link=${_tmp}
fi

if [ "${_phrase}" = "" ]
	then
	#if no phrase then take title
	_phrase=${_title}
fi

if [ ${_skip} -eq 0 ]
	then	 
	############################  make folder and page from info
	mkdir ${_a2folder}/${_title}
	cd ${_a2folder}/${_title}
	cp ${_weblinkdir}/default.html index.html
	_date=`date`
	echo "<"${_link}">"
	perl -i -pe "s/PHRASE/${_phrase}/g" index.html
	perl -i -pe "s/TITLE/${_title}/g" index.html
	perl -i -pe "s/DATE/${_date}/g" index.html
	perl -i -pe "s#THISLINK#${_link}#g" index.html
	more index.html
	touch WEBLINK.txt
fi

if [ ${_update} -eq 1 ]
	then
	######################  make a new links page
	_olddir=`pwd`
	cd ${_a2folder}
	echo "<html><head><title>linx</title></head><body><tt><ol>" > ${_linxfile}
	for dir in `ls -p | grep "/"`
		do
		_thisdir=`echo $dir | perl -i -pe 's#/##g' | perl -i -pe 's/\n//g'`
		if [ -e "${_thisdir}/WEBLINK.txt" ]
			then
			echo "   <li><a href=\"${_thisdir}\">${_thisdir}</a>" >> ${_linxfile}
		fi
	done
	echo "</ol><p>.<p>.<p>updated: `date`</body>" >> ${_linxfile}
	#cat ${_linxfile}
	cd ${_olddir}
	echo "updated page mylinx.html"
	##########################################
fi

if [ ${_rm} -eq 1 ]
	then
	cd ${_a2folder}
	if [ -e ${_title} ]
		then
		echo ""
		echo " rm `pwd`/$_title"
		echo -n "Confirm: [Y]/n : "
		read confirm
		if [ "$confirm" != "n" -a "$confirm" != "no" ]
			then
			_archivefolder=~/Documents/Software/unixscripts/web/weblink/archive/
			cp -rv ${_title} ${_archivefolder}/
			echo "looking in directory: `pwd`"
			rm -rv ${_title}
			if [ -e ~/Sites/a2/acct/public_html/${_title}/index.html ]
				then
				cd ~/Sites/a2/acct/public_html/${_title}
				echo "looking in directory: `pwd`"
				echo "<html></html>" > index.html
				more index.html
				rm -v WEBLINK.txt
				cd ${_a2folder}
			fi
			else
			echo "removal of ${_title} skipped."
		fi
	fi
fi



if [ ${_edit} -eq 1 ]
	then
	cd ${_a2folder}/${_title}
	echo "edit ${_a2folder}/${_title}/index.html"
	echo "${_SCRIPTNAME} will not advance until the file is closed in textmate..."
	mate -w index.html
fi

if [ ${_fire} -eq 1 ]
	then
	echo "firethis: ${_a2folder}/${_title}/index.html"
	cd ${_a2folder}/${_title}
	firethis index.html
fi

if [ ${_up} -eq 1 ]
	then
	a2out ${_upa2} up
fi


 
# 
# 
# end weblink, ~/Documents/Software/unixscripts//web/weblink.sh

