#!/bin/sh
# Unix shell script for installing "pmx.tex" on systems with
# the teTeX distribution and MusixTeX installed.

# 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

# Find the TeX/MetaFont root directory. "kpsetool" is a teTeX-specific
# utility program.
if (kpsetool -v '$TEXMF' >/dev/null 2>&1)
then
    TEXMF=`kpsetool -v '$TEXMF'`
    if [ ! -d $TEXMF ]
    then
	echo "Can't locate TeX/MetaFont directory tree."
	exit 1
    fi
else
    echo "install.pmx works with the teTeX distribution only."
    exit 1
fi

# Now go there.
cd $TEXMF

# Check if there's already a version of pmx.tex installed.
if (kpsetool -w tex pmx.tex >/dev/null 2>&1)
then
    echo "pmx.tex already installed. Overwriting with new version."
    PMXTEX=`kpsetool -w tex pmx.tex`
    INSTDIR=`dirname $PMXTEX`
else
    # if pmx.tex isn't there, put it into the musixtex dir
    if (kpsetool -w tex musixtex.tex >/dev/null 2>&1)
    then
	# install to the MusiXTeX tex file directory
	MUSIXTEX=`kpsetool -w tex 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.tex $INSTDIR/pmx.tex
then
    chmod 644 $INSTDIR/pmx.tex
else
    echo "Error installing pmx.tex. Please install manually."
    exit 1
fi

# And update the ls-R database
texhash

# back to the PMX source directory
cd $PMXDIR
