#!/bin/sh

# Takes the same arguments as mtx-update

# you may change this if you want ...
CONTEXTROOT="$PWD/tex"

# suggested by Tobias Florek to check for ruby & rsync
if [ ! -x "`which rsync`" ]; then
	echo "You need to install rsync first."
	exit 1
fi
if [ ! -x "`which ruby`" ]; then
	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" ;;
			# we currently support just mipsel, but Debian is lying (reports mips64)
			# we need more hacks to fix the situation, this is just a temporary solution
			mips|mips64|mipsel|mips64el) platform="linux-mipsel" ;;
			*) platform="unknown" ;;
		esac ;;
	# Mac OS X
	Darwin)
        echo "=== system branch is Darwin: $system" # DEBUG
		case "$cpu" in
			i*86) platform="osx-intel" ;;
			x86_64) 
                echo "=== cpu branch is x86_64: $cpu" # DEBUG
                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" ;;
			x86_64|amd64) platform="kfreebsd-amd64" ;;
			*) platform="unknown" ;;
		esac ;;
	# cygwin
	CYGWIN*)
		case "$cpu" in
			i*86) platform="cygwin" ;;
			x86_64|ia64) platform="cygwin-64" ;;
			*) 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

echo "=== system:   $system" # DEBUG
echo "=== cpu:      $cpu" # DEBUG
echo "=== platform: $platform" # DEBUG

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

exit # DEBUG
