#!/bin/bash
# Update one production Odenza portal directory from git. Keeps .env, storage,
# config, vendor, and a reviewed server patch. Rolls the directory back if login fails.
set -eu

DIR_NAME="${1:?directory name}"
BRANCH="${2:?branch}"
SHA="${3:?sha}"
MODE="${4:-apply}"

ROOT=/data/websites
LIVE="$ROOT/$DIR_NAME"
NEW="$ROOT/$DIR_NAME.new"
KEEP_DIR=/data/portal-deploy/keep/$DIR_NAME
BASE_BRIDGE=7bc618337fc68581270ff80ba634d2728cd8acee
export GIT_SSH=/data/portal-deploy/git-ssh

case "$DIR_NAME" in
  td.odenza.redtag.ca)
    REPO=git@github.com:redlabelvacations/td-odenza.redtag.ca.git
    HOST=travel.tdreferralrewards.com
    export PORTAL_DEPLOY_KEY=/data/portal-deploy/github_deploy
    ;;
  atb.odenza.redtag.ca)
    REPO=git@github.com:redlabelvacations/atb-odenza.redtag.ca.git
    HOST=travel.atbmyrewards.com
    export PORTAL_DEPLOY_KEY=/data/portal-deploy/github_deploy_atb
    ;;
  atbbusiness.odenza.redtag.ca)
    REPO=git@github.com:redlabelvacations/atb-odenza.redtag.ca.git
    HOST=travel.atbmybusinessrewards.com
    export PORTAL_DEPLOY_KEY=/data/portal-deploy/github_deploy_atb
    ;;
  *)
    echo "unknown directory $DIR_NAME" >&2
    exit 1
    ;;
esac

if [ "$BRANCH" != "master" ]; then
  echo "production deploy only accepts master (got $BRANCH)" >&2
  exit 1
fi

if [ ! -f "$LIVE/.env" ]; then
  echo "missing .env on $LIVE" >&2
  exit 1
fi

rm -rf "$NEW"
GIT_SSH="$GIT_SSH" git clone -b "$BRANCH" "$REPO" "$NEW"
cd "$NEW"
git checkout "$SHA"
got=$(git rev-parse HEAD)
if [ "$got" != "$SHA" ]; then
  echo "checked out $got, wanted $SHA" >&2
  exit 1
fi

cp -a "$LIVE/.env" "$NEW/.env"
rm -rf "$NEW/storage"
cp -a "$LIVE/storage" "$NEW/storage"

if [ -f "$KEEP_DIR/OdenzaBridge.php" ]; then
  newblob=$(git rev-parse "HEAD:app/Libraries/Odenza/OdenzaBridge.php")
  if [ "$newblob" != "$BASE_BRIDGE" ]; then
    echo "OdenzaBridge.php changed in git ($newblob). Server patch was not applied." >&2
    exit 1
  fi
  cp "$KEEP_DIR/OdenzaBridge.php" "$NEW/app/Libraries/Odenza/OdenzaBridge.php"
fi

echo "---DIFF---"
diff -rq \
  -x .env -x .git -x vendor -x vendor-old -x vendor.zip -x old-site -x storage -x node_modules -x .new \
  "$LIVE" "$NEW" || true
echo "---END DIFF---"

if [ "$MODE" = "diff" ]; then
  echo "diff only, live directory unchanged"
  exit 0
fi

if [ -d "$LIVE/config" ]; then
  cp -a "$LIVE/config/." "$NEW/config/"
fi
if [ -d "$LIVE/public/build" ]; then
  mkdir -p "$NEW/public/build"
  cp -a "$LIVE/public/build/." "$NEW/public/build/"
fi

for rel in public/js public/css public/js_0222 public/js_0311 public/js_0529 public/js_0807 public/site-check.php .env_old .env_prod public/img/icons/amenities-icon-defs.svg public/img/icons/icon-defs_1206.svg; do
  if [ -e "$LIVE/$rel" ] && [ ! -e "$NEW/$rel" ]; then
    mkdir -p "$NEW/$(dirname "$rel")"
    cp -a "$LIVE/$rel" "$NEW/$rel"
  fi
done

rm -rf "$NEW/vendor"
cp -a "$LIVE/vendor" "$NEW/vendor"
if [ -d "$LIVE/bootstrap/cache" ]; then
  mkdir -p "$NEW/bootstrap/cache"
  cp -a "$LIVE/bootstrap/cache/." "$NEW/bootstrap/cache/"
fi
echo "kept the production vendor"

chown -R redtag:redtag "$NEW"
chmod -R a+rX "$NEW"
chmod -R a+rwX "$NEW/storage" "$NEW/bootstrap/cache"

bak="$ROOT/$DIR_NAME.bak.$(date +%Y%m%d%H%M%S)"
mv "$LIVE" "$bak"
if ! mv "$NEW" "$LIVE"; then
  mv "$bak" "$LIVE"
  echo "swap failed, restored $bak" >&2
  exit 1
fi

code=$(curl -sk -o /dev/null -w '%{http_code}' --resolve "${HOST}:443:127.0.0.1" --connect-timeout 20 "https://${HOST}/session/login" || true)
echo "HTTP $code https://${HOST}/session/login"
case "$code" in
  200|302)
    mkdir -p /data/portal-deploy/current
    printf '%s %s\n' "$SHA" "$BRANCH" > "/data/portal-deploy/current/$DIR_NAME"
    echo "deployed $DIR_NAME $SHA"
    ;;
  *)
    rm -rf "$NEW"
    mv "$LIVE" "$NEW.failed"
    mv "$bak" "$LIVE"
    echo "rolled back $DIR_NAME after HTTP $code" >&2
    exit 1
    ;;
esac
