#!/bin/bash

usage()
{
	echo ""
	echo "Usage: `basename $0` [options] [-jn] [-v] [-h]"
	echo "Options:"
	echo "  release, debug: "
	echo "      Specifies the build type."
	echo "  i386, amd64, arm, arm64, mips, mips64, ppc, ppc64:"
	echo "      Specifies the architecture for code generation."
	echo "  clean: "
	echo "      Clean the build folder."
	echo "  ci: "
	echo "      Specifies the environment is CI."
	echo "  -h, --help:"
	echo "      Print this message and exit."
	echo "  -j: enable make '-j' option."
	echo "      if 'n' is not given, will set jobs to auto detected core count, otherwise n is used."
	echo "  -v: verbose make"
	echo ""
	exit 0
}

HOST_OS=`uname`
HOST_ARCH=`uname -m`

case ${HOST_ARCH} in
	i386|i686) HOST_ARCH="i386"
		;;
	x86_64|amd64) HOST_ARCH="amd64"
		;;
	armv6|armv7|armv7s|armv7l) HOST_ARCH="arm"
		;;
	aarch64) HOST_ARCH="arm64"
		;;
	mips|mipsel) HOST_ARCH="mips"
		;;
	mips64) HOST_ARCH="mips64"
		;;
	powerpc) HOST_ARCH="ppc"
		;;
	ppc64) HOST_ARCH="ppc64"
		;;
esac

TARGET_OS=$HOST_OS
TARGET_ARCH=$HOST_ARCH
BUILD_TYPE="release"
BUILD_PREFIX=""
BUILD_OPTION=""

for i in "$@"
do
	case $i in
		i386|amd64|arm|arm64|mips|mips64|ppc|ppc64) TARGET_ARCH=$i
			;;
		release|debug|clean) BUILD_TYPE=$i
			;;
		ci) CI="ci"
			;;
		-j*) ENABLE_JOBS=1; BUILD_JOBS="${i#-j}"
			;;
		 -v) BUILD_VERBOSE='VERBOSE=1' 
			;;
  --help|-h) usage
			;;
		  *) echo "illegal option $i"
			usage
			;;
	esac
done

if [ $TARGET_ARCH != $HOST_ARCH ] || [ $TARGET_OS != $HOST_OS ]; then
	case $TARGET_ARCH in
		i386)
			if [ $HOST_ARCH = "amd64" ]; then
				BUILD_OPTION="-m32"
			else
				usage
			fi
			;;
		amd64)
			if [ $TARGET_OS = "Linux" ]; then
				BUILD_PREFIX="x86_64-linux-gnu-"
			else
				usage
			fi
			;;
		arm)
			if [ $TARGET_OS = "Linux" ]; then
				BUILD_PREFIX="arm-linux-gnueabihf-"
				BUILD_OPTION="-march=armv7-a -mfpu=vfp3 -marm"
			else
				usage
			fi
			;;
		arm64)
			if [ $TARGET_OS = "Linux" ]; then
				BUILD_PREFIX="aarch64-linux-gnu-"
			else
				usage
			fi
			;;
		mips)
			if [ $TARGET_OS = "Linux" ]; then
				BUILD_PREFIX="mips-linux-gnu-"
			else
				usage
			fi
			;;
		mips64)
			if [ $TARGET_OS = "Linux" ]; then
				BUILD_PREFIX="mips64-linux-gnuabi64-"
			else
				usage
			fi
			;;
		ppc)
			if [ $TARGET_OS = "Linux" ]; then
				BUILD_PREFIX="powerpc-linux-gnu-"
			else
				usage
			fi
			;;
		ppc64)
			if [ $TARGET_OS = "Linux" ]; then
				BUILD_PREFIX="powerpc64-linux-gnu-"
			else
				usage
			fi
			;;
	esac
fi

if [ "$BUILD_PREFIX" != "" ]; then
	export CC=${BUILD_PREFIX}gcc
	export CXX=${BUILD_PREFIX}g++
fi

inform()
{
	echo ""
	echo "submodule vender is not existed!"
	echo "you can execute the given command to init and update it."
	echo " \$ git submodule init"
	echo " \$ git submodule update"
	echo ""
	exit 1
}

if [ "$ENABLE_JOBS" = "1" -a "$BUILD_JOBS" = "" ]; then
	#get cpu core count 
	CPU_CORE=1
	case ${HOST_OS} in
		Darwin)
			CPU_CORE=$(sysctl hw.ncpu | awk '{print $2}')
			;;
		Linux)
			CPU_CORE=$(cat /proc/cpuinfo | grep processor | wc -l)
			;;
		Windows)
			CPU_CORE=$(echo $NUMBER_OF_PROCESSORS)
			;;
	esac
	echo "host machine has ${CPU_CORE} core"

	if [ "$CPU_CORE" = "1" ]; then
		BUILD_JOBS=""
	else
		# set build jobs with cpu core count
		BUILD_JOBS=${CPU_CORE}
	fi
fi

export BUILD_JOBS
export BUILD_VERBOSE

if [ -d vender ]; then 
	cd vender
	sh build $BUILD_TYPE $TARGET_ARCH
	if [ $? != 0 ]; then
		exit 1
	fi
	cd ..
else
	inform
fi

cd fibjs
sh build $BUILD_TYPE $TARGET_ARCH $CI
if [ $? != 0 ]; then
	exit 1
fi
cd ..
