#!/bin/bash

BASEDIR=$(dirname $0)

cd $BASEDIR

TGT_PLATFORM=NONE
PLATFORM_WIN32=0
PLATFORM_WIN64=0
PLATFORM_LINUX32=0
PLATFORM_LINUX64=0
PLATFORM_WEB=0
PLATFORM_ANDROID=0
PROJECT_NAME=""
PROJECT_CATEGORY=AudioVideo
APP_TYPE=Application
TERMINAL_FLAG=false
PROJECT_DIR=""
OUTPUT_DIR=""
ENABLE_WEB_THREADS=false
ICON=""
SOURCE="main.bas"
ANDROID_APP_ID=""
ANDROID_ORIENTATION="default"
ANDROID_KEYSTORE=""
ANDROID_KEYSTORE_PASS=""
ANDROID_ALIAS=""
ANDROID_ALIAS_PASS=""
ANDROID_RELEASE=0
ANDROID_DEBUG=0
ANDROID_JAVA_DIR=""
RCBASIC_STUDIO=1 

DST_ARG=$( cat $1 )

# echo "args = $DST_ARG"

for (( i = 1; i < 50; ++i ))
do

	OPT_VAL=$( echo $DST_ARG | cut -d' ' "-f$i" )
	if [ "7$OPT_VAL" == "7" ]; then
		break
	fi

	PARAM_NAME=$( echo $OPT_VAL | cut -d'=' -f1 )

	if [ "$PARAM_NAME" == "TGT_PLATFORM" ]; then

		TGT_PLATFORMS="$( echo $OPT_VAL | cut -d'=' -f2 ),"
		echo "platforms: $TGT_PLATFORMS"

		for (( n = 1; n < 10; ++n ))
		do

			PARAM_VAL=$( echo $TGT_PLATFORMS | cut -d',' "-f$n" )

			if [ "7$PARAM_VAL" == "7" ]; then
				break
			fi
			
			if [ "$PARAM_VAL" == "WEB" ]; then
				PLATFORM_WEB=1
			elif [ "$PARAM_VAL" == "WIN_32" ]; then
				PLATFORM_WIN32=1
			elif [ "$PARAM_VAL" == "WIN_64" ]; then
				PLATFORM_WIN64=1
			elif [ "$PARAM_VAL" == "LINUX_32" ]; then
				PLATFORM_LINUX32=1
			elif [ "$PARAM_VAL" == "LINUX_64" ]; then
				PLATFORM_LINUX64=1
			elif [ "$PARAM_VAL" == "ANDROID" ]; then
				PLATFORM_ANDROID=1
			fi

		done
	
	elif [ "$PARAM_NAME" == "PROJECT_NAME" ]; then
		
		PROJECT_NAME=$( echo $OPT_VAL | cut -d'=' -f2 | tr -d '"' )
		#echo "Project name is $PROJECT_NAME"
	
	elif [ "$PARAM_NAME" == "PROJECT_CATEGORY" ]; then
		PROJECT_CATEGORY=$( echo $OPT_VAL | cut -d'=' -f2 | tr -d '"' )
		
	elif [ "$PARAM_NAME" == "APP_TYPE" ]; then
		APP_TYPE=$( echo $OPT_VAL | cut -d'=' -f2 | tr -d '"' )
		
	elif [ "$PARAM_NAME" == "TERMINAL_FLAG" ]; then
		TERMINAL_FLAG=$( echo $OPT_VAL | cut -d'=' -f2 | tr -d '"' )
		
	elif [ "$PARAM_NAME" == "PROJECT_DIR" ]; then
		PROJECT_DIR=$( echo $OPT_VAL | cut -d'=' -f2 | tr -d '"' )
		
	elif [ "$PARAM_NAME" == "OUTPUT_DIR" ]; then
		OUTPUT_DIR=$( echo $OPT_VAL | cut -d'=' -f2 | tr -d '"' )
		
	elif [ "$PARAM_NAME" == "ENABLE_WEB_THREADS" ]; then
		ENABLE_WEB_THREADS=$( echo $OPT_VAL | cut -d'=' -f2 | tr -d '"' )
		
	elif [ "$PARAM_NAME" == "ICON" ]; then
		ICON=$( echo $OPT_VAL | cut -d'=' -f2 | tr -d '"' )
		
	elif [ "$PARAM_NAME" == "SOURCE" ]; then
		SOURCE=$( echo $OPT_VAL | cut -d'=' -f2 | tr -d '"' )
		
	elif [ "$PARAM_NAME" == "ANDROID_APP_ID" ]; then
		ANDROID_APP_ID=$( echo $OPT_VAL | cut -d'=' -f2 | tr -d '"' )
		
	elif [ "$PARAM_NAME" == "ANDROID_ORIENTATION" ]; then
		ANDROID_ORIENTATION=$( echo $OPT_VAL | cut -d'=' -f2 | tr -d '"' )
	
	elif [ "$PARAM_NAME" == "ANDROID_KEYSTORE" ]; then
		ANDROID_KEYSTORE=$( echo $OPT_VAL | cut -d'=' -f2 | tr -d '"' )
		
	elif [ "$PARAM_NAME" == "ANDROID_KEYSTORE_PASS" ]; then
		ANDROID_KEYSTORE_PASS=$( echo $OPT_VAL | cut -d'=' -f2 | tr -d '"' )
		
	elif [ "$PARAM_NAME" == "ANDROID_ALIAS" ]; then
		ANDROID_ALIAS=$( echo $OPT_VAL | cut -d'=' -f2 | tr -d '"' )
		
	elif [ "$PARAM_NAME" == "ANDROID_ALIAS_PASS" ]; then
		ANDROID_ALIAS_PASS=$( echo $OPT_VAL | cut -d'=' -f2 | tr -d '"' )
		
	elif [ "$PARAM_NAME" == "ANDROID_RELEASE" ]; then
		ANDROID_RELEASE=$( echo $OPT_VAL | cut -d'=' -f2 | tr -d '"' )
		
	elif [ "$PARAM_NAME" == "ANDROID_DEBUG" ]; then
		ANDROID_DEBUG=$( echo $OPT_VAL | cut -d'=' -f2 | tr -d '"' )
		
	elif [ "$PARAM_NAME" == "ANDROID_JAVA_DIR" ]; then
		ANDROID_JAVA_DIR=$( echo $OPT_VAL | cut -d'=' -f2 | tr -d '"' )
		
	elif [ "$PARAM_NAME" == "RCBASIC_STUDIO" ]; then
		RCBASIC_STUDIO=$( echo $OPT_VAL | cut -d'=' -f2 | tr -d '"' )
		
	fi

	#echo "$i --> $OPT_VAL"
done




echo "WIN32=$PLATFORM_WIN32"
echo "WIN64=$PLATFORM_WIN64"
echo "LINUX32=$PLATFORM_LINUX32"
echo "LINUX64=$PLATFORM_LINUX64"
echo "WEB=$PLATFORM_WEB"
echo "ANDROID=$PLATFORM_ANDROID"
echo "NAME=$PROJECT_NAME"
echo "CAT=$PROJECT_CATEGORY"
echo "TYPE=$APP_TYPE"
echo "TERMINAL=$TERMINAL_FLAG"
echo "PROJECT_DIR=$PROJECT_DIR"
echo "OUTPUT_DIR=$OUTPUT_DIR"
echo "WEB_THREADS=$ENABLE_WEB_THREADS"
echo "ICON=$ICON"
echo "SOURCE=$SOURCE"
echo "ANDOIRD_APP_ID=$ANDROID_APP_ID"
echo "ANDROID_ORIENTATION=$ANDROID_ORIENTATION"
echo "ANDROID_KEYSTORE=$ANDROID_KEYSTORE"
echo "ANDROID_KEYSTORE_PASS=$ANDROID_KEYSTORE_PASS"
echo "ANDROID_ALIAS=$ANDROID_ALIAS"
echo "ANDROID_ALIAS_PASS=$ANDROID_ALIAS_PASS"
echo "ANDROID_RELEASE=$ANDROID_RELEASE"
echo "ANDROID_DEBUG=$ANDROID_DEBUG"
echo "ANDROID_JAVA_DIR=$ANDROID_JAVA_DIR"

if [ "--$PLATFORM_WEB" == "--1" ]; then
	pushd $OUTPUT_DIR
	web_folder="$PROJECT_NAME"_WEB
	if [ -d "$web_folder" ]; then
		rm -rf "$web_folder"
	fi
	mkdir "$web_folder"
	popd
	out_file="$OUTPUT_DIR/$web_folder/$PROJECT_NAME".html
	out_file=${out_file//"//"/"/"}
	../dist/em/em_build.sh "$PROJECT_DIR" "$out_file"
fi

