#!/bin/bash

if [ $# -le 0 ] ; then
    echo ""
    echo "Usage: `basename $0` InstallationDirectory"
    echo "Install folder Tcl into specified installation directory"
    echo ""
    exit 1
fi

INSTDIR=$1
if [ "`dirname $INSTDIR`" = "." ] ; then
  INSTDIR=`pwd`/`basename $INSTDIR`
fi


if [ ! -d "${INSTDIR}" ]; then
    echo "Installation directory ${INSTDIR} does not exist."
    echo "Check name or create manually."
    exit 1
fi

if [ -d "${INSTDIR}/Tcl" ]; then
    echo "Tcl directory ${INSTDIR}/Tcl already exists."
    echo "Remove manually before next installation attempt."
    exit 1
fi

echo "Extracting Tcl into ${INSTDIR} ..."
match=$(grep --text --line-number '^PAYLOAD:$' $0 | cut -d ':' -f 1)
payload_start=$((match + 1))
tail -n +$payload_start $0 | (cd ${INSTDIR} && tar -xzf -)

echo ""
echo "Add the following lines to your shell resource file (ex. ~/.bashrc):"
echo "export PATH=\"${INSTDIR}/Tcl/bin:\$PATH\""
echo "export LD_LIBRARY_PATH=\"${INSTDIR}/Tcl/lib:\$LD_LIBRARY_PATH\""
echo "export TCLLIBPATH=\"${INSTDIR}/Tcl/lib \$TCLLIBPATH\""

exit 0
