#!/bin/tcsh -f
# buildRandomSutta: generate a "random sutta" javascript for the site
set me=$0:t             		# the name of this script
set today = `date`;

set site="~/Sites/ati.svn"	# top level dir of the site
set list_dir="tech/lists/";	# where the file lists reside
set srcFile = "${site}/tech/lists/URL_tipitaka.txt";	# the master list of sutta files (generated by makeLists)
set jsDir = ${site}/js;
set tmpFile = ${jsDir}/tmp.js;  
set jsFile = ${jsDir}/randomSutta.js;  # where the script lives

echo -n "${me}: Creating file to edit...";
cp $srcFile $tmpFile;   #copy the raw file list

set nFiles = `wc -l $tmpFile`; # count the lines in the file (wc returns count and filename)
set nFiles = $nFiles[1];	# nFiles is the line count

echo -n "editing...";
# edit the tmpFile to turn it into javascript
ed -s $tmpFile << EOT
%s/^/	"/
%s/\$/",/
\$s/,//
1i
var suttaList=new Array(
.
\$a
);
var nFiles=$nFiles;

function getRandomSuttaPath() {
	var ranSutta=suttaList[Math.floor(nFiles*Math.random())];
	return ranSutta;
}
.
1i
// Random sutta picker
// Generated by $me on $today
.
w
q
EOT

echo -n "installing..."
mv $tmpFile $jsFile

echo "done."
