#!/bin/bash 
#
#  2007/04/08 kameda@iit.tsukuba.ac.jp
#  2007/04/18 kameda@iit.tsukuba.ac.jp
#

#
# Binaries and Scripts
#

nkfbin=nkf
awkbin=awk
awkscr=./trios-manager05.awk
awkcut=./trios-cutter.awk

if ! which $nkfbin 1>/dev/null 2>/dev/null ; then
	echo "$nkfbin not found. Operation cancelled."
	exit 1
fi
if ! which $awkbin 1>/dev/null 2>/dev/null ; then
	echo "$awkbin not found. Operation cancelled."
	exit 1
fi
if [ ! -f $awkscr ]; then
	echo "$awkscr not found. Operation cancelled."
	exit 1
fi
if [ ! -f $awkcut ]; then
	echo "$awkcut not found. Operation cancelled."
	exit 1
fi
echo "Binaries and Scripts check passed."

#
# Options
#

vf=0
optstr=""

while [ "$1" != "" ]
do
  case "$1" in
    a-ORIGINAL)
	## echo "aformat = ORIGINAL"
	optstr=${optstr}" -v aformat=ORIGINAL"
	;;
    a-FirstFamily)
	## echo "aformat = FirstFamily"
	optstr=${optstr}" -v aformat=FirstFamily"
	;;
    a-FirstFAMILY)
	## echo "aformat = FirstFAMILY"
	optstr=${optstr}" -v aformat=FirstFAMILY"
	;;
    a-FAMILYFirst)
	## echo "aformat = FAMILYFirst"
	optstr=${optstr}" -v aformat=FAMILYFirst"
	;;
    a-FamilyF.)
	## echo "aformat = FamilyF."
	optstr=${optstr}" -v aformat=FamilyF."
	;;

    t-ORIGINAL)
	## echo "tformat = ORIGINAL"
	optstr=${optstr}" -v tformat=ORIGINAL"
	;;
    t-TitleString)
	## echo "tformat = TitleString"
	optstr=${optstr}" -v tformat=TitleString"
	;;
    t-TITLESTRING)
	## echo "tformat = TITLESTRING"
	optstr=${optstr}" -v tformat=TITLESTRING"
	;;
    t-Titlestring)
	## echo "tformat = Titlestring"
	optstr=${optstr}" -v tformat=Titlestring"
	;;

    -h* | --h* )
	echo "(Help will come here.)"
	exit 1
	;;
    *)
	if [ ! -f $1 ]; then
		echo $0 'error : "'$1'" is not a regular file.'
		exit 1
	fi
	break;
	;;
  esac
  shift
done

if [ $# -ne 1 ]; then
	echo You need to specify just one db file.
	exit 1
fi

echo "options     = $optstr"
echo "source file = $1"
echo "rests       = $* (It should be one file only)"

case "$1" in
  *-edt.txt )
	## echo "imode = text"
	optstr=${optstr}" -v imode=text"
	;;
  *)
	## echo "imode = tab"
	optstr=${optstr}" -v imode=tab"
	;;
esac

echo "all options = $optstr"

#
#  Generating HTML files
#
#  templates:  index_{date,class}-{e,j}.tmpl
#  results  :  index_{date,class}-{e,j}.html

pubdb=__trios.txt

echo "en / date ..."
nkf -e -Lu $1 | awk $optstr -v vf=$vf -v lng=en -v key=date -f $awkscr > $pubdb
nkf -e -Lu index_date-e.tmpl  | awk -v pubdb=$pubdb -f $awkcut | nkf -s -Lw > index_date-e.html

echo "jp / date ..."
nkf -e -Lu $1 | awk $optstr -v vf=$vf -v lng=jp -v key=date -f $awkscr > $pubdb
nkf -e -Lu index_date-j.tmpl  | awk -v pubdb=$pubdb -f $awkcut | nkf -s -Lw > index_date-j.html

echo "en / type ..."
nkf -e -Lu $1 | awk $optstr -v vf=$vf -v lng=en -v key=type -f $awkscr > $pubdb
nkf -e -Lu index_class-e.tmpl | awk -v pubdb=$pubdb -f $awkcut | nkf -s -Lw > index_class-e.html

echo "jp / type ..."
nkf -e -Lu $1 | awk $optstr -v vf=$vf -v lng=jp -v key=type -f $awkscr > $pubdb
nkf -e -Lu index_class-j.tmpl | awk -v pubdb=$pubdb -f $awkcut | nkf -s -Lw > index_class-j.html

rm $pubdb

dstr=`date +%Y%m%d-%H%M%S`

echo "editable-text for backup ..."
nkf -e -Lu $1 | awk $optstr -v vf=$vf -v lng=jp -v key=date -v omode=text  -f $awkscr | nkf -s -Lw > pub-${dstr}-edt.txt

echo "tab-text for backup ..."
nkf -e -Lu $1 | awk $optstr -v vf=$vf -v lng=jp -v key=date -v omode=tab   -f $awkscr | nkf -s -Lw > pub-${dstr}-tab.txt

echo "tab-text for TRIOS ..."
nkf -e -Lu $1 | awk $optstr -v vf=$vf -v lng=jp -v key=date -v omode=trios -f $awkscr | nkf -s -Lw > pub-${dstr}-tri.txt

echo "done."


exit 0




