#!/bin/bash -ex
# Note: -ex option above will terminate on first error, and echo each command

# This build file is executed by Sagoku Static Web App deployment
# when you configure your Sagoku Static Web App build script path to point here
# The container that executes this script will copy the result to S3 bucket sequoia-static

SRC_DIR=$1 # root folder of the workspace, /opt/git/workspace
BUILD_DIR=$2 # /mnt/deb-builds/${PACKAGE_NAME}-${VERSION}
PACKAGE_NAME=$3 # appName
VERSION=$4 # autogenerated timestamp
env
echo "Setting up Package $PACKAGE_NAME Version ${VERSION}"
echo "Building ${PACKAGE_NAME} in ${BUILD_DIR}"

# Set up node and npm install libraries.
wget https://nodejs.org/dist/v6.9.2/node-v6.9.2-linux-x64.tar.xz
xz -dc node-v6.9.2-linux-x64.tar.xz | tar xf - -C /usr/local/
export PATH=$PATH:/usr/local/node-v6.9.2-linux-x64/bin
# This is where the node package file is
cd ${SRC_DIR}
mkdir -p ${SRC_DIR}/build
npm install
npm run build

cp -r ${SRC_DIR}/documentation/assets ${SRC_DIR}/documentation/styleguide/assets

rsync -va ${SRC_DIR}/documentation/ ${BUILD_DIR}
ls -al ${SRC_DIR}/documentation/
ls -al ${BUILD_DIR}
echo "Build script complete"
