# Example setup file for ConTeXt distribution
#
# Author:  Hans Hagen
# Patches: Arthur R. & Mojca M.
#
# Usage:
#   . setuptex [texroot]
#
# On the first run also execute:
#   mktexlsr
#   texexec --make --alone

#
# PLATFORM
#

# we will try to guess the platform first
# (needs to be kept in sync with first-setup.sh and mtxrun)
# if yours is missing, let us know

system=`uname -s`
cpu=`uname -m`

case "$system" in
  # linux
  Linux)
    case "$cpu" in
      i*86) platform="linux" ;;
      x86_64|ia64) platform="linux-64" ;;
      *) platform="unknown" ;;
    esac ;;
  # Mac OS X
  Darwin)
    case "$cpu" in
      ppc*|powerpc|power*|Power*) platform="osx-ppc" ;;
      i*86) platform="osx-intel" ;;
      *) platform="unknown" ;;
    esac ;;
  # FreeBSD
  FreeBSD|freebsd)
    case "$cpu" in
      i*86) platform="freebsd" ;;
      x86_64) platform="freebsd" ;;
      *) platform="unknown" ;;
    esac ;;
  # SunOS/Solaris
  SunOS)
    case "$cpu" in
      sparc) platform="sun" ;;
      i86pc) platform="sunos-intel" ;;
      *) platform="unknown" ;;
    esac ;;
  *) platform="unknown"
esac

if test "$platform" = "unknown" ; then
  echo "Error: your system \"$system $cpu\" is not supported yet."
  echo "Please report to the ConTeXt mailing-list (ntg-context@ntg.nl)"
fi

#
# PATH
#

# this resolves to path of the setuptex script
OWNPATH=$(cd -P -- "$(dirname -- "$0")" && pwd -P)

# but one can also call
# . setuptex path-to-tree

TEXROOT=""
# first check if any path has been provided in the argument, and try to use that one
if [ $# -ne 0 ] ; then
	# TODO: resolve any errors
	ARGPATH=$(cd -P -- "$(dirname -- "$1")" && pwd -P) && ARGPATH=$ARGPATH/$(basename -- "$1")
	if test -f "$ARGPATH/texmf/tex/plain/base/plain.tex" ; then
		TEXROOT="$ARGPATH"
		echo "Setting \"$TEXROOT\" as TEXROOT."
	else
		echo "The argument \"$ARGPATH\" is not a valid TEXROOT path."
		echo "There is no file \"$ARGPATH/texmf/tex/plain/base/plain.tex\""

		if [ -f "$OWNPATH/texmf/tex/plain/base/plain.tex" ]; then
			TEXROOT="$OWNPATH"
			echo "Setting \"$TEXROOT\" as TEXROOT instead."
		fi
	fi
else
	if [ -f "$OWNPATH/texmf/tex/plain/base/plain.tex" ]; then
		TEXROOT="$OWNPATH"
		echo "Setting \"$TEXROOT\" as TEXROOT."
	else
		echo "\"$OWNPATH\" is not a valid TEXROOT path."
		echo "There is no file \"$OWNPATH/texmf/tex/plain/base/plain.tex\""
	fi
fi

if [ "$TEXROOT" != "" ]; then

export TEXROOT


# binaries & formats
export TEXMFOS=$TEXROOT/texmf-$platform

# base TeX files & fonts
export TEXMFMAIN=$TEXROOT/texmf
# ConTeXt
export TEXMFCONTEXT=$TEXROOT/texmf-context
# cache for LuaTeX
export TEXMFCACHE=$TEXROOT/texmf-cache

# user additions or modifications
export TEXMFLOCAL=$TEXROOT/texmf-local

# optional (by/for Hans)
export TEXMFFONTS=$TEXROOT/texmf-fonts
export TEXMFEXTRA=$TEXROOT/texmf-extra
export TEXMFPROJECT=$TEXROOT/texmf-project

# FIXME
export TEXMFVAR=$TMP/texmf-var
export VARTEXMF=$TEXMFVAR


unset CTXDEVTXPATH CTXDEVMPPATH CTXDEVMFPATH
unset CTXDEVPLPATH CTXDEVRBPATH CTXDEVPYPATH CTXDEVJVPATH

# the next lines fail on dec alpha os's, so there you need
# to comment them (bug traced down by kees van marle/martin
# corrino)

export HOMETEXMF=/nonexist

export TEXMFCNF='$TEXROOT/texmf{-local,-context,}/web2c'
export TEXMF='{$TEXMFPROJECT,$TEXMFFONTS,$TEXMFLOCAL,$TEXMFOS,$TEXMFCONTEXT,$TEXMFEXTRA,!!$TEXMFMAIN}'
export TEXMFDBS=$TEXMF

export TEXFORMATS='$TEXMFOS/web2c{/$engine,}'
export MPMEMS=$TEXFORMATS

unset TEXINPUTS MPINPUTS MFINPUTS

export PATH=$TEXMFOS/bin:$PATH
# TODO: OSFONTDIR

export RUBYLIB=$TEXMFCONTEXT/scripts/context/ruby:$RUBYLIB

# just to make sure that the binaries are executable
if ( (test -w "$TEXMFOS/bin/texmfstart") && (! test -x "$TEXMFOS/bin/texmfstart") ) ; then
    chmod 755 $TEXMFOS/bin/*
fi

export CTXMINIMAL=yes

else
	# TODO
	echo "provide a proper tex root (like '. setuptex /something/tex')" ;
fi

