#!/bin/bash
#
# Copyright 2013 Red Hat, Inc.
# All Rights Reserved.
#
# Licensed 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
set -o pipefail

SCRIPT_NAME=$(basename $0)
SCRIPT_HOME=$(dirname $0)

function show_options () {
    echo "Usage: $SCRIPT_NAME [options]"
    echo
    echo "Write devtest defined environment variables to a file."
    echo
    echo "Creates a tripleorc file that can be sourced later to restore"
    echo "evironment variables that are defined by devtest.md"
    echo
    echo "Options:"
    echo "    -f, --file        -- Specify path for tripleorc file."
    echo "                         Defaults to tripleorc in the current directory."
    echo "    -o, --overwrite   -- Overwrite file if it already exists."
    exit $1
}

TEMP=`getopt -o hof: -l help,overwrite,file: -n $SCRIPT_NAME -- "$@"`
if [ $? != 0 ] ; then echo "Terminating..." >&2 ; exit 1 ; fi

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"

while true ; do
    case "$1" in
        -f | --file) FILE=$2; shift 2 ;;
        -o | --overwrite) OVERWRITE=--overwrite; shift 1 ;;
        -h) show_options 0;;
        --) shift ; break ;;
        *) echo "Error: unsupported option $1." ; exit 1 ;;
    esac
done

FILE=${FILE:-"tripleorc"}
OVERWRITE=${OVERWRITE:-""}

# Don't overwrite $FILE if it already exists and the overwrite option
# wasn't provided.
if [ -f $FILE -a -z "$OVERWRITE" ]; then
    echo $FILE exists, not overwriting.
    echo Either delete the file first, or specify -o
    exit 1
fi

rm -f $FILE
touch $FILE

ENV_VARS="TRIPLEO_ROOT
LIBVIRT_DEFAULT_URI
LIBVIRT_NIC_DRIVER
NODE_DIST
DEPLOY_IMAGE_ELEMENT
OVERCLOUD_LIBVIRT_TYPE
DIB_COMMON_ELEMENTS"

for env_var in $ENV_VARS; do
    if [ ! -z "${!env_var}" ]; then
        echo export $env_var=\"${!env_var}\" >> $FILE
    fi
done

# Also write out updated $PATH and $ELEMENTS_PATH
if [ -n "$TRIPLEO_ROOT" ]; then

    # Add a newline for some clarity in the tripleorc file.
    echo >> $FILE

    # When tripleorc is later sourced, we only want to update $PATH and
    # $ELEMENTS_PATH if they haven't already been updated.  Otherwise, we will
    # keep making them longer each time tripleorc is sourced.
    cat >> $FILE <<EOF
SCRIPTS_PATH=\$TRIPLEO_ROOT/tripleo-incubator/scripts
if [[ ! "\$PATH" =~ (^|:)"\$SCRIPTS_PATH"(:|$) ]]; then
    export PATH=\$TRIPLEO_ROOT/tripleo-incubator/scripts:\$PATH
fi

TIE_PATH=\$TRIPLEO_ROOT/tripleo-image-elements/elements
if [[ ! "\$ELEMENTS_PATH" =~ (^|:)"\$TIE_PATH"(:|$) ]]; then
    export ELEMENTS_PATH=\$TIE_PATH\${ELEMENTS_PATH:+":\$ELEMENTS_PATH"}
fi

source devtest_variables.sh
EOF

fi
