#!/usr/bin/env bash

function usage()
{
    echo "VMD wrapper"
    echo ""
    echo "./vmd_wrapper"
    echo "\t-h --help"
    echo "\t--host VALUE"
    echo "\t--dispdev VALUE"
    echo ""
}

SSH=""
DISPDEV="win"
while [ "$1" != "" ]; do
    PARAM=$1
    VALUE=$2
    case $PARAM in
        -h | --help)
            usage
            exit
            ;;
        --host)
            SSH="/usr/bin/ssh $2 -oTCPKeepAlive=True -oSendEnv=True"
	        shift
	        shift
            exec $SSH $(readlink -f "$0")  $*
            exit 0
            ;;
        --dispdev)
            DISPDEV=$VALUE
            ;;
        *)
            echo "ERROR: unknown parameter \"$PARAM\""
            usage
            exit 1
            ;;
    esac
    shift
    shift
done


if [ ! -d "$VMD_PATH" ]; then
	if [ -e /Applications ]; then
		SUFFIX=MACOSXX86
		for F in /Applications/VMD*.app/Contents/vmd; do
			VMD_PATH=$F
		done
	
	else
		SUFFIX=LINUXAMD64
		VMD=$(which vmd 2>/dev/null) 
		if [ ! -x "$VMD" ]; then
			echo "Cannot find VMD. Ensure that 'vmd' is in the PATH"
			exit 1
		fi

		VMD_PATH=$(grep defaultvmddir $VMD | head -1 | sed 's/.*=//g' | sed 's/"//g' )
	fi
fi


if [ ! -d "$VMD_PATH" ]; then
	echo "Cannot find VMD. Set VMD_PATH to point to the directory containing vmd_XXX"
	exit 1
fi


#echo "[$VMD_PATH]"

if ! [ -x "$VMD_PATH/vmd_$SUFFIX" ]; then
	echo "Cannot find vmd_$SUFFIX. Ensure that 'vmd' is in the PATH"
	exit 1
fi

export LD_LIBRARY_PATH="$VMD_PATH:$LD_LIBRARY_PATH"
export VMDDIR="$VMD_PATH"
export MASTERVMDDIR="$VMD_PATH"
export VMDDISPLAYDEVICE="$DISPDEV"
export VMDTITLE=off
export TCL_LIBRARY="$VMD_PATH/scripts/tcl"
export PYTHONPATH="$VMD_PATH/scripts/python"
export STRIDE_BIN="$VMD_PATH/stride_$SUFFIX"
export SURF_BIN="$VMD_PATH/surf_$SUFFIX"
export TACHYON_BIN="$VMD_PATH/tachyon_$SUFFIX"
if [ "$DISPLAY" == "" ]; then 
	export DISPLAY=":0" 
fi
echo "Display: [$DISPLAY]"
exec "$MASTERVMDDIR/vmd_$SUFFIX" -eofexit -nt $*

