#!/bin/bash
# Spec file generator for perl modules
# (c) 2000-2022 Bernhard Rosenkraenzer <bero@lindev.ch>

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

if echo $NAME |grep -q ^perl-; then
	NAME="$(echo $NAME |cut -b6-)"
fi
if echo $NAME |grep -q ::; then
	NAME="$(echo $NAME |sed -e 's,::,-,g')"
fi

mkdir -p "$ABFDIR/perl-$NAME"
cd "$ABFDIR/perl-$NAME"
if [ ! -e "perl-$NAME.spec" ]; then
	cat >"perl-$NAME.spec" <<EOF
%define module $NAME
%undefine _debugsource_packages

Name:		perl-%{module}
Version:	
Release:	1
Summary:	
URL:		https://metacpan.org/pod/$(echo $NAME |sed -e 's,-,::,g')
Source:		https://cpan.org/modules/by-module/$(echo $NAME |cut -d- -f1)/%{module}-%{version}.tar.gz
License:	Perl (Artistic or GPL)
Group:		Development/Perl
BuildRequires:	perl
BuildArch:	noarch

%description

%prep
%autosetup -p1 -n %{module}-%{version}
perl Makefile.PL INSTALLDIRS=vendor

%build
%make_build

%check
make test

%install
%make_install INSTALLDIRS=vendor

%files
%doc Changes MANIFEST README
%{perl_vendorlib}/*/*
%{_mandir}/man3/*.3pm*
EOF
fi
exec $EDITOR perl-"$NAME".spec
