#!/usr/bin/env bash
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
set -e
. "$STREAMPIPES_WORKDIR/bin/common"

STREAMPIPES_ENVIRONMENTS_PATH=$STREAMPIPES_WORKDIR/environments

cli_help_env() {
  cat <<EOF
View current environment or inspect and select StreamPipes environment.

Usage: streampipes env [OPTIONS]

Example:
# list all available environments
streampipes env --list

# inspect services in environments
streampipes env --inspect backend

# set specific environment, here 'backend'
streampipes env --set backend

Options:
  -i, --inspect   Inspect services in environment
  -l, --list      List all available environments
  -s, --set       Set environment
EOF

  exit 1
}

[ "$1" == '--help' ] || [ "$1" == '-h' ] && cli_help_env

if [ "$#" -gt 3 ]; then
    fatal "Illegal number of arguments, see 'streampipes ${0##*/} --help'"
fi

if ! docker info > /dev/null 2>&1; then
  echo "Streampipes environment uses docker, and it isn't running - please start docker and try again!"
  exit 1
fi

while [[ "$#" -gt 0 ]]; do
    case $1 in
        -i|--inspect)
          if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
            inspect_environment="$2"
            shift 2
          else
            fatal "Environment for $1 is missing" >&2
          fi
          ;;
        -l|--list) list_environments=true; shift ;;
        -s|--set)
          if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
            set_environment="$2"
            shift 2
          else
            fatal "Environment for $1 is missing" >&2
          fi
          ;;
        *) fatal "Unsupported command $1, see 'streampipes ${0##*/} --help'" >&2 ;;
    esac
done

show_curr_environment(){
  if [ ! -f "$STREAMPIPES_WORKDIR/.spenv" ]; then
    fatal "No environment set. See 'streampipes ${0##*/} --help'"
  else
    get_curr_environment
    info "Current environment: $curr_environment"
  fi
}

check_environment(){
  template=$STREAMPIPES_ENVIRONMENTS_PATH/$1
  if [ ! -f "$template" ]; then
    fatal "Environment not found '$1'. see 'streampipes ${0##*/} --list'"
  fi
}

list_environments(){
  info "Available StreamPipes environment templates:"
  for entry in "$STREAMPIPES_ENVIRONMENTS_PATH"/*
  do
    echo "${entry##*/}"
  done
}

set_environment(){
  check_environment $set_environment
  info "Set environment to '$set_environment'"
  cp $STREAMPIPES_ENVIRONMENTS_PATH/$set_environment $STREAMPIPES_WORKDIR/.spenv
  set_lf_line_encoding $STREAMPIPES_WORKDIR/.spenv
}

inspect_environment(){
  check_environment $inspect_environment
  search_str="[environment:"


  info "Inspect services in environment '$inspect_environment'"
  while IFS='' read -r line; do
    [[ -n "$line" && "$line" != *"$search_str"* && "$line" != [[:blank:]#]* ]] && echo "$line"
  done < "$STREAMPIPES_ENVIRONMENTS_PATH/$inspect_environment"
}

if [ "$list_environments" = true ]; then
  list_environments
fi

if [ -n "$inspect_environment" ]; then
  inspect_environment
fi

if [ -n "$set_environment" ]; then
  if is_streampipes_running; then
    warning "Cannot change template on already running environment. Stop it before changing 'streampipes down'."
  else
    set_environment
  fi
fi

if [[ -z "$1" && "$list_environments" != true && ! -n "$inspect_environment" \
      && ! -n "$set_environment" ]]; then
  show_curr_environment
fi
