#!/bin/sh

LDFLAGS=
GCFLAGS=
PREFIX=/usr/local

CONFIG_FILE=$(dirname "$0")/config.redo

usage() {
    echo "$0: [--strip] [--static] [--prefix=<prefix>] [--debug] [--with-tags <tags>]"
}

while [ $# -gt 0 ]; do
    # shellcheck disable=SC2089
    case $1 in
        --static) LDFLAGS="$LDFLAGS -linkmode external -extldflags \"-static\"";;
        --strip) LDFLAGS="$LDFLAGS -w -s";;
        --prefix) PREFIX=$2; shift;;
        --with-tags) TAGS="$TAGS $2"; shift;;
        --debug) GCFLAGS="$GCFLAGS all=-N -l";;
        -h|--help) usage;;
        *) echo "Unknown flag $1"; exit 1;;
    esac
    shift;
done

LDFLAGS=${LDFLAGS# *}
TAGS=${TAGS# *}

#shellcheck disable=SC2090
export LDFLAGS PREFIX DESTDIR TAGS

envsubst <"${CONFIG_FILE}.in" >"${CONFIG_FILE}"
