#!/bin/sh

#NOTE: if your filesystem is mounted with 'noexec' flag, run this file
#as 
#	sh autorun
#instead of
#	./autorun

#FOR UNIX-like platforms ONLY!!
#autorun script for starting Stunnix Advanced Web Server locally and directing 
#user's  browser to the page on the site served by it.
#
#Copyright (c) 2004-2019 by Stunnix company - see http://www.stunnix.com
#for more details.

#uncomment this line or set this env. var to get tracing
#STUNNIXWS_TRACE=1

cd "`dirname $0`"
export STUNNIXWS_STARTED_BY="`pwd`/`basename $0`"


show_message_in_browser()
{
    htmlfn="${PWD}/$1"

    found_browser=0
    echo trying to open browser with this page open\: "${htmlfn}"
	if [ -r "${htmlfn}" ]; then 
	    for browser in firefox google-chrome netscape; do
		if which ${browser} > /dev/null 2>&1; then
		    cd /
		    echo starting ${browser}
		    ${browser} "${htmlfn}" > /dev/null 2>&1  &
		    found_browser=1
		    break
		fi
	    done
	fi
	if [ ${found_browser} = 0 ]; then
	    echo no browser found to open page $htmlfn - please open it in any browser for instructions
	fi
}

check_32bit_support_or_die()
{
if [ -r site/stunnixwebsrv/check-linux-32bit-support ]  ; then
    if ! site/stunnixwebsrv/check-linux-32bit-support > /dev/null 2>&1 ; then
	echo "You do not have libraries for 32 bit apps installed. The product will not work until you install them."
	echo "To install them on Ubuntu or Debian, run"
	echo "  sudo apt-get update; sudo apt-get install ia32-libs"
	echo "and if it fails, run"
        echo "  sudo apt-get update; sudo apt-get install lib32z1 lib32ncurses5 lib32bz2-1.0"
	echo ""
	echo "To install them on RedHat-based distros (Fedora, CentOS, RHEL), run"
	echo "  sudo yum install redhat-lsb.i686"
	echo "To install them on SuSE, run"
	echo "  sudo zypper install lsb-core-ia32 zlib-32bit"

	show_message_in_browser site/stunnixwebsrv/special-pages/linux-install-32bit-libs/en/page.html	
	exit 1;
    fi
fi
}






#check all mac app bundles for presence of 'Contents/Resources/data'
for f in *.app; do
    dir="$f/Contents/Resources/data"
    if [ -r "${dir}" ] ; then
	cd "${dir}"
    fi
done

#if 'data' folder exists, chdir to it
if [ -d data ]; then
    cd data
fi

PLAT=`uname -m`
if [ "${PLAT}" = "i686" ]; then PLAT=x86 ; fi
if [ "${PLAT}" = "i586" ]; then PLAT=x86 ; fi
if [ "${PLAT}" = "i386" ]; then PLAT=x86 ; fi


PLATSUBDIR=linux-${PLAT}
if [ -d perl/${PLATSUBDIR} ]; then  #we have perl for this arch, assume everything else is also available
    : # everything is fine
else
    if [ "${PLAT}" = "x86_64" ]; then
	check_32bit_support_or_die;
	#the function above won't return if we can't run 32bit code here
	PLAT=x86
	#will use 32bit version 

    else 
	#we are on 32-bit CPU, but only 64-bit perl is available!
	show_message_in_browser site/stunnixwebsrv/special-pages/linux-64bit-linux-required/en/page.html
	exit 1;
    fi
fi

export STUNNIXWS_CPU_PLATFORM=${PLAT}
PLATSUBDIR=linux-${PLAT}

PERLLIB="$PERLLIB:${PWD}/perl/common-inc/"
export PERLLIB
PERL5LIB="$PERL5LIB:${PWD}/perl/common-inc/"
export PERL5LIB


if [ -d compressed ]; then
    SAWS_contentroot=${PWD}
    export SAWS_contentroot
    cd compressed/tools/${PLAT}-linux
    ./loader 
    exit 0;
fi 


if [ `uname` = "Linux" ]; then #are we on Linux?
    if uname -m | grep 86 >/dev/null ; then #are we on Intel x86-compatible CPU
	if  perl/${PLATSUBDIR}/perl -v  >/dev/null 2>&1; then #can we run Perl we ship?
	    #export PERLLIB="$PERLLIB:${PWD}/perl/${PLATSUBDIR}/lib"
	    #export PERL5LIB="$PERL5LIB:${PWD}/perl/${PLATSUBDIR}/lib"

	    if [  $STUNNIXWS_TRACE ]; then #if debugging was requested by setting env.var
		#trace startup
		#-I"${PWD}/perl/${PLATSUBDIR}/lib"
		perl/${PLATSUBDIR}/perl  site/stunnixwebsrv/starter.pl ./site/stunnixwebsrv/startsite.pl $@ _debug 1
		exit 0
	    fi

	    #we are here because no debugging was requested
	    exec nohup perl/${PLATSUBDIR}/perl   site/stunnixwebsrv/starter.pl \
		./site/stunnixwebsrv/startsite.pl $@ </dev/null >/dev/null 2>&1 &
	    exit 0
	fi
    fi
fi

#we are here because there is no prebuilt perl included for OS this 
#script is running under. Let's hope packager #listed all modules in 
#perl/fallback-inc and that system we are running on includes perl
PERLLIB=$PERLLIB:`pwd`/perl/fallback-inc/
export PERLLIB
PERL5LIB=$PERL5LIB:`pwd`/perl/fallback-inc/
export PERL5LIB

exec nohup perl site/stunnixwebsrv/startsite.pl $@  </dev/null >/dev/null 2>&1 &
