#!/bin/bash
#
# package/common/verdat.sh
#
# Write out the vergen vars used in the Makefile.inc to a 
# Makefile.local that is included in the Makefile.
#
# Why? Because the TeamCity build caches the local
# git repo so it isn't fully visible when running the build in
# a vagrant instance.

vergen="perl tools/vergen"

OXI_VERSION="$($vergen --format version 2>/dev/null)"
TT_VERSION_SYMBOLS="$($vergen --dump --dumpformat '--define KEYWORD=\"VALUE\" ' 2>/dev/null)"

for i in $($vergen --list-formats 2>/dev/null); do
    value=$($vergen --format $i 2>/dev/null)
    if [ -n "$value" ]; then
        TT_VERSION_SYMBOLS="$TT_VERSION_SYMBOLS --define $i=\"$value\""
    fi
done

OPENSSL_PREFIX=/opt/myperl/ssl
OPENSSL_INCLUDE=$OPENSSL_PREFIX/include
OPENSSL_INC=$OPENSSL_INCLUDE
OPENSSL_LIB=$OPENSSL_PREFIX/lib

case "$1" in
    bash)
        cat <<EOF
# Generated by $0
export OXI_VERSION="$OXI_VERSION"
export TT_VERSION_SYMBOLS="${TT_VERSION_SYMBOLS//\"/\"}"

export OPENSSL_PREFIX="$OPENSSL_PREFIX"
export OPENSSL_INCLUDE="$OPENSSL_INCLUDE"
export OPENSSL_INC="$OPENSSL_INC"
export OPENSSL_LIB="$OPENSSL_LIB"
echo "WARNING: Using local makefile generated by $0 $1"
EOF
        ;;
    make|*)
        cat <<EOF
# Generated by $0
export OXI_VERSION = $OXI_VERSION
export TT_VERSION_SYMBOLS = $TT_VERSION_SYMBOLS

export OPENSSL_PREFIX = $OPENSSL_PREFIX
export OPENSSL_INCLUDE = $OPENSSL_INCLUDE
export OPENSSL_INC = $OPENSSL_INC
export OPENSSL_LIB = $OPENSSL_LIB
\$(warning Using local makefile generated by $0 $1)
EOF
        ;;
esac
