#!/bin/bash

echo "This script is DEPRECATED. Please use fuel-mirror utility!"

# This shell script was wraps the fuel-mirror utility to provide backward compatibility
# with previous version of tool.

usage() {
cat <<EOF
Usage: `basename $0` [options]

Create and update local mirrors of MOS and/or Ubuntu.

IMPORTANT!
If NO parameters specified, this script will:
- Create/Update both MOS and Ubuntu local mirrors
- Set them as repositories for existing NEW environments in Fuel UI
- Set them as DEFAULT repositories for new environments

Options:

  -h| --help          This help screen.
  -d| --no-default    Don't change default repositories for new environments
  -a| --no-apply      Don't apply changes to Fuel environments
  -M| --mos           Create/Update MOS local mirror only
  -U| --ubuntu        Create/Update Ubuntu local mirror only
  -p| --password      Fuel Master admin password (defaults to admin)
  -v| --verbose       Add detailed output

EOF
}

# Parse options
OPTS=`getopt -o hdaMUNp: -l help,no-default,no-apply,mos,ubuntu,password:,dry-run,verbose -- "$@"`
if [ $? != 0 ]; then
    usage
    exit 1
fi

eval set -- "$OPTS"

CMD_OPTS="--pattern=ubuntu"
REPO_GROUPS=""

while true ; do
    case "$1" in
        -h| --help        ) usage ; exit 0;;
        -d | --no-default ) OPT_NO_DEFAULT=1; shift;;
        -a | --no-apply   ) OPT_NO_APPLY=1; shift;;
        -N | --dry-run    ) EXEC_PREFIX="echo  EXEC  "; shift;;
        -M | --mos        ) REPO_GROUPS="$REPO_GROUPS mos"; shift;;
        -U | --ubuntu     ) REPO_GROUPS="$REPO_GROUPS ubuntu"; shift;;
        -p | --password   ) CMD_OPTS="$CMD_OPTS --fuel-password=$2"; shift; shift;;
        -v | --verbose    ) CMD_OPTS="$CMD_OPTS --verbose"; shift;;
        --                ) shift; break;;
        *                 ) break;;
    esac
done

if [[ "$@" != "" ]]; then
    echo "Invalid option -- $@"
    usage
    exit 1
fi

if [[ "$REPO_GROUPS" == "" ]]; then
    REPO_GROUPS="mos ubuntu"
fi

CMD_OPTS="$CMD_OPTS --group $REPO_GROUPS"

$EXEC_PREFIX fuel-mirror create ${CMD_OPTS}

if [[ "$OPT_NO_DEFAULT" == "" ]]; then
    CMD_OPTS="$CMD_OPTS --default"
fi

if [[ "$OPT_NO_APPLY" == "" ]]; then
    $EXEC_PREFIX fuel-mirror apply ${CMD_OPTS}
fi
