#!/bin/sh

#
# create-document - create document using templates in templates/ directory
#
# Developed by Lubomir Host 'rajo' <rajo AT platon.sk>
# Copyright (c) 2003-2005 Platon SDG, http://platon.sk/
# Licensed under terms of GNU General Public License.
# All rights reserved.
#

# $Platon: platos/_scripts/create-document,v 1.15 2005/09/18 00:57:53 rajo Exp $

# Uncomment following 2 lines for debugging
#DEBUG="echo"
#NODEBUG=":"

DATE_SHORT="`date '+%y-%m-%d'`";
DATE_LONG="`date '+%Y-%m-%d'`";
DATE=$DATE_LONG;
YEAR="`date '+%Y'`";

PERCENT_SIGN="%" # only for testing sed
HERE=`pwd`

MYSELF="platos/_scripts/create-document"

dirname="$1"
filename="$2"
model="$3"
STYLESHEET="$4"
PREFIX="$5"
APPEND_DIRNAME="$6"

usage()
{ # {{{
	echo
	echo "Usage	[path] [filename] [naming schema] [stylesheet] [prefix] [append dirname?]"
	echo
	exit 1
} # }}}

generate_sed_cmd()
{ # {{{
	cmd=""
	for key in $*; do
		cmd="$cmd s%@$key@%"`echo "${!key}" | sed -e 's/%/\\\\%/g;' `"%g;"; 
	done
	echo "$cmd"
} # }}}

if [ -z "$TOP_DIR" ]; then
	echo '$TOP_DIR is empty, using current directory...'
	TOP_DIR="."
fi

# document path {{{
if [ -z "$dirname" ]; then
	echo -en "Enter name of directory for document withouth date string []: "
	read dirname
	if [ -z "$dirname" ]; then
		echo "Can't create document with empty name"
		exit 1;
	fi
fi
# }}}

# document filename {{{
if [ -z "$filename" ]; then
	echo -en "Enter name of file withouth date string and extension []: "
	read filename
	if [ -z "$filename" ]; then
		echo "Can't create document with empty name"
		exit 1;
	fi
fi
filename=${filename%.tex}

# create available options:
file=`basename $filename`
f_dir=`dirname $filename`

dirname="$dirname/$f_dir"
dirname=`$TOP_DIR/_scripts/path-cleanup "$dirname" | sed -e 's%^/%%g;'`

d_dir=`dirname  $dirname`
l_dir=`basename $dirname`

out=""
out_1="$d_dir/$DATE-$l_dir/$DATE-$file.tex"
out_2="$d_dir/$DATE-$l_dir/$file-$DATE.tex"
out_3="$d_dir/$DATE-$l_dir/$file.tex"
out_4="$d_dir/$l_dir/$DATE-$file.tex"
out_5="$d_dir/$l_dir/$file-$DATE.tex"
out_6="$d_dir/$l_dir/$file.tex"
# }}}

# which naming sheme? {{{
model=`echo $model | sed -e 's%^\(.\).*%\1%g;'`

which_out="out_$model"
out="${!which_out}"
# }}}

#
# Choose filename
# {{{
while [ -z "$out" ]; do

	out=""
	out_1="$d_dir/$DATE-$l_dir/$DATE-$file.tex"
	out_2="$d_dir/$DATE-$l_dir/$file-$DATE.tex"
	out_3="$d_dir/$DATE-$l_dir/$file.tex"
	out_4="$d_dir/$l_dir/$DATE-$file.tex"
	out_5="$d_dir/$l_dir/$file-$DATE.tex"
	out_6="$d_dir/$l_dir/$file.tex"

	echo "Available filename models:";
	echo "	[P] Prints *.tex files listing.";
	echo "	[D] Toggle date format"
	for model in 1 2 3 4 5 6; do
		which_out="out_$model"
		echo "	[$model] ${!which_out}"
	done

	echo -en "Please select action or choose filename model []: ";
	read model 

	# take first character from user input
	model=`echo $model | sed -e 's%^\(.\).*%\1%g;'`

	case "$model" in
		[pP])
			echo "-----<% Start of listing files %>-----"
			ls -1 $d_dir/$DATE-$l_dir/*.tex $d_dir/$l_dir/*.tex 2> /dev/null ;
			echo "-----<%  End of listing files  %>-----"
			;;
		[dD])
			echo -n "Changed date format from '$DATE' to";
			if [ "$DATE" = "$DATE_LONG" ]; then
				DATE=$DATE_SHORT;
			else
				DATE=$DATE_LONG;
			fi
			echo " '$DATE'.";
			;;
		[1-6])
			which_out="out_$model"
			out="${!which_out}"
			;;
		*)
			echo "Unsupported model: $model";
			;;
	esac
done
# }}}

dirname=`dirname   $out`
filename=`basename $out`

if [ -f "$out" ]; then
	echo
	echo "Document '$out' already exists, please use another name"
	exit 1;
fi

filename=${filename%.tex}
FILENAME=$filename

if [ -z "$STYLESHEET" ]; then
	echo -en "Please choose stylesheet [platon]: ";
	read STYLESHEET
	STYLESHEET="${STYLESHEET:-platon}"
fi

if [ -z "$PREFIX" ]; then
	echo -en "Please specify prefix of your documents if you wish to use it: ";
	read PREFIX
fi

if [ -z "$APPEND_DIRNAME" ]; then
	echo -en "Append directory name to output files? ";
	read APPEND_DIRNAME
fi

echo "Creating document '$out'"
$DEBUG mkdir -p "$dirname"

#
# Create recursive Makefiles, SUBDIRS, .cvsignore etc...
# {{{
pathdir=""
echo "  STEP 1: Creating directory structure:";
for DIRNAME in `echo "$dirname" | sed 's%^\./%%g; s%/% %g;'`; do
	$NODEBUG cd $DIRNAME
	updir="$pathdir"
	pathdir="$pathdir/$DIRNAME"
	TOP_DIR="$TOP_DIR/.."
	TEMPLATES="$TOP_DIR/_templates"
	STYLESHEET_DIR="$TOP_DIR/_stylesheets/$STYLESHEET" # is not the same STYLESHEET_DIR as in Rules.make

	for file in Makefile .cvsignore; do
		if [ -f "$file" ]; then
			echo "    File '.$pathdir/$file' already exists, skipping..."
		else
			echo "    Creating file '.$pathdir/$file'"
			$NODEBUG sed -e "`generate_sed_cmd APPEND_DIRNAME PREFIX STYLESHEET DATE YEAR TOP_DIR STYLESHEET_DIR DIRNAME FILENAME PERCENT_SIGN`" \
				"$TEMPLATES/$file" > "$file"
		fi
	done

	# check directory name is not already there
	if [ "` grep -E \"^$DIRNAME\$\" ../SUBDIRS 2>/dev/null `" ]; then
		echo "    Checking '$DIRNAME' in .$updir/SUBDIRS file.";
	else
		echo "    Adding '$DIRNAME' into .$updir/SUBDIRS file.";
		$NODEBUG echo "# Added by $MYSELF, $DATE_LONG" >> ../SUBDIRS;
		$NODEBUG echo "$DIRNAME" >> ../SUBDIRS;
	fi

done
# }}}

#
# now we are in directory, where document should by located
#

echo "  STEP 2: Creating document .$pathdir/$filename.tex.";
$NODEBUG sed -e "`generate_sed_cmd APPEND_DIRNAME PREFIX STYLESHEET DATE YEAR TOP_DIR STYLESHEET_DIR DIRNAME FILENAME PERCENT_SIGN`" \
	"$STYLESHEET_DIR/template.tex" > "$filename.tex"

echo "  STEP 3: Adding '$filename.pdf' into .$pathdir/pdf_OUTPUT file.";
$NODEBUG echo "# Added by $MYSELF, $DATE_LONG" >> pdf_OUTPUT;
$NODEBUG echo "$filename.pdf" >> pdf_OUTPUT;

echo "  STEP 4: Adding '$filename.ps' into .$pathdir/ps__OUTPUT file.";
$NODEBUG echo "# Added by $MYSELF, $DATE_LONG" >> ps__OUTPUT;
$NODEBUG echo "$filename.ps" >> ps__OUTPUT;

echo "  STEP 5: Adding '$filename.html' into .$pathdir/htm_OUTPUT file.";
$NODEBUG echo "# Added by $MYSELF, $DATE_LONG" >> htm_OUTPUT;
$NODEBUG echo "$filename.html" >> htm_OUTPUT;

echo "  STEP 6: Adding '$filename.rtf' into .$pathdir/rtf_OUTPUT file.";
$NODEBUG echo "# Added by $MYSELF, $DATE_LONG" >> rtf_OUTPUT;
$NODEBUG echo "$filename.rtf" >> rtf_OUTPUT;

echo "Now you have to add files in directory '.$pathdir' into CVS and commit them."

# required, if script is sourced by current shell
cd $HERE

# vim: ft=sh
# vim600: fdm=marker fdc=3 fdl=0

