# Example setup file for ConTeXt distribution
#
# Author:  Hans Hagen
# Patches: Arthur R. & Mojca M.
# (t)csh version: Alan B.
#
# Usage :
#   source setuptex.csh [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

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

switch ( $system )
  # linux
  case Linux:
    switch ( $cpu )
      case i*86:
        set platform="linux"
        breaksw
      case x86_64:
      case ia64:
        set platform="linux-64"
        breaksw
      case ppc:
        set platform="linux-ppc"
        breaksw
      default:
        set platform="unknown"
    endsw
    breaksw
  # Mac OS X
  case Darwin:
    switch ( $cpu )
      case ppc*:
      case powerpc:
      case power*:
      case Power*:
        set platform="osx-ppc"
        breaksw
      case i*86:
        set platform="osx-intel"
        breaksw
      default:
        set platform="unknown"
    endsw
    breaksw
  # FreeBSD
  case FreeBSD:
  case freebsd:
    switch ( $cpu )
      case i*86:
        set platform="freebsd"
        breaksw
      case x86_64:
        set platform="freebsd"
        breaksw
      default:
        set platform="unknown"
    endsw
    breaksw
  # SunOS/Solaris
  case SunOS:
    switch ( $cpu )
      case sparc:
        set platform="sun"
        breaksw
      case i86pc:
        set platform="sunos-intel"
      default:
        set platform="unknown"
    endsw
    breaksw
  # Other
  default:
    set platform="unknown"
endsw

if ( $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).
endif

#
# PATH
#

# this resolves to path of the setuptex script
# We use $0 for determine the path to the script, except for bash and (t)csh where $0
# always is bash or (t)csh.

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

# first check if any path has been provided in the argument, and try to use that one
if ( $# > 0 ) then
	set ownpath=$1
else
	# $_ should be `history -h 1` but doesn't seem to work...
	set cmd=`history -h 1`
	if ( $cmd[2]:h == $cmd[2]:t ) then
		set ownpath=$cwd
	else
		set ownpath=$cmd[2]:h
	endif
	unset cmd
endif

if ( -f "$ownpath/texmf/tex/plain/base/plain.tex" ) then
	setenv TEXROOT $ownpath
	unset 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\".
	echo Please provide a proper tex root (like \"source setuptex /path/tex\")
	unset ownpath
	exit
endif

# binaries & formats
setenv TEXMFOS $TEXROOT/texmf-$platform

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

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

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

# FIXME
# Note also that TMP is not necessairly set...
if ( $?TMP == 0 ) then
	setenv TMP /tmp
endif
setenv TEXMFVAR $TMP/texmf-var
setenv VARTEXMF $TEXMFVAR


unsetenv CTXDEVTXPATH CTXDEVMPPATH CTXDEVMFPATH
unsetenv 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)

setenv HOMETEXMF /nonexist

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

setenv TEXFORMATS \$TEXMFOS/web2c\{/\$engine,\}
setenv MPMEMS     \$TEXMFOS/web2c\{/\$engine,\}

unsetenv TEXINPUTS MPINPUTS MFINPUTS

setenv PATH $TEXMFOS/bin:$PATH
# TODO: OSFONTDIR

if ( $?RUBYLIB == 0 ) then
	setenv RUBYLIB $TEXMFCONTEXT/scripts/context/ruby
else
	setenv RUBYLIB $TEXMFCONTEXT/scripts/context/ruby:$RUBYLIB
endif
which ruby > /dev/null
if ( $status ) then
	echo Warning: the package \"ruby\" is not located.
endif

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

setenv CTXMINIMAL yes
