#!/bin/bash
#
#  Created by duus on 2007-08-02.
# 
if [ "$1" = "version" -o "$1" = "help" ]
	then
	echo "binpost SCRIPT SCRIPTDIR \"phrase\" posts that webpage"
	exit 1;
fi
_maindir=`unixscriptsfolder`
if [ "$1" = "nohelp" ]
	then
	_nohelp=1
	shift
	else
	_nohelp=0
fi
if [ "$1" = "noversion" ]
	then
	_noversion=1
	shift
	else
	_noversion=0
fi
if [ "$1" = "skip" ]
	then
	_skip=1
	shift
	else
	_skip=0
fi
_thisscript=$1
_thisdir=$2
_thisphrase=$3
_webscripts=~/Documents/PPL/duus/web/scripts/
_scripts_webpage=index.html
_header_page=~/Documents/PPL/duus/avatars/txt/duuspage_header.html
_footer_page=~/Documents/PPL/duus/avatars/txt/duuspage_footer.html
#
########################################## make a page for tmp
# 0. get phrase
if [ "${_thisdir}" = "" ]
	then
	_thisdir=${_thisscript}
fi

if [ "${_thisphrase}" = "" ]
	then
	if [ -e ${_maindir}/${_thisdir}/${_thisscript}phrase.txt ]
		then
		_thisphrase=`cat ${_maindir}/${_thisdir}/${_thisscript}phrase.txt`
		else
		echo "please input catchphrase."
		read -e _thisphrase
	fi
fi
# update phrase file
echo -n "${_thisphrase}" > ${_maindir}/${_thisdir}/${_thisscript}phrase.txt

# 1. make folder in webscripts
#
if [ ${_skip} -eq 0 ]
	then
	cd ${_webscripts}
	if [ ! -e ${_thisdir} ]
		then
		mkdir ${_thisdir}
	fi
	cd ${_thisdir}
	if [ ! -e index.html ]
		then
		echo "<html><head><title>404 Not Found</title></head><body><h1>404 File Not Found</h1><a href=\"..\">back</a></body>" > index.html
	fi
	if [ ! -e ${_thisscript} ]
		then
		mkdir ${_thisscript}
	fi
	cd ${_thisscript}
	#
	# 2. put script in that folder
	#
	cp ${_maindir}/${_thisdir}/${_thisscript}.sh .
	perl -i -pe 's#~#~#g' ${_thisscript}.sh
	perl -i -pe 's#duus#duus#g' ${_thisscript}.sh
	#
	# 3. make a cover page for that folder
	echo "...making page ${_maindir}/${_thisdir}/${_thisscript}/index.html"
	echo "  ${_thisscript}: ${_thisphrase}"
	echo "<html><head><title>${_thisscript} - a duusscript</title></head><body><tt><h1>${_thisscript}: ${_thisphrase}</h1><p>" > index.html
	if [ ${_noversion} -eq 0 ]
		then
		echo "<b>version information:</b><p>"  >> index.html
		${_maindir}/${_thisdir}/${_thisscript}.sh version | perl -i -pe 's#~#~#g' | perl -i -pe 's#duus#duus#g'  |  perl -i -pe 's/</&lt;/g' |  perl -i -pe 's/>/&gt;/g'| perl -i -pe 's/#v/<br>/g' >> index.html
	fi
	if [ ${_nohelp} -eq 0 ]
		then
		echo "<p><b>help information:</b><p>" >> index.html
		${_maindir}/${_thisdir}/${_thisscript}.sh help | perl -i -pe 's#~#~#g' | perl -i -pe 's#duus#duus#g'  |  perl -i -pe 's/</&lt;/g' |  perl -i -pe 's/>/&gt;/g'  | perl -i -pe 's/#h/<br>/g' >> index.html
	fi
	echo "<p><a href=\"${_thisscript}.sh\">(download script)</a><p>" >> index.html
	echo "<p> to install: <ol><li>copy script into a folder on your path.<li>type chmod +x ${_thisscript}.sh to make it executable" >> index.html
	echo "</ol><p><hr>">>index.html
	echo "<i> a <a href=\"http://thedu.us/scripts/\">duusscript</a>... This page updated: `date`, ">>index.html
	echo "when duus was feeling `getmood`.</i>" >> index.html
	echo "</body></html>" >> index.html
	echo "${_thisphrase}" > phrase.txt
fi
# 4. make new scripts page
##################### make new scripts page
_olddir=`pwd`
echo "...remaking ${_webscripts}/${_scripts_webpage}"
cd ${_webscripts}
#
_casevar=`jot -r 1 1 4` # Random selection of header
echo "choosing header ${_casevar}.  what?"
case "${_casevar}" in
	'1' ) # case 
	echo "<html><head><title> d^_^bscripts &amp; duusscripts</title></head><body><tt>" > ${_webscripts}/${_scripts_webpage}
    	;;
    '2' ) # case 
	echo "<html><head><title>(error 425) unordered collection of duusscripts produced on `nicedate`</title></head><body><tt>" > ${_webscripts}/${_scripts_webpage}
         ;;
	'3' ) # case 
	
	echo "<html><head><title>  d^_^bscripts &amp; duusscripts, while listening to `itunes get artist`</title></head><body><tt>" > ${_webscripts}/${_scripts_webpage}
    	;;
    '4' ) # case 
	echo "<html><head><title>(error 425) unordered collection of d^_^bscripts &amp; duusscripts</title></head><body><tt>" > ${_webscripts}/${_scripts_webpage}
         ;;
	*) # default case
		echo "not found: ${_casevar}"
		;;
esac
#
echo "<blockquote><blockquote><blockquote>&nbsp;<p>" >> ${_webscripts}/${_scripts_webpage}
if [ -e ${_header_page} ]
	then 
	cat ${_header_page} >> ${_webscripts}/${_scripts_webpage}
	else
	echo "Note: ${_header_page} does not exist"
fi
echo "<h2>duusscripts </h2>" >> ${_webscripts}/${_scripts_webpage}
echo "<ol>" >> ${_webscripts}/${_scripts_webpage}
for dir in `ls -p | grep "/"`
        do
        _adir=`echo $dir | perl -i -pe 's#/##g' | perl -i -pe 's/\n//g'`
        #echo $_adir
        echo "   <li><b>${_adir}<b><ul>" >> ${_webscripts}/${_scripts_webpage}
		cd ${_adir}
		for _scriptpage in `ls -p | grep "/"`
			do
			_ascriptpage=`echo $_scriptpage | perl -i -pe 's#/##g' | perl -i -pe 's/\n//g'`
			#echo ${_ascriptpage}
			cd ${_ascriptpage}
			echo "   <li><i><a href=\"${_adir}/${_ascriptpage}\">${_ascriptpage} info page</a></i>  (<a href=\"${_adir}/${_ascriptpage}/${_ascriptpage}.sh\">download script</a>)<br>" >> ${_webscripts}/${_scripts_webpage}
			if [ -e phrase.txt ]
				then
				cat phrase.txt >> ${_webscripts}/${_scripts_webpage}
			fi
			cd ..
		done
		cd ..
		echo "</ul>" >> ${_webscripts}/${_scripts_webpage}
done
echo "</ol><p>&nbsp;<p>&nbsp;<p>updated: `date`, when duus was feeling `getmood`. &nbsp;&nbsp;  <img height=10 width=15 src=\"http://thedu.us/imgs/fl_icon.gif\"><p>&nbsp;<p>">> ${_webscripts}/${_scripts_webpage}
echo "<p>&nbsp;<p>&nbsp;<p>">> ${_webscripts}/${_scripts_webpage}

for _file in `ls`
	do
	if [[ "${_file}" = *e* ]]
		then
		_thischar=:
		else
		_thischar=.
	fi
	echo "  TAIL: ${_file}-> thischar ${_thischar}  ...  *= duus "
	echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" >> ${_webscripts}/${_scripts_webpage}
	echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; " >> ${_webscripts}/${_scripts_webpage}
	echo " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; "  >> ${_webscripts}/${_scripts_webpage}
	echo " &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;${_thischar}<br>" >> ${_webscripts}/${_scripts_webpage}
done

if [ -e ${_footer_page} ]
	then 
	cat ${_footer_page} >> ${_webscripts}/${_scripts_webpage}
	else
	echo "Note: ${_footer_page} does not exist, you `getname`."
fi
echo " </tt></blockquote></blockquote></blockquote></body>" >> ${_webscripts}/${_scripts_webpage}
#cat ${_scripts_webpage}
cd ${_olddir}
echo open ${_webscripts}/${_scripts_webpage}
runlater 1 firethis file://${_webscripts}/${_scripts_webpage} &
##################################################
#
