#!/bin/sh

# =======================================================================
# Installation Script for HFM - Hamster Font Manager
#
# $Date: 1998/11/10 14:01:58 $
# $Revision: 1.6 $
#
# 1998 The Hamster Project Team [EB]
# =======================================================================

# To install hfm on your system, just run this script (if possible, as
# 'root') and answer the questions in a sensitive way.
# No editing in this file is needed! (if it works...)

# =======================================================================

if [ ! -f lib/hfm.blank ]; then
  echo "HFM - Hamster Font Manager - Installation Script"
  echo ""
  echo "Missing file: lib/hfm.blank"
  echo ""
  exit 1
fi

HFMVERSION=`grep HFMVersion lib/hfm.blank | cut -f 3 -d " " | cut -f 2 -d \"`

echo "HFM v$HFMVERSION - Hamster Font Manager - Installation Script"
echo ""

# Some rational and reasonable defaults.

# Which echo to use...
ECHON=yes
if echo -n test | grep -- "-n" >/dev/null 2>&1; then
  ECHON=no
fi
echonn () {
  if [ "$ECHON" = "yes" ]; then
    echo -n "$*"
  else
    echo "$*\\c"
  fi
}

INSTALLDIR=/usr/local/lib/hfm
BINDIR=/usr/local/bin

INSTALL_PROG=./install-sh

# Find a Tk interpreter.

echonn "Finding tixwish... "

TIXWISH=`which tixwish4.1.8.0`
if [ ! -f "$TIXWISH" ]; then
  TIXWISH=`which tixwish4.1.8.1`
fi
if [ ! -f "$TIXWISH" ]; then
  TIXWISH=`which tixwish4.1.7.6`
fi
if [ ! -f "$TIXWISH" ]; then
  TIXWISH=`which tixwish4.1.7.6.1`
fi
if [ ! -f "$TIXWISH" ]; then
  TIXWISH=`which tixwish`
fi
if [ ! -f "$TIXWISH" ]; then
  echo ""
  echo "I can't find a Tk interpreter with Tix on your system"
  echo "This is needed in order to run hfm."
  exit 1
fi

# Test which version of TCL is included in the found tixwish
TCLREQUIRED=7.6
TIXERRMSG="wrong version"
TIXERROR=`echo "if {[info tclversion] < $TCLREQUIRED} { puts \"$TIXERRMSG:[info patchlevel]\" } ; exit" | $TIXWISH 2>&1`
if [ -n "$TIXERROR" ]; then
  echo "$TIXWISH - Error"
  echo ""
  FOUNDERR=`echo $TIXERROR | cut -f 1 -d ":"`
  if [ "$FOUNDERR" = "$TIXERRMSG" ]; then
    FOUNDVERS=`echo $TIXERROR | cut -f 2 -d ":"`
    echo "Your tixwish '$TIXWISH' includes TCL version $FOUNDVERS."
    echo "That's too old, hfm requires at least TCL $TCLREQUIRED to run!"
  else
    echo "Your tixwish in '$TIXWISH' will not start."
    echo "Error: $TIXERROR"
    echo ""
    echo "Make sure your tixwish interpreter works before"
    echo "running this setup again!"
  fi
  echo ""
  exit 1
fi

echo "$TIXWISH"

# Search if necessary tools are installed

echonn "Scanning for needed tools... "
PFBTOPS=`which pfbtops`
if [ ! -f "$PFBTOPS" ]; then
  echo "pfbtops tool not found!"
  echo ""
  echo "This is needed for hfm to work!"
  echo "The 'pfbtops' tool comes with the 'groff' package"
  exit 1
fi
echo "Found!"

# Find out where we want the system installed.

echonn "Enter the top level installation directory [$INSTALLDIR]: "
read INPUT_INSTALLDIR
if [ "$INPUT_INSTALLDIR" = "" ]; then
  INPUT_INSTALLDIR="$INSTALLDIR"
fi
INSTALLDIR=$INPUT_INSTALLDIR
# "~" is not supported for "$HOME" in /bin/sh
INSTALLDIR=`echo $INSTALLDIR | sed -e "/~/ s;;${HOME};g"`
mkdir -p "$INSTALLDIR" >/dev/null 2>&1
if [ ! -d "$INSTALLDIR" ]; then 
  echo "I can't create that directory: $INSTALLDIR"
  exit 1
fi
if [ ! -w "$INSTALLDIR" ]; then
  echo "I can't write to that directory: $INSTALLDIR"
  exit 1
fi

# Some directories we have to create.

FONTSDIR=${INSTALLDIR}/fonts
mkdir -p $FONTSDIR
if [ ! -d $FONTSDIR ]; then
  echo "I can't create the directory $FONTSDIR"
  exit 1
fi

echonn "Enter the directory where you want the program file [$BINDIR]: "
read INPUT_BINDIR
if [ "$INPUT_BINDIR" = "" ]; then
  INPUT_BINDIR="$BINDIR"
fi
BINDIR="$INPUT_BINDIR"
# "~" is not supported for "$HOME" in /bin/sh
BINDIR=`echo $BINDIR | sed -e "/~/ s;;${HOME};g"`
if [ ! -d "$BINDIR" ]; then 
  # Try to make that dir
  mkdir -p "$BINDIR" >/dev/null 2>&1
  if [ ! -d "$BINDIR" ]; then 
    echo "I can't create that directory: $BINDIR"
    exit 1
  fi
fi
if [ ! -w "$BINDIR" ]; then
  echo "I can't write to the directory $BINDIR"
  exit 1
fi

cd lib
echo "Installing files..."

echonn "- hfm starter... "
for file in hfm
do
  if [ -r $file.blank ]; then
    sed -e "/%TIXWISH%/ s;;$TIXWISH;g" \
        -e "/%INSTALLDIR%/ s;;$INSTALLDIR;g" \
        -e "/%HFMDIR%/ s;;$INSTALLDIR;g" \
        -e "/%FONTSDIR%/ s;;$FONTSDIR;g" \
        -e "/%BINDIR%/ s;;$BINDIR;g" ${file}.blank > $file
    $INSTALL_PROG -m 555 $file $BINDIR
    rm -f $file
  fi
  echo "$file"
done

echonn "- modules... "
for f in mod_*.tcl
do
  $INSTALL_PROG -m 444 $f $INSTALLDIR
  echonn "$f "
done
echo ""

# Search for required files in modules:

echonn "- misc files... "
for f in `grep -h "# REQUIRED-FILES" mod_*.tcl | cut -f 2 -d ":"`
do
  $INSTALL_PROG -m 444 $f $INSTALLDIR
  echonn "$f "
done
echo ""

# Execute other plugin related installation code:

echo "- plugin installation..."

grep -h INSTALL-CODE mod*.tcl | cut -f 2- -d ":" | (
while read line
do
  line=`echo $line | sed -e "/%TIXWISH%/ s;;$TIXWISH;g" \
        -e "/%INSTALLDIR%/ s;;$INSTALLDIR;g" \
        -e "/%HFMDIR%/ s;;$INSTALLDIR;g" \
        -e "/%FONTSDIR%/ s;;$FONTSDIR;g" \
        -e "/%BINDIR%/ s;;$BINDIR;g"`
  eval $line
done
)

echo "All done!"
