#!/bin/sh

# This is the xdvi wrapper script for teTeX, version 0.2.
# Copyright Thomas Esser, 1998. Permission to distribute under the terms
# of the GNU general public license version 2 or later.

# This script sets some environment variables to make sure that xdvi's
# ressource file in $XDVIINPUTS/xdvi is read by xdvi.bin.
# Special care was taken to make this work for old R3, too. Therefore,
# we need to modify XAPPLRESDIR. If you are running R4 or later, you
# can set XUSERFILESEARCHPATH for user specific application default
# files. You cannot use XAPPLRESDIR for user specific application default
# files.

test -f /bin/sh5 && test -z "$RUNNING_SH5" \
  && test x"`(uname -s) 2>&1`" = xULTRIX \
  && { RUNNING_SH5=true; export RUNNING_SH5; exec /bin/sh5 $0 ${1+"$@"}; }
RUNNING_SH5=

test -f /bin/bsh && test -z "$RUNNING_BSH" \
  && test x"`(uname -s) 2>&1`" = xAIX \
  && { RUNNING_BSH=true; export RUNNING_BSH; exec /bin/bsh $0 ${1+"$@"}; }
RUNNING_BSH=

# -help and -version only work if they are the only options. Therefore,
# we cannot give the "-name xdvi" parameter in that case.
# SU: Actually, with current xdvik, they should work in any position; but
# we may keep this for compatibility with older versions and non-k xdvi.
have_basename=`basename foo/bar 2>&1 | grep -i 'not found'`
if [ "$have_basename"xxx = "xxx" ]; then
    BASE_NAME=`basename $0`
else
    BASE_NAME=`echo $0 | sed 's!.*/!!'`
fi

case "$#:$1" in
  1:-help|1:-version)
    NAMEOPT=;;
  *)
    NAMEOPT="-name $BASE_NAME";;
esac

#case "$#:$1" in
#  1:-help|1:-version)
#    NAMEOPT=;;
#  *)
#    NAMEOPT='-name xdvi';;
#esac


xdviappfile=`kpsewhich -progname=xdvi --format='other text files' XDvi`
if test -n "$xdviappfile"; then
  xdviappdir=`dirname "$xdviappfile"`
  xdviapppath="${xdviappdir}/%l_%t/%N:${xdviappdir}/%N"

  # For R3, we have to set XAPPLRESDIR.
  ### SU: removed, xdvik won't work with R3 anyway, and XAPPLRESDIR is
  ### for user customizations in current X
  ### XAPPLRESDIR="$xdviappdir"; export XAPPLRESDIR

  # For R4 or later, we have to set XFILESEARCHPATH, since XAPPLRESDIR might
  # be ignored (if XUSERFILESEARCHPATH is set)
  XFILESEARCHPATH="$xdviapppath:${XFILESEARCHPATH-%D}"; export XFILESEARCHPATH
fi

#xdviappfile=`kpsewhich -progname=xdvi --format='other text files' XDvi`
#if test -n "$xdviappfile"; then
#  xdviappdir=`dirname "$xdviappfile"`
#  xdviapppath="${xdviappdir}%N"
#
#  # For R3, we have to set XAPPLRESDIR.
#  XAPPLRESDIR="$xdviappdir"; export XAPPLRESDIR
#
#  # For R4 or later, we have to set XFILESEARCHPATH, since XAPPLRESDIR might
#  # be ignored (if XUSERFILESEARCHPATH is set)
#  XFILESEARCHPATH="$xdviapppath:${XFILESEARCHPATH-%D}"; export XFILESEARCHPATH
#fi

 RUNXDVIA="yes"
# export RUNXDVIA

echo "yes" > tmp@
latex=`which latex`
bindir=`
gawk '
BEGIN{}
{texloc=index(latex,"/latex")
 bindir=substr(latex,1,texloc-1)
 print bindir
}
END{} ' latex="$latex" tmp@ `

searchtexmf="y"
if test "$searchtexmf" = "y"; then
TEXMF=`kpsexpand '$TEXMF'|cut -d! -f 3 `
#VARTEXFONTS=`kpsexpand '$VARTEXFONTS' `
if test "$VARTEXFONTS" = ""; then
   VARTEXFONTS=`kpsexpand '$VARFONTS' `
   export VARFONTS
else
   export VARTEXFONTS
fi

echo "yes" > tmp@
texmfcnffile=`kpsexpand -w cnf texmf.cnf`
TEXMFCNF=`
gawk '
{texmfcnfloc=index(texmfcnffile,"/texmf.cnf")
 print substr(texmfcnffile,1,texmfcnfloc-1)
}' texmfcnffile=$texmfcnffile tmp@ `

echo $TEXMFCNF > tmp@
TEXMF=`
gawk '
{
  inline=$0
  gsub(/web2c/,"",inline)
  inline=substr(inline,1,length(inline)-1)
  print inline
}' tmp@ `

texmffile=`kpsexpand '$TEXMFDIST' `
if test "$texmffile" != ""; then
TEXMFDIR=` gawk '
   {lbr=index(texmffile,"{")
    sep=index(texmffile,",")
    dash=index(texmffile,"-")
    texmfpre=substr(texmffile,1,dash)
    if (lbr > 0)
       {texlive=substr(texmffile,lbr+1,sep-lbr-1) 
        texmf =texmfpre texlive
       }
    else
       {if (dash > 0)
           {texlive=substr(texmffile,dash+1) 
            texmf =texmfpre texlive
           } 
       }    
    print texmf
   }' texmffile=$texmffile tmp@ `
fi


if test "$TEXMFDIR" != "" -a -d "$TEXMFDIR"; then
   TEXMF=$TEXMFDIR
fi

fi



#TEXMFMAIN=`kpsexpand '$TEXMFMAIN'|cut -d! -f 1 `


  rm -f dvipdfm.dat nextrun@ newfile@no

  cp $TEXMF/ttfonts.map .
   
if test ! -s UnicodeBig.sfd; then
   ln -s $TEXMF/UnicodeBig.sfd UnicodeBig.sfd > /dev/null
fi

if test ! -s UBig5.sfd; then
   ln -s $TEXMF/UBig5.sfd UBig5.sfd > /dev/null
fi
if test ! -s big5.sfd; then
   ln -s $TEXMF/big5.sfd big5.sfd > /dev/null 
fi
if test ! -s Big5.sfd; then
   ln -s $TEXMF/Big5.sfd Big5.sfd > /dev/null 
fi
if test ! -s UBg5plus.sfd; then
   ln -s $TEXMF/UBg5plus.sfd UBg5plus.sfd > /dev/null
fi
if test ! -s UGB.sfd; then
   ln -s $TEXMF/UGB.sfd UGB.sfd > /dev/null
fi
if test ! -s EUC.sfd; then
   ln -s $TEXMF/EUC.sfd EUC.sfd > /dev/null
fi
if test ! -s unicode.sfd; then
   ln -s $TEXMF/unicode.sfd unicode.sfd > /dev/null
fi

  if test ! -s $HOME/".NotFirstRunDvi"; then
     echo " "; echo " "
     echo "    **********************************************"
     echo "    *  If have errors after running dvips,       *"
     echo "    *  please give command:                      *"
     echo "    *       updmap-sys                           *"
     echo "    *  when you are supper user, or give command:*"     
     echo "    *       updmap                               *"
     echo "    *  when you are not super user,              *"
     echo "    *  and then run dvips again.                 *"
     echo "    **********************************************"     
     echo "     " 
     echo "yes" > $HOME/".NotFirstRunDvi"
#     read xxxxxxxxxxxx
  fi   

# exec xdvi.bin $NAMEOPT ${1+"$@"}

#exit
  
if test -s $bindir/xdvi-motif.bin; then
   exec xdvi-motif.bin $NAMEOPT ${1+"$@"}
else
  if test -s $bindir/xdvi-xaw; then
     exec xdvi-xaw $NAMEOPT ${1+"$@"}
#     exec xdvi-xaw3d.bin $NAMEOPT ${1+"$@"}
  else
    if test -s $bindir/xdvi.bin; then
       exec xdvi.bin $NAMEOPT ${1+"$@"}
    else
      if test -s $bindir/xdvi-xaw3d.bin; then
          exec xdvi-xaw3d.bin $NAMEOPT ${1+"$@"}
      else   
          if test -s $bindir/xdvi-xaw3d; then
             exec xdvi-xaw3d $NAMEOPT ${1+"$@"}
          else   
             exec xdvi.bin $NAMEOPT ${1+"$@"}
          fi   
      fi 
    fi   
  fi  
fi   



rm *.sfd
if test -s ttfonts.map; then
   rm ttfonts.map
fi

rm -f tmp@