#!/bin/sh
# Unix shell script for installing "pmx.tex" on systems with
# the teTeX distribution and MusixTeX installed.
# (J Gilbey <jdg@maths.qmw.ac.uk>, S Evert <evert@mathematik.uni-stuttgart.de>)

# The current working directory should be the PMX source root dir
PMXDIR=`pwd`
if [ ! -x $PMXDIR/install.pmx ]
then
    echo "install.pmx must be run from PMX source directory."
    exit 1
fi

# Which version of TeX are we using?
# We need one that provides the 'kpsewhich' tool.
if ( [ ! "`kpsewhich --version 2>&1`" ] ) 2>/dev/null
then
    # Can't run 'kpsewhich', so it's probably not there.
    echo
    echo "ERROR install.pmx works with web2c-based distributions of TeX only."
    echo "ERROR Please install manually."
    echo
    exit 1
else
    echo "Good. This is a web2c-based TeX distribution."
fi

# There seem to be a plethora of different kpathsea versions around,
# all of which act differently, in particular with respect to options
# and the command-line syntax in general. Here we try to take care of
# the differences. This is all Julian Gilbey's (brilliant) work.
KPSEVER="`kpsewhich --version 2>/dev/null | sed -n '/version/s/^.*ion //p'`"
case "$KPSEVER" in
  2.*) # Version 2.x -> might be NTeX
    echo "Kpathsea version 2.x  (might be NTeX)"
    # kpsewhich syntax is "kpsewhich <filename>"
    KPSEWHICH="kpsewhich";
    # Don't know if we have to go to the texmf root directory with
    # NTeX, but I'll keep it for now ... anyway we can get the texmf
    # root in this version of kpathsea with "kpsexpand tex '$TEXMF'"
    cd "`kpsexpand tex '$TEXMF'`";;
  3.*) # Version 3.x -> recent teTeX
    echo "Kpathsea version 3.x  (probably a recent teTeX version)"
    # kpsewhich syntax is "kpsewhich <filename>"
    KPSEWHICH="kpsewhich";;
  "")  # --version not supported -> probably old teTeX
    echo "Hmmm... looks like an old version of teTeX"
    # kpsewhich syntax is "kpsewhich <format> <filename>"
    KPSEWHICH="kpsewhich tex"; 
    # In some old versions of teTeX, we need to be in the root directory
    # of the texmf tree when we run 'texhash' in the end. In teTex < 0.9
    # we can just use the $TEXMF variable.
    cd "`kpsexpand '$TEXMF'`";;
  *)   # Ooops. We haven't come across this version before. 
    echo "Help\!  Don't know how to deal with this kpathsea version."
    exit 1;;
esac

# Check if there's already a version of pmx.tex installed.
if ($KPSEWHICH pmx.tex >/dev/null 2>&1)
then
    echo "pmx.tex already installed. Overwriting with new version."
    PMXTEX=`$KPSEWHICH pmx.tex`
    INSTDIR=`dirname $PMXTEX`
    rm -f $PMXTEX
else
    # if pmx.tex isn't there, put it into the musixtex dir
    if ($KPSEWHICH musixtex.tex >/dev/null 2>&1)
    then
        # install to the MusiXTeX tex file directory
        MUSIXTEX=`$KPSEWHICH musixtex.tex`
        INSTDIR=`dirname $MUSIXTEX`
    else
        echo "Can't find MusiXTeX installation. Aborting."
        exit 1
    fi
fi

# Now install pmx.tex
echo "Installing pmx.tex to $INSTDIR"
if cp $PMXDIR/pmx-orig/pmx.tex $INSTDIR/pmx.tex
then
    chmod 644 $INSTDIR/pmx.tex
else
    echo
    echo "ERROR installing pmx.tex. Please install manually."
    echo
    exit 1
fi

# And update the ls-R database
texhash || (
    # Either we don't have texhash in this TeX distribution or
    # it didn't work. We'll just tell the users they have to figure
    # it out for themselves.
    echo
    echo "WARNING texhash didn't work. You'll probably have to update"
    echo "WARNING your ls-R database manually. Please refer to the documentation"
    echo "WARNING supplied with your LaTeX distribution."
    echo
)

# back to the PMX source directory
cd $PMXDIR
