#!/bin/sh

version="=Version" # From debian.rules
license="# ${0} ${version} -- install/remove format/base files
# Copyright (C) 1995 Erick Branderhorst <branderh@debian.org>.

# This is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.

# This is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the file
# /usr/doc/copyright/GPL for more details."

stderr ()
{
    echo "$1" >/dev/stderr ;
}

usage="Usage: ${0} --install|--remove format
  -i|--install   Install a format/base file and related link
  -r|--remove    Remove a format/base file, related log file and link
  -h|--help|-V|--version|-L|--license       Show help/version/license"

ini=/usr/lib/texmf/ini
bin=/usr/bin

case $# in
  1 )
    case "${1}" in
      --version|-V )
        echo "${0} version ${version}"; exit 0 ;;
      --license|-L )
        echo "$license"; exit 0 ;;
      --help|-h|--install|-i|--remove|-r )
        echo "$usage"; exit 0 ;;
    esac ;;
  2 )
    case "${1}" in
      --install|-i )
        if [ -f ${ini}/${2}.initex ]; then
#	  if [ ${ini}/${2}.initex -nt ${ini}/${2}.fmt ]; then
	    echo -n "Rebuilding \`${2}' format ... " && \
            cd ${ini} && \
            yes|initex ${2}.initex >/dev/null && \
            cd ${bin} && \
            ln -sf virtex ${2} && \
	    echo "done" && \
            exit 0
#          else
#            echo "No need to rebuilt \`${2}' format"
#          fi
        elif [ -f ${ini}/${2}.inimf ]; then
          cd ${ini} && \
          yes|inimf ${2}.inimf >/dev/null && \
	  exit 0
        else
          stderr "Can't find ${2}.initex or ${2}.inimf in ${ini}"
          exit 1
        fi ;;
      --remove|-r )
        if [ -f ${ini}/${2}.fmt -o -f ${ini}/${2}.base -o \
             -f ${ini}/${2}.log -o -f ${bin}/${2} ]; then
          rm -rf ${ini}/${2}.{fmt,base,log} ${bin}/${2}
          echo "Removed ${2} format/base and related files"
          exit 0
        elif [ -f ${ini}/${2}.initex -o -f ${ini}/${2}.inimf ]; then
          stderr "Can't remove ${2} format/base or related files"
          exit 0 
        else
          stderr "Can't find ${2}.initex or ${2}.inimf in ${ini}"
          exit 1
        fi ;;	
    esac ;;
esac

stderr "Try $0 --help"; exit 1;
