#!/usr/bin/env bash

## Configure assets username.
##
## Options
##  set     Set the username (default)
##  get     Get the currently configured username.
##  reset   Reset the username.
##
## Usage:
## * fin assets/username set      Set username
## * fin assets/username get      Get the currently set value
## * fin assets/username reset    Reconfigure the value.

. ${PROJECT_ROOT}/.docksal/scripts/common

readonly DEFAULT_USERNAME="$(whoami)"
readonly EVOLVINGWEB_CONFIG_DIR="$HOME/.config/evolvingweb"
readonly ASSETS_USERNAME_FILE="assets-username"

# Set assets username.
set_username() {
  if [ -f "$EVOLVINGWEB_CONFIG_DIR/$ASSETS_USERNAME_FILE" ]; then
    print_info_message "Assets username is already configured."
    print_info_message "current configured value is $(cat $EVOLVINGWEB_CONFIG_DIR/$ASSETS_USERNAME_FILE)."
  else
    read -p "Enter your assets username[${DEFAULT_USERNAME}] : " USERNAME
    ASSETS_USERNAME=${USERNAME:-$DEFAULT_USERNAME}
    mkdir -p "$HOME/.config/evolvingweb"
    echo "$ASSETS_USERNAME" > "$EVOLVINGWEB_CONFIG_DIR/$ASSETS_USERNAME_FILE"
  fi
}

case "$1" in
  get)
    if [ -f "$EVOLVINGWEB_CONFIG_DIR/$ASSETS_USERNAME_FILE" ]; then
      cat "$EVOLVINGWEB_CONFIG_DIR/$ASSETS_USERNAME_FILE";
    else
      echo "";
    fi
    ;;
  reset)
    rm "$EVOLVINGWEB_CONFIG_DIR/$ASSETS_USERNAME_FILE";
    set_username;
    ;;
  set)
    set_username;
    ;;
  *)
    set_username;
    ;;
esac
