#!/bin/bash

PKG=openbao
if [ ! -f $PKG.spec.in ]; then
    echo "$PKG.spec.in doesn't exist!" >&2
    exit 1
fi
ORG="$(grep ^Source0: $PKG.spec.in|cut -d/ -f4)"
# Check if someone accidentally modified $PKG.spec. This is tricky
# because git does not keep track of whether not a file is readonly.
# This script generates it read-only, so if it isn't writable then it
# is OK.  Otherwise if it is more than 2 seconds newer than the .in 
# most likely it was edited after git checkout.
if [ -w $PKG.spec ]; then
    INTIME="$(stat -c '%Y' $PKG.spec.in)"
    OUTTIME="$(stat -c '%Y' $PKG.spec)"
    if (( $OUTTIME >= $INTIME + 2 )); then
        echo "$PKG.spec is newer than $PKG.spec.in -- was it accidentally edited?" >&2
        exit 2
    fi
fi
VERSION="$(sed -n 's/%global package_version[ \t]*//p' $PKG.spec.in)"

DISTURL="$(sed -n "s/^Source0: //p" $PKG.spec.in|sed -e "s/%{name}/$PKG/g" -e "s/%{package_version}/$VERSION/g")"
DISTBALL="$(basename $DISTURL)"
DIST="$(echo $DISTBALL|sed 's/\.tar.*//')"
if [ ! -f "$DISTBALL" ]; then
    curl -sLO "$DISTURL"
    if [ ! -f "$DISTBALL" ]; then
        echo "Couldn't download $DISTURL!" >&2
        exit 4
    fi
fi
GOMOD=go.mod
NVERS=VERSION_NODE_MODULES.txt
LDEP=LICENSE_DEPENDENCIES.md
NDEP=SPDX_NODE_MODULES.txt
DISTFILES="$GOMOD $NVERS $LDEP $NDEP"
trap "rm -f $DISTFILES" 0
TARPATHS=""
for F in $DISTFILES; do
    TARPATHS="$TARPATHS $DIST/$F"
done
tar --xform "s/^$DIST/./" -xf $DISTBALL $TARPATHS
for F in $DISTFILES; do
    if [ ! -f $F ]; then
        echo "$F not found in $DISTBALL!" >&2
        exit 4
    fi
done
rm -f $PKG.spec

while IFS='' read -r LINE; do
    if [[ "$LINE" = *"bundled provides" ]]; then
        awk '{if (index($1, "/") != 0 && ($1 != "//")) {print "Provides: bundled(golang("$1")) = "gensub("-","~","g",$2)}}' $GOMOD | sort | uniq
        awk '{print "Provides: bundled(nodejs-"$1") = "gensub("-","~","g",$2)}' $NVERS
    elif [[ "$LINE" = "License:"* ]]; then
        MAINLICENSE="$(echo $LINE|sed 's/^License: //')"
        # These get detected by go_vendor_license but not by the tool
        # used upstream, github.com/google/go-licenses.
        # The first is from github.com/apparentlymart/go-textseg/v13/LICENSE
        # and the second from github.com/Nvveen/Gotty/LICENSE
        EXTRALICENSES="Unicode-DFS-2016 BSD-2-Clause-Views"
	# Something went very crazy with the hashicorp/vic dependency in
	# openbao-2.6.0. It has an "open_source_licenses.txt" file that lists
	# a huge number of licenses, many with non-SPDX compatible names.
	# They apply to the whole vic repository, not just the tiny piece
	# used indirectly by OpenBao (hashicorp/vic/pkg/vsphere/tags) which
	# only contains 4 files marked as Apache-2.0, so filter out the rest
	# with awk.
        LICENSES="$((awk '/\/vic\/pkg\/vsphere\// {skip=3} {if (skip > 0) {skip--} else print}' $LDEP | sed -n "s/^\*\*.*License:\*\* //p"|grep -v Unknown;echo $EXTRALICENSES|tr ' ' '\n';sed 's/^BSD$/BSD-3-Clause/;/^Public Domain/d;s/^(\([^ ]*\) AND \([^)]*\))/\1\n\2/' $NDEP)|sort -u|grep -v "^$MAINLICENSE$"|tr '\n' ':'|sed 's/:$//;s/:/ AND /g')"
        if [ -z "$LICENSES" ]; then
            echo "Didn't find any licenses in $LDEP!" >&2
            exit 4
        fi
        rm -f $LDEP
        echo "License: $MAINLICENSE AND $LICENSES"
    else
        echo "$LINE"
    fi
done <$PKG.spec.in >$PKG.spec
chmod 444 $PKG.spec
