#!/bin/bash

if ! [ -x "$(command -v jq)" ]; then
  echo "ERROR: Program 'jq' is needed and not installed." >&2
  exit 1
fi

version=$(jq -r .version package.json)

echo "
   ____       _               _   ____
  |  _ \ ___ | |__   ___ _ __| |_|___ \\
  | |_) / _ \| '_ \ / _ \ '__| __| __) |
  |  _ < (_) | |_) |  __/ |  | |_ / __/
  |_| \_\___/|_.__/ \___|_|   \__|_____|

* RELEASE WebClient ${version} DISTRIBUTION SCRIPT *

Let's go!"

echo "
Compiling sources..."
yarn build
echo "Done."

echo "
Renaming dist files with version number..."
cd dist
mv css/app.css "css/app.$version.css"
mv css/chunk-vendors.css "css/chunk-vendors.$version.css"
mv js/app.js "js/app.$version.js"
mv js/chunk-vendors.js "js/chunk-vendors.$version.js"

precacheFile=`find . -name 'precache-manifest*'`
mv $precacheFile "precache-manifest.$version.js"

echo "
Cleaning unnecessary files..."
rm favicon.ico
rm index.html

echo "
Replacing server's webclient folder"
cd ../..
rm -R server/src/public/webclient
cp -R client/dist server/src/public/webclient
rm -R server/src/public/img
mv server/src/public/webclient/img server/src/public/img

echo "
Done.

You should now check and modify content of 'precache-manifest' file,
according to server/src/public/webclient folders structure and file names.

See you next release. Bye!"
