#!/bin/bash
SCRIPTDIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
WD=`pwd`

# get the current vuejs mode
VUEMODE=`$SCRIPTDIR/vuemode.sh query`
if [[ $? -eq 1 ]]; then
  exit 1
fi

if [[ "$1" == "dev" ]]; then
  BUILDMODE='dev';
else
  BUILDMODE='prod';
fi

# switch vuejs to build mode
$SCRIPTDIR/vuemode.sh $BUILDMODE

# run the grunt script in the scripts working directory
# so we can be sure all javascript is properly built from the most recent source
cd $SCRIPTDIR
rm -rf "${SCRIPTDIR}/amd/build/" # clear all  files in the amd build dir so we don't have leftover files messing things up
grunt amd
grunt scssplugin # plugin specific scss task to compile styles.css from scss files

# run the build php script
php ${SCRIPTDIR}/build.php $@

if [[ "$VUEMODE" != "$BUILDMODE" ]]; then
  # switch vuejs vack to original mode
  $SCRIPTDIR/vuemode.sh $VUEMODE

  # re-do the grunt compiling
  grunt amd
fi

# return to the working directory
cd $WD
