#!/bin/bash
SOURCE="${BASH_SOURCE[0]}"
while [ -h "$SOURCE" ]; do # resolve $SOURCE until the file is no longer a symlink
    DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
    SOURCE="$(readlink "$SOURCE")"
    [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE" # if $SOURCE was a relative symlink, we need to resolve it relative to the path where the symlink file was located
done
DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"

cd $DIR/../
. ./meta/env


### Parameter resolve
params="$(getopt -o h -l no-ts-rebuild,uglify,help --name "$(basename "$0")" -- "$@")"
function usage(){
    echo "$1 --no-ts-rebuld|--uglify"
}

if [ $? -ne 0 ]
then
    usage
fi

eval set -- "$params"
unset params

while true
do
    case $1 in
        -h|--help)
            usage
            ;;
        --no-ts-rebuild)
            noTsRebuild="yes"
            ;;
        --uglify)
            uglify="yes"
            ;;
        --)
            shift
            break
            ;;
        *)
            usage
            ;;
    esac
done

if [ "$noTsRebuild" = "yes" ];then
    echo "Skip building webapp ts..."
else
    echo "Building webapp ts...."
    echo "Generating meta.ts"
    ./webapp/dev/createMeta
    echo "Generating R.ts"
    node ../cli/lib/cli.js -l ./lib/leaf.d.ts webapp/src/R.d.ts
    tsc -p ./webapp/tsconfig.json
fi

echo "Building server ts...."
tsc -p ./server/tsconfig.json
echo "Building stylesheet"
./webapp/node_modules/less/bin/lessc ./webapp/src/style/main.less > ./webapp/src/style/main.css

echo "Ensure dist folders"
mkdir -p ./dist/webapp/style
mkdir -p ./dist/webapp/script

echo "Packaging apps"
version=$RANDOM

./webapp/node_modules/leaf-require-cli/bin/leaf-require --main "main" -r "script" \
             --stand-alone --compile \
             ./webapp/src/script -c \
             > ./dist/webapp/script/bundle-$version.js


mkdir -p ./dist/webapp/resource
cp -rf ./webapp/src/resource/* ./dist/webapp/resource/
cp ./webapp/src/style/main.css ./dist/webapp/style/main-$version.css
cp ./webapp/src/index.html ./dist/webapp/index.html


function csed(){
    case "$OSTYPE" in
        darwin*)  sed -i ".backup" -e $@;; 
        linux*)   sed -i $@;;
        *)        sed -i ".backup" $@;;
    esac
}
function escapePath(){
    echo ${1//\//\\\/}
}

echo "purge debug sections"
csed "s/<\!--debug-->.*<\!--\/debug-->//g" ./dist/webapp/index.html
csed "s/<\!--release>//g" ./dist/webapp/index.html
csed "s/<\/release-->//g" ./dist/webapp/index.html
csed '/^\s*$/d' ./dist/webapp/index.html
csed "s/CDN_BASE/`escapePath $CDN_BASE`/g" ./dist/webapp/index.html
csed "s/VERSION/$version/g" ./dist/webapp/index.html
csed "s/url(../url(`escapePath $CDN_BASE`/g" ./dist/webapp/style/main-$version.css

if [ "$uglify" = "yes" ];then
    ./webapp/node_modules/uglify/bin/uglify -s ./dist/webapp/script/bundle-version.js -o ./dist/webapp/script/bundle-$version.min.js
    ./webapp/node_modules/uglifycss/bin/uglifycss ./dist/webapp/style/main-version.css > ./dist/webapp/style/main-$version.min.css
fi

echo $version > ./dist/webapp/version
