#!/bin/sh

# Takes the same arguments as mtx-update

# feel free to suggest a more elegant solution
if which rsync &> /dev/null; then
	echo "OK" > /dev/null
else
	echo "You need to install rsync first."
	exit
fi
if which ruby &> /dev/null; then
	echo "OK" > /dev/null
else
	echo "You might want to install Ruby first if you want to use pdfTeX or XeTeX."
fi

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

case "$system" in
	# linux
	Linux)
		case "$cpu" in
			i*86) platform="linux" ;;
			x86_64|ia64) platform="linux-64" ;;
			# a little bit of cheating with ppc64 (won't work on Gentoo)
			ppc|ppc64) platform="linux-ppc" ;;
			*) platform="unknown" ;;
		esac ;;
	# Mac OS X
	Darwin)
		case "$cpu" in
			i*86) platform="osx-intel" ;;
			x86_64) platform="osx-64" ;;
			ppc*|powerpc|power*|Power*) platform="osx-ppc" ;;
			*) platform="unknown" ;;
		esac ;;
	# FreeBSD
	FreeBSD|freebsd)
		case "$cpu" in
			i*86) platform="freebsd" ;;
			x86_64) platform="freebsd" ;; # no special binaries are available yet
			amd64) platform="freebsd-amd64" ;;
			*) platform="unknown" ;;
		esac ;;
	# kFreeBSD (debian)
	GNU/kFreeBSD)
		case "$cpu" in
			i*86) platform="kfreebsd-i386" ;;
			amd64) platform="kfreebsd-amd64" ;;
			*) platform="unknown" ;;
		esac ;;
	# SunOS/Solaris
	SunOS)
		case "$cpu" in
			sparc) platform="solaris-sparc" ;;
			i86pc) platform="solaris-intel" ;;
			*) platform="unknown" ;;
		esac ;;
	*) platform="unknown"
esac

# temporary patch for 64-bit Leopard with 32-bit kernel
if test "$platform" = "osx-intel"; then
	# if running Snow Leopard or later
	# better: /usr/bin/sw_vers -productVersion
	if test `uname -r|cut -f1 -d"."` -ge 10 ; then
		# if working on 64-bit hardware
		if test `sysctl -n hw.cpu64bit_capable` = 1; then
			# snowleopard32=TRUE
			platform="osx-64"
		fi
	fi
fi

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)"
  exit
fi

# Uncomment the line below if you want to force 64-bit Mac OS X binaries on Snow Leopard,
# but keep in mind that you also need to adjust the "setuptex" script.
# You should either create your own version of setuptex script where you set the platform
# to osx-64 explicitely or you can set up the path in .bash_profile.
#
# But the best option is most probably to just boot into 64-bit mode.
# See http://support.apple.com/kb/HT3773 for instructions.

#platform=osx-64

export PATH=$PWD/bin:$PATH

# download or rsync the latest scripts first
rsync -rlptv rsync://contextgarden.net/minimals/setup/$platform/bin .

# download or update the distribution
# you may remove the --context=beta switch if you want to use "current"
mtxrun --verbose --script ./bin/mtx-update.lua --update --make --force --engine=all --context=beta --platform=$platform --texroot=$PWD/tex $@

# if [ "x$snowleopard32" = "xTRUE" ];
# then
# 	echo
# 	echo "You appear to be running Snow Leopard on 64-bit hardware with 32-bit kernel."
# 	echo "If you want to run 64-bit TeX binaries modify this installer or reboot"
# 	echo "with 64-bit kernel."
# 	echo
# fi

echo
echo "When you want to use context, you need to initialize the tree by typing:"
echo
echo "  . $PWD/tex/setuptex"
echo
echo "in your shell or add"
echo "  \"$PWD/tex/texmf-$platform/bin\""
echo "to PATH variable if you want to set it permanently."
echo "This can usually be done in .bashrc, .bash_profile"
echo "(or whatever file is used to initialize your shell)."
echo

if which ruby &> /dev/null; then
	echo "OK" > /dev/null
else
	echo "You might want to install Ruby if you want to use pdfTeX or XeTeX."
	echo
fi

