#!/usr/bin/env nix-shell
#!nix-shell -p nodePackages.firebase-tools -p vault -i bash

vault_path=unix:///run/tokend/sock
deploycmd="deploy"
postdeploy () {
  return
}

if [[ "$(groups)" =~ (.* |^)"users"($| .*) ]] || ! test -f /etc/NIXOS; then
  vault_path=https://vault.int.lukegb.com
  channelname="$(id -un)"
  deploycmd="hosting:channel:deploy $channelname"
  postdeploy () {
    firebase hosting:channel:open $channelname --token="$token"
  }
fi

cd $(nix-build ../.. -A web.lukegbcom)
token="$(vault read --field=token --address="$vault_path" gcp/roleset/lukegbcom-deployer/token)"

firebase $deploycmd --token="$token"
# Do it twice because sometimes it doesn't actually do anything the first time
firebase $deploycmd --token="$token"
postdeploy