#!/bin/bash -e
#
# Simple script for generating cslatex format file without problems.
# Copyright (C) 1996, 1997 Milan Zamazal
# Changes: (C) 1998 Petr Spatka
# Copying and warranty: GPL2


# Check number of parameters

if [ $# -eq 0 ]; then
  echo "usage: $0 US | BR [ CZ ] [ CZ-T1 ] [ SK ] [ SK-T1 ]"
  exit -1
elif [ $# -gt 4 ]; then
  echo "$0: at most 4 parameters can be specified"
  exit -1
fi


# Check values of parameters

EN=
CZ=
SK=

while [ -n "$1" ]; do
  case $1 in
    US|us)
      if [ -n "$EN" ]; then
	echo "$0: US and BR cannot be both specified"
	exit -1
      fi
      EN=US
      ;;
    BR|br)
      if [ -n "$EN" ]; then
	echo "$0: US and BR cannot be both specified"
	exit -1
      fi
      EN=BR
      ;;
    CZ|cz)
      if [ -z "$CZ" ]; then
        CZ=CZIL2
      elif [ "$CZ" = "CZT1" ]; then
        CZ=CZILT
      else
        echo "$0: warning: duplicate option: CZ"
      fi
      ;;
    CZ-T1|cz-t1)
      if [ -z "$CZ" ]; then
        CZ=CZT1
      elif [ "$CZ" = "CZIL2" ]; then
        CZ=CZILT
      else
        echo "$0: warning: duplicate option: CZ-T1"
      fi
      ;;
    SK|sk)
      if [ -z "$SK" ]; then
        SK=SKIL2
      elif [ "$SK" = "SKT1" ]; then
        SK=SKILT
      else
        echo "$0: warning: duplicate option: SK"
      fi
      ;;
    SK-T1|sk-t1)
      if [ -z "$SK" ]; then
        SK=SKT1
      elif [ "$SK" = "SKIL2" ]; then
        SK=SKILT
      else
        echo "$0: warning: duplicate option: SK-T1"
      fi
      ;;
    *)
      echo "$0: possible patterns are: US BR CZ CZ-T1 SK SK-T1"
      exit -1
  esac
  shift
done


# Create temporary working directory

LATEXDIR=/usr/share/texmf/tex/latex/cslatex
MYTMPDIR=${TMPDIR:=/tmp}/cslatex.$$
trap "rm -r $MYTMPDIR" EXIT INT QUIT TERM
mkdir $MYTMPDIR


# Create proper hyphen.cfg

awk -v PATTERNS="$EN $CZ $SK" \
' BEGIN { IN=0 }
  /^---pdm---/ { if(IN) { IN=0 }
                 else { IN=1; GET=(PATTERNS~substr($0,10)) }
		 next
	       }
  IN && !GET { next }
  { print }' \
  $LATEXDIR/cslatex-hyphen.cfg >$MYTMPDIR/hyphen.cfg


# Make format file

export TEXINPUTS=$MYTMPDIR/:/usr/share/texmf/tex/latex//:/usr/share/texmf/tex/generic//
initex latex.ltx

