#!/bin/bash
export LANG=C

_no_clean=0
_loglevel=1
_build_param=""
_default_b="LSI=V4H FORCE_115200=1"
_echo="@"
if [ -f env.ini ]; then
  . env.ini
fi

while getopts "cl:vp:h" opt; do
  case $opt in
  c) _no_clean=1
    ;;
  l) _loglevel=$OPTARG
    ;;
  v) _echo=""
    ;;
  p) if [ "x$OPTARG" = "x0" ]; then
        _build_param="${_build_param} MEASURE_TIME=1 MEASURE_TIME_NOPRINT=1"
     elif [ "x$OPTARG" = "x1" ]; then
        _build_param="${_build_param} MEASURE_TIME=1"
     fi
    ;;
  h)
    echo "usage: $0 [option]"
    echo "  -l                 set loglevel (default: $_loglevel)"
    echo "  -p                 enable MEASURE_TIME (0: NO PRINT, 1: PRINT)"
    echo ""
    echo "  -c                 do not clean  before build"
    echo "  -v                 build verbosely"
    exit 0
  esac
done
shift $((OPTIND-1))

if [ $_no_clean -ne 1 ]; then
    make clean
    make CC=echo AS=echo LD=echo OC=echo OD=echo >& /dev/null
fi
BUILD_PARAM="${BUILD_PARAM} ${_build_param} LOG_LEVEL=$_loglevel V=$_echo"

_uuid=`uuidgen`
_obj_bat=/run/lock/${_uuid}.bat
    GHS_PATH=`winepath -w $(readlink -f ../comp_202015)`
    echo "@path %path%;${GHS_PATH}" > ${_obj_bat}
    echo "@set GHS_LMHOST=@10.230.22.105" >> ${_obj_bat}
    echo "@set GHS_LMWHICH=ghs" >> ${_obj_bat}
    echo "make -f dos.mk ${_default_b} ${BUILD_PARAM} $*" >> ${_obj_bat}
    echo 'set /p "_wait=WAIT..."' >> ${_obj_bat}
wineconsole z\:\\run\\lock\\${_uuid}.bat
rm ${_obj_bat}
