#!/bin/bash
set -e

BRANCH_PATH="/etc/trup/source.conf"
. "$BRANCH_PATH"

#to correctly display the branches in steam, we need to use the SteamOS branched rel, rc, beta and staging. Since our branches have different names, we need to translate them.

if [[ $# -eq 1 ]]; then
  case "$1" in
    "-c")
      case "$BRANCH" in
        "stable")
          echo "rel"
          exit 0
          ;;
        "testing")
          echo "beta"
          exit 0
          ;;
        "unstable")
          echo "rc"
          exit 0
          ;;
        *)
          # This can happen on CI/dev builds or when downgrading from a newer build that knows of more branches.  The update
          # path should decide how to handle it.
          echo >&2 "Warning: Unrecognized currently selected branch name '$BRANCH', updates may not succeed."
          echo "$BRANCH"
          exit 0
          ;;
      esac
      ;;
    "-l")
      echo rel
      echo beta
      echo rc
      exit 0
      ;;
    "rel")
      sed -i "s/^BRANCH=.*/BRANCH=stable/" "$BRANCH_PATH"
      exit 0
      ;;
    "beta")
      sed -i "s/^BRANCH=.*/BRANCH=testing/" "$BRANCH_PATH"
      exit 0
      ;;
    "rc")
      sed -i "s/^BRANCH=.*/BRANCH=unstable/" "$BRANCH_PATH"
      exit 0
      ;;
  esac
fi

echo "Usage: steamos-select-branch <-rel|beta|rc>" 1>&2
