#!/bin/bash
set -euo pipefail

curr_path=$( cd "$(dirname "${BASH_SOURCE[0]}")" ; pwd -P )
source "$curr_path"/dirs

function color {
  tput bold
  tput setaf "$1"
  shift
  echo "$@"
  tput sgr0
}
function green {
  color 2 "$@"
}
function blue {
  color 4 "$@"
}

blue "Building Starshot: LG/WebOS TV"
echo

# Put the webOS CLI on path
export PATH=$WEBOS_CLI_TV:$PATH

# URLs from: https://jira.twitch.com/browse/IPSYS-2130?focusedCommentId=828999&page=com.atlassian.jira.plugin.system.issuetabpanels%3Acomment-tabpanel#comment-828999
STARSHOT_URLS=(
  "(Main) https://87hn6uil7zv5hvmly5mz.tv.twitch.a2z.com"
  "https://fix7y1uvk4b1ahet92bx.tv.twitch.a2z.com"
  "https://j7738ebzthft3lh5e2g0.tv.twitch.a2z.com"
  "https://dmwhop68wex46a485ze6.tv.twitch.a2z.com"
  "https://osposbc8hid4yfli2i3e.tv.twitch.a2z.com"
  "https://2fsgxn2ycmkf1ilujxl3.tv.twitch.a2z.com"
  "(Local) https://localhost.tv.twitch.tv"
  "(LG Production 1.0) https://lzfnhhvpeppznbnngsio.tv.twitch.tv"
  "(LG Production 2.0) https://ushbsamruavydrm2.lg.tv.twitch.tv"
  "(LG Production 3.0) https://mibnwrctglhhkgb3.lg.tv.twitch.tv"
  "Quit"
)

blue "Staging Environments:"
PS3="Which Staging Environment would you like to target? "
URL=""
select opt in "${STARSHOT_URLS[@]}"
do
    case $opt in
         "(Main) https://87hn6uil7zv5hvmly5mz.tv.twitch.a2z.com")
            URL="https://87hn6uil7zv5hvmly5mz.tv.twitch.a2z.com"
            break
            ;;
         "https://fix7y1uvk4b1ahet92bx.tv.twitch.a2z.com")
            URL="https://fix7y1uvk4b1ahet92bx.tv.twitch.a2z.com"
            break
            ;;
        "https://j7738ebzthft3lh5e2g0.tv.twitch.a2z.com")
            URL="https://j7738ebzthft3lh5e2g0.tv.twitch.a2z.com"
            break
            ;;
        "https://dmwhop68wex46a485ze6.tv.twitch.a2z.com")
            URL="https://dmwhop68wex46a485ze6.tv.twitch.a2z.com"
            break
            ;;
        "https://osposbc8hid4yfli2i3e.tv.twitch.a2z.com")
            URL="https://osposbc8hid4yfli2i3e.tv.twitch.a2z.com"
            break
            ;;
        "https://2fsgxn2ycmkf1ilujxl3.tv.twitch.a2z.com")
            URL="https://2fsgxn2ycmkf1ilujxl3.tv.twitch.a2z.com"
            break
            ;;
        "(Local) https://localhost.tv.twitch.tv")
            URL="https://localhost.tv.twitch.tv"
            break
            ;;
        "(LG Production 1.0) https://lzfnhhvpeppznbnngsio.tv.twitch.tv")
            URL="https://lzfnhhvpeppznbnngsio.tv.twitch.tv"
            break
            ;;
        "(LG Production 2.0) https://ushbsamruavydrm2.lg.tv.twitch.tv")
            URL="https://ushbsamruavydrm2.lg.tv.twitch.tv"
            break
            ;;
        "(LG Production 3.0) https://mibnwrctglhhkgb3.lg.tv.twitch.tv")
            URL="https://mibnwrctglhhkgb3.lg.tv.twitch.tv"
            break
            ;;
        "Quit")
            echo "Exiting"
            exit
            ;;
        *) echo "Invalid option '$REPLY'";;
    esac
done
echo

yarn clean

BUILD_DIR="$WEBOS_DIR"/build

pushd "$WEBOS_DIR"/../laser-array
BUILD_COMMIT=$(git rev-parse HEAD)
REACT_APP_STARSHOT_URL="$URL" REACT_APP_GIT_COMMIT="$BUILD_COMMIT" yarn build
cp -R build "$WEBOS_DIR"
popd

# index.html must have elevated permissions or you encounter the
# following error when launching on a TV:
#
# UNABLE TO LOAD (Error Code: -10)
# This may be a temporary issue. Please try again later.
#
chmod 755 "$BUILD_DIR"/index.html
# Static Assets will fail to load if file permissions are too
# restrictive
chmod -R 755 "$BUILD_DIR"/static

green "Built Laser Array with Starshot domain set to '$URL'"

# Package
cp -R "$WEBOS_DIR"/src/* "$BUILD_DIR"
ares-package --outdir "$WEBOS_DIR"/dist "$BUILD_DIR"

green "Starshot built for LG/WebOS TV!"
