#!/bin/bash
# Spec file generator for Golang module packages
#

URL=""
BOOTSTRAP=false
DEVEL=false
SNAPSHOT=false
while [ "`echo $1 |cut -b1`" = "-" ]; do
	case "$1" in
	-a|--altgoipaths)
		ALTGOIPATHS="$2"
		shift
		;;
	-b|--bootstrap)
		BOOTSTRAP=true
		;;
	-d|--devel)
		DEVEL=true
		;;
	-g|--goipath)
		GOIPATH="$2"
		shift
		;;
	-s|--snapshot)
		SNAPSHOT=true
		;;
	-u|--url)
		URL="$2"
		shift
		;;
	esac
	shift
done

NAME=`echo $1 |sed -e "s/\.spec$//"`
if [ -n "$URL" ]; then
	[ -z "$GOIPATH" ] && GOIPATH="`echo $URL |sed -e 's,.*://,,'`"
	[ -z "$NAME" ] && NAME=golang-"`echo $GOIPATH |tr / - |sed 's,\.com-,-,'`"
	MNAME="`echo $NAME |sed -e 's,.*-,,'`"
fi

[ -z "$EDITOR" ] && EDITOR="$VISUAL"
if [ -z "$EDITOR" ]; then
	if [ -e /usr/bin/vim ]; then
		EDITOR=/usr/bin/vim
	fi
fi
ID="`cat /etc/passwd |grep "^$(id -un):" |cut -d: -f5` <`id -un`@`hostname |cut -d. -f2-`>"
[ -e ~/.vs ] && source ~/.vs
[ -z "$ABFDIR" ] && ABFDIR=~/abf

mkdir -p "$ABFDIR/$NAME"
cd "$ABFDIR/$NAME"
if [ ! -e "$NAME.spec" ]; then
	if $DEVEL; then
		cat >>"$NAME.spec" <<EOF
%global debug_package %{nil}

EOF
	fi
	if $BOOTSTRAP; then
		cat >>"$NAME.spec" <<EOF
%bcond_without bootstrap2

EOF
	fi
	if $BOOTSTRAP; then
		cat >>"$NAME.spec" <<EOF
# Run tests in check section
%bcond_with check

EOF
	else
		cat >>"$NAME.spec" <<EOF
# Run tests in check section
%bcond_without check

EOF
	fi
	if $SNAPSHOT; then
		cat >>"$NAME.spec" <<EOF
%define beta %{nil}
%define scmrev %{nil}

EOF
	fi
	cat >>"$NAME.spec" <<EOF
# $URL
%global goipath		$GOIPATH
EOF
	if [ -n "$ALTGOIPATHS" ]; then
		cat >>"$NAME.spec" <<EOF
%global goaltipaths	$ALTGOIPATHS
EOF
	fi
	cat >>"$NAME.spec" <<EOF
%global forgeurl	$URL
Version:		

%gometa

Summary:	
Name:		$NAME

EOF
	if $SNAPSHOT; then
		cat >>"$NAME.spec" <<EOF
%if "%{beta}" == ""
%if "%{scmrev}" == ""
Release:	1
Source0:	%{name}-%{version}.tar.xz
%else
Release:	0.%{scmrev}.1
Source0:	%{name}-%{scmrev}.tar.xz
%endif
%else
%if "%{scmrev}" == ""
Release:	0.%{beta}.1
Source0:	%{name}-%{version}%{beta}.tar.xz
%else
Release:	0.%{beta}.0.%{scmrev}.1
Source0:	%{name}-%{scmrev}.tar.xz
%endif
%endif
EOF
	else
		cat >>"$NAME.spec" <<EOF
Release:	1
EOF
		if [ -n "$URL" ]; then
			cat >>"$NAME.spec" <<EOF
Source0:	$URL/archive/v%{version}/${MNAME}-%{version}.tar.gz
EOF
		else
			cat >>"$NAME.spec" <<EOF
Source0:	https://github.com/$NAME/$NAME/archive/v%{version}/${NAME}-%{version}.tar.gz
EOF
		fi
	fi

	if $BOOTSTRAP; then
		cat >>"$NAME.spec" <<EOF
%if %{with bootstrap2}
# Generated from Source100
Source3:	vendor.tar.zst
Source100:	golang-package-dependencies.sh
%endif
EOF
	fi

	if [ -n "$URL" ]; then
		echo "URL:		$URL" >>"$NAME.spec"
	else
		echo 'URL:		https://github.com/$NAME/$NAME' >>"$NAME.spec"
	fi

	cat >>"$NAME.spec" <<EOF
License:	GPL
Group:		Development/Other
EOF
	echo 'BuildRequires:	compiler(go-compiler)' >>"$NAME.spec"

	cat >>"$NAME.spec" <<EOF

%description
%{summary}

EOF

	if $DEVEL; then
		cat >>"$NAME.spec" <<EOF
%package devel
Summary:	%{summary}
Group:		Development/Other
BuildArch:	noarch

%description devel
%{description}

This package contains library source intended for
building other packages which use import path with
%{goipath} prefix.

EOF
	fi

	echo '%prep' >>"$NAME.spec"
	if $SNAPSHOT; then
		echo '%autosetup -p1 -n %{name}%{!?scmrev:-%{version}%{?beta:%{beta}}}' >>"$NAME.spec"
	elif [ -n "$URL" ]; then
		echo "%autosetup -p1 -n $MNAME-%{version}" >>"$NAME.spec"
	else
		echo '%autopatch -p1' >>"$NAME.spec"
	fi

	if $BOOTSTRAP; then
		cat >>"$NAME.spec" <<EOF

rm -rf vendor

%if %{with bootstrap2}
tar xf %{S:3}
%endif
EOF
	fi

	cat >>"$NAME.spec" <<EOF

%build
%gobuildroot
for cmd in \$(ls -1 cmd) ; do
	%gobuild -o _bin/\$cmd %{goipath}/cmd/\$cmd
done

%install
%goinstall
for cmd in \$(ls -1 _bin) ; do
	install -Dpm 0755 _bin/\$cmd %{buildroot}%{_bindir}/\$cmd
done

EOF
	if [ -n "$ALTGOIPATHS" ]; then
		cat >>"$NAME.spec" <<EOF
# install alternative name
ln -fs . %{buildroot}%{_datadir}/gocode/src/%{goaltipaths}
echo \"%{_datadir}/gocode/src/%{goaltipaths}\" >> devel.file-list
EOF
	fi

	cat >>"$NAME.spec" <<EOF

%check
%if %{with check}
%gochecks
%endif

%files
%license LICENSE
%doc README.md
%{_bindir}/*

EOF

	if $DEVEL; then
		cat >>"$NAME.spec" <<EOF
%files devel -f devel.file-list
%license LICENSE
%doc README.md

EOF
	fi

	if $BOOTSTRAP; then
		cat >>"package-source.sh" <<EOF
#!/bin/bash
set -e

if [[ \`grep -Es "%bcond_without\sbootstrap2" *.spec\` ]]
then
	sh ./golang-package-dependencies.sh
fi
EOF

		cat >>"golang-package-dependencies.sh" <<EOF
#!/bin/sh
# Needed for second stage bootstrapping (first stage bootstrap is installing
# from the binary package): This script builds the go library in online mode,
# creating a local repository with all the dependencies (including those that
# need to be built with go).
#
# (c) 2020 Bernhard Rosenkraenzer <bero@lindev.ch>
# (c) 2024 mandian <mandian@tutanota.com>
# Released under the terms of the GPLv3

cd "\`dirname \$0\`"
NAME="\`grep ^Name $NAME.spec |cut -d: -f2- |xargs echo\`"
VERSION="\`grep ^Version $NAME.spec |cut -d: -f2- |xargs echo\`"
URL="\`grep ^URL $NAME.spec |cut -d: -f2- |xargs echo\`"
SOURCE="\`grep ^Source0 $NAME.spec |cut -d: -f2- |sed -e "s,%{name},\$NAME,g; s,%{version},\$VERSION,g;" |xargs echo\`"
MAJOR="\`echo \$VERSION |cut -d. -f1\`"

PKG="\`echo \$SOURCE |sed -e "s,.*\/,," |xargs echo\`"
DIR="\`echo \$PKG |sed -e "s,\.tar.*,," |xargs echo\`"

#export VENDOR=\`pwd\`/vendor
#rm -rf \$VENDOR

[ -e \$PKG ] || wget \$SOURCE
tar xf \$PKG

pushd \$DIR 1>/dev/null
go mod vendor

find vendor
tar cf ../vendor.tar vendor
zstd --ultra -22 --rm -f ../vendor.tar

popd 1>/dev/null
rm -rf repository \$DIR

exit 0
EOF
	fi
fi


exec "$EDITOR" "$NAME".spec

