#!/bin/bash
#
#   configure - Configure a product
#
#   Usage: configure --parts "app, web" --password "password" --passfile path ...
#
#   Manages git-crypt files, submodules and parts. Defaults to --passfile /etc/farm/gpg.key
#

GPG_PASSFILE=/etc/farm/gpg.key

PARTS="app, ui, web"
while [[ $# -gt 0 ]] ; do
    arg="$1"
    case ${arg} in
        --passfile)
            GPG_PASSFILE="${2}"
            shift ; shift
            ;;
        --password)
            GPG_PASSWORD="${2}"
            shift ; shift
            ;;
        --parts)
            PARTS="${2}"
            shift ; shift
            ;;
        *)
            break
            ;;
    esac
done

PARTS=$(echo ${PARTS} | tr ',' ' ')

#
#   Decrypt secrets
#
if [ -x ".git-crypt" -a ! -f .decrypted ] ; then
    echo "Decrypt secrets"
    if [ -z "${GPG_PASSWORD}" -a -f ${GPG_PASSFILE} ] ; then
        GPG_PASSWORD=$(cat ${GPG_PASSFILE})
    fi
    if [ "${GPG_PASSWORD}" != "" ] ; then
        gpg --pinentry-mode loopback --no-tty --passphrase "${GPG_PASSWORD}" --yes \
            --output .git-crypt/keys/default/0/decrypted.key \
            --decrypt .git-crypt/keys/default/0/*.gpg
        git-crypt unlock .git-crypt/keys/default/0/decrypted.key
    else
        git-crypt unlock
    fi
    if [ $? != 0 ] ; then
        echo "Git unlock failed"
        exit 1
    fi
    >.decrypted
fi

git submodule update --init --recursive

for PART in ${PARTS}
do
    [ ! -d ${PART} ] && continue
    cd ./${PART}
    if [ -f configure ] ; then
        echo -e "\nConfigure ${PART}"
        PART=${PART} ./configure $*
    fi
    [ $? != 0 ] && exit 2
    cd ..
done
