#!/bin/sh
# Spec file generator for libraries
# (c) 2000-2013 Bernhard Rosenkraenzer <bero@lindev.ch>

STATIC=false
SNAPSHOT=false
while [ "`echo $1 |cut -b1`" = "-" ]; do
	case "$1" in
	-s|--snapshot)
		SNAPSHOT=true
		;;
	-a|--autotools|--autoconf)
		BUILDSYS=autotools
		;;
	-b|--buildsys)
		shift
		BUILDSYS="$1"
		;;
	-c|--cmake)
		BUILDSYS=cmake
		;;
	-C|--custom-buildsys)
		BUILDSYS=custom
		;;
	-m|--meson)
		BUILDSYS=meson
		;;
	-S|--static)
		STATIC=true
		;;
	esac
	shift
done

[ -z "$BUILDSYS" ] && BUILDSYS=cmake

case $BUILDSYS in
autotools)
	BSDEP="autoconf automake slibtool"
	;;
custom)
	BSDEP=""
	;;
*)
	BSDEP="$BUILDSYS"
	;;
esac

if [ -z "$1" ]; then
	exit 1
fi

NAME=`echo $1 |sed -e "s/\.spec$//"`
if [ -z "$EDITOR" ]; then
	if [ -e /usr/bin/vim ]; then
		EDITOR=/usr/bin/vim
	else
		EDITOR=/bin/vi
	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
	cat >$NAME.spec <<EOF
%define major 0
EOF
	if $SNAPSHOT; then
		cat >>$NAME.spec <<EOF
%define beta %{nil}
%define scmrev %{nil}
EOF
	fi
	cat >>$NAME.spec <<EOF
%define libname %mklibname ${NAME/lib/}
%define devname %mklibname ${NAME/lib/} -d
EOF
	if $STATIC; then
		cat >>$NAME.spec <<EOF
%define staticname %mklibname ${NAME/lib/} -d -s
EOF
	fi

	cat >>$NAME.spec <<EOF

Name:		$NAME
Version:	
EOF

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

	cat >>$NAME.spec <<EOF
Summary:	
URL:		https://github.com/$NAME/$NAME
License:	GPL
Group:		System/Libraries
EOF
	[ -n "$BSDEP" ] && echo "BuildRequires:	$BSDEP" >>$NAME.spec
	[ "$BUILDSYS" != "custom" ] && echo "BuildSystem:	$BUILDSYS" >>$NAME.spec
	cat >>$NAME.spec <<EOF

%description

%package -n %{libname}
Summary:	
Group:		System/Libraries

%description -n %{libname}

%package -n %{devname}
Summary:	Development files for %{name}
Group:		Development/C
Requires:	%{libname} = %{EVRD}

%description -n %{devname}
Development files (Headers etc.) for %{name}.
EOF

	if $STATIC; then
		cat >>$NAME.spec <<EOF

%package -n %{staticname}
Summary:	Static libraries for linking to %{name}
Group:		Development/C
Requires:	%{devname} = %{EVRD}

%description -n %{staticname}
Static libraries for linking to %{name}.

Install this package if you wish to develop or compile applications using
%{name} statically (users of the resulting binary won't need %{name} installed
with static linking).
EOF
	fi

	cat >>$NAME.spec <<EOF

%prep
EOF

	if $SNAPSHOT; then
		cat >>$NAME.spec <<EOF
%if "%{scmrev}" == ""
%autosetup -p1 -n %{name}-%{version}%{beta}
%else
%autosetup -p1 -n %{name}
%endif
EOF
	else
		cat >>$NAME.spec <<EOF
%autosetup -p1
EOF
	fi

	if [ "$BUILDSYS" = "custom" ]; then
		cat >>$NAME.spec <<EOF

%conf

%build
%make_build

%install
%make_install
EOF
	fi

	cat >>$NAME.spec <<EOF

%files
%{_bindir}/*

%files -n %{libname}
%{_libdir}/*.so.%{major}*

%files -n %{devname}
%{_includedir}/*
%{_libdir}/*.so
%{_libdir}/pkgconfig/*
EOF
	if [ "$BUILDSYS" = "cmake" ]; then
		echo '%{_libdir}/cmake/*' >>$NAME.spec
	fi

	if $STATIC; then
		cat >>$NAME.spec <<EOF

%files -n %{staticname}
%{_libdir}/*.a
EOF
	fi
fi
exec $EDITOR "$NAME".spec
