#!/bin/bash
dirname=$(dirname "$0")
pushd "$dirname" 1>/dev/null 2>/dev/null && absname=$(pwd) && popd 1>/dev/null 2>/dev/null 
pushd "$absname"

# uname <option>, where <option>:
# -s, --kernel-name        print the kernel name
# -n, --nodename           print the network node hostname
# -r, --kernel-release     print the kernel release
# -v, --kernel-version     print the kernel version
# -m, --machine            print the machine hardware name
# -p, --processor          print the processor type or "unknown"
# -i, --hardware-platform  print the hardware platform or "unknown" - does not work on OSX
# -o, --operating-system   print the operating system - does not work on OSX

OS=Unknown
ARCH=Unknown
SO=.Unknown

# Find ARCH by machine
case `uname -m` in
    x86_64) ARCH=x64;;
    i686)   ARCH=x86;;
    i386)   ARCH=x86;;
    arm*)   ARCH=arm;;
esac

if [[ -n "${ARTIST_UNKNOWN_ARCH_PREFER}" && "${ARCH}" == "Unknown" ]]; then
	ARCH="${ARTIST_UNKNOWN_ARCH_PREFER}"
fi

# Find OS by kernel name
case `uname -s` in
    Linux)
	OS=Linux
	OSARCH=$OS/$ARCH
	BINARCH=$OS/$ARCH
	SO=.so
	# this should go away at some point (instead: -Wl,-rpath $ORIGIN)
	export LD_LIBRARY_PATH="$absname/":"$absname/bin/$OSARCH/":$LD_LIBRARY_PATH
	;;
    Darwin)
	OS=OSX
	OSARCH=$OS # universal
	BINARCH=$OS/$ARCH # not universal
	SO=.dylib
	# this should go away at some point (instead: install_name_tool with @rpath and @executable path)
	export DYLD_LIBRARY_PATH="$absname/bin/$OSARCH":$DYLD_LIBRARY_PATH
	;;
    *)
	case `uname -o` in
	    Cygwin)
		OS=Windows
		SO=.dll
		;;
	esac
esac

export LUA_PATH="$absname/?.lua;$LUA_PATH"
"./bin/$BINARCH/luajit" project/main.lua #"$@"
