#! /bin/bash
# 
# Run from an account directory to update all the child directory's standard files

function argparse {
  PARAMS=""
  while (( "$#" )); do
    case "$1" in
      -h|--help)
        echo Usage: $0 '[-t|--test] [-d|--debug]'
        exit 0
        ;;
      -t|--test)
        TESTING="/bin/echo TESTING: "
        shift
        ;;
      -d|--debug)
        >&2 echo debug: Enabling debugging..
        DEBUG=1
        shift
        ;;
#      -p|--only-path)
#        if [ -n "$2" ] && [ ${2:0:1} != "-" ]; then
#          ONLY_PATH=$2
#          shift 2
#        else
#          echo "Error: Argument for $1 is missing" >&2
#          exit 1
#        fi
#        ;;
      -*|--*=) # unsupported flags
        echo "Error: Unsupported flag $1" >&2
        exit 1
        ;;
      *) # preserve positional arguments
        PARAMS="$PARAMS $1"
        shift
        ;;
    esac
  done
  # set positional arguments in their proper place
  eval set -- "$PARAMS"
}

# Main
argparse $*

SHORT_PWD=$( basename ${PWD}  )
PARENT_PWD=$( basename $( cd .. && pwd ) )
[[ $DEBUG == 1 ]] && >&2 echo debug: PWD=$PWD
[[ $DEBUG == 1 ]] && >&2 echo debug: SHORT_PWD=$SHORT_PWD

# Sanity Checking
if [[ $SHORT_PWD == "000-skeleton" ]]; then
  >&2 echo Error: Cannot run from skeleton directory. Exiting...
  exit 1
fi

if [[ $SHORT_PWD =~ ^[0-9]{3}-.* ]]; then
  >&2 echo Error: We appear to be in a module directory. Please run from the account directory you wish to update. Exiting...
  exit 2
fi

if [[ ! $PARENT_PWD =~ ^aws ]]; then
  >&2 echo Error: We do not appear to be in an account directory. Failing...
  exit 3
fi

if [[ $SHORT_PWD =~ legacy ]]; then
  >&2 echo Error: This account is appears to be a legacy account. This script will not work.
  exit 4
fi

if [[ -f UNUSED.ACCOUNT ]]; then
  >&2 echo Error: This account is marked as unused. Failing...
  exit 4
fi

if [[ -f UNMANAGED.ACCOUNT ]]; then
  >&2 echo Error: This account is marked as unmanaged. Failing...
  exit 4
fi

for i in `seq -f "../../../000-skeleton/%g*" 0 9`; do
  MODULE=$( basename $i )
  [[ $TESTING ]] && >&2 echo debug: Processing module $MODULE...
  if [[ -d $MODULE ]]; then
    [[ $DEBUG ]] && >&2 echo debug: Module exists. Updating carefully.
    pushd . > /dev/null
    cd $MODULE
    update_from_skeleton $*
    popd > /dev/null
  else
    if [[ $MODULE =~ \* ]]; then
      continue
    fi

    # Special case: 020-attach-transit-gateway-to-standard-vpc
    if [[ $MODULE == "020-attach-transit-gateway-to-standard-vpc" && $SHORT_PWD =~ -c2$ ]]; then
      echo Skipping $MODULE for C2 Accounts
      continue
    fi

    # Special case: 005-iam in common 
    if [[ $SHORT_PWD =~ common-services && \
          ( $MODULE == "005-iam" || \
            $MODULE == "010-standard-vpc" || \
            $MODULE == "020-attach-transit-gateway-to-standard-vpc" || \
            $MODULE == "025-test-instance" \
          ) \
       ]]; then
      # No standard vpc in common services. It doesn't break things, just doesn't seem appropriate at this time.
      echo Skipping $MODULE for Common Services Accounts
      continue
    fi

    read -p "$MODULE doesn't presently exist. Create it? [y/N]? " -n 1 -r
    echo ""
    if [[ $REPLY =~ ^[Yy]$ ]]
    then
      [[ -d ../../../000-skeleton/$MODULE ]] && ${TESTING} cp -rv ../../../000-skeleton/$MODULE .
    else
      echo Skipping...
    fi
  fi
done

echo Finished. Don\'t forget to update account.hcl. 
echo '--------------- 000-skeleton/account.hcl ----------------'
cat ../../../000-skeleton/account.hcl
echo '---------------       finished           ----------------'

