#!/bin/tcsh -f
# buildRandomArticle: generate a "random article" 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_lib.txt";	# the master list of files (generated by makeLists)
set jsDir = ${site}/js;
set tmpFile = ${jsDir}/tmp.js;  
set jsFile = ${jsDir}/randomArticle.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 now the line count

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

function getRandomFilePath() {
	var ranFile=fileList[Math.floor(nFiles*Math.random())];
	return ranFile;
}
.
1i
// Random file picker
// Generated by $me on $today
.
w
q
EOT

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

echo "done."
