#!/bin/bash
# Copyright 2017 Red Hat, Inc.
#
# 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.

#
# Script to download src puppet modules.
# This script can be used in conjuction with upload-puppet-modules
#
set -eu
set -o pipefail

SCRIPT_NAME=$(basename $0)
SCRIPT_HOME=$(cd $(dirname $0); pwd)


function show_options {
    echo "Usage: $SCRIPT_NAME "
    echo
    echo "Options:"
    echo "    -h, --help                               -- print this help."
    echo "    -d, --directory <directory>              -- Puppet modules directory. Required."
    echo "    -m, --modules 'ceph oslo'                -- Puppet modules. Optional. Must be quoted and space delimited"
    echo "    -n, --no-pull                            -- If the module directory exists do not clone or repull the source"
    echo "    -x, --no-extras                          -- Do not download midonet, pacemaker, tripleo"
    echo
    echo Script to download source puppet modules.
    echo This script can be used in conjuction with upload-puppet-modules
    echo
    echo This script uses a list of puppet modules defined in
    echo https://raw.githubusercontent.com/openstack/puppet-openstack-integration/master/openstack_modules.txt
    echo Extra puppet modules are downloaded by default
    exit
}

TEMP=`getopt -o m:hnxd: -l help,modules:,no-pull,no-extras,directory: -n $SCRIPT_NAME -- "$@"`
if [ $? != 0 ]; then
    echo "Terminating..." # >&2
    exit 1
fi

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

MODULES=
MODULE_DEFAULT_LIST=
MODULES_DIRECTORY=
NO_PULL=

# Default module selection
# openstack_modules.txt is the definitive list of puppet modules
: ${MODULE_DEFAULT_LIST_URL:="https://raw.githubusercontent.com/openstack/puppet-openstack-integration/master/openstack_modules.txt"}

# Additional puppet modules may be required
: ${MODULES_EXTRAS:="
midonet
pacemaker
tripleo
"}

while true ; do
    case "$1" in
        -h|--help) show_options; 0 >&2;;
        -d|--directory) MODULES_DIRECTORY=$2 ; shift 2;;
        -m|--modules) MODULES=$2 ; shift 2;;
        -n|--no-pull) NO_PULL=1 ; shift ;;
        -x|--no-extras) MODULES_EXTRAS=""; shift ;;
        --) shift ; break;;
        *) echo "Error: unsupported option $1." ; exit 1;;
    esac
done

if [ -z $MODULES_DIRECTORY ]; then
    show_options;
    exit
fi

if [[ $MODULES="" ]]; then
    MODULE_DEFAULT_LIST=`curl -s --connect-timeout 5 $MODULE_DEFAULT_LIST_URL`
    if [ $? != 0 ]; then
        echo "Error getting the list of puppet modules from $MODULE_DEFAULT_LIST_URL"
    fi
fi

: ${MODULES:="$MODULE_DEFAULT_LIST $MODULES_EXTRAS"}

modules_directory=${MODULES_DIRECTORY%/}

mkdir -p $modules_directory
pushd $MODULES_DIRECTORY

for module in $MODULES; do
    echo "checking $module"
    if [[ -d $module && -z "$NO_PULL" ]]; then
        pushd $module > /dev/null
        git pull
        popd > /dev/null
    elif ! [ -d $module ]; then
        git clone https://opendev.org/openstack/puppet-$module $module
    else
        echo "$module already exists and will not be updated"
    fi
done

popd
