#! /usr/bin/perl -w
#####
#create-spec [-h] < xxx.spec.in > package/xxx.spec
#
#Create a spec file for LiMaL package
#
#Available macros in the xxx.spec.in:
#  @RPMNAME@		- read from file RPMNAME
#  @RPMARCH@            - read architecture (list) from RPMARCH file
#  @VERSION@		- read from file VERSION
#  @MAINTAINER@		- read from file MAINTAINER
#  @RPMLIB@		- RPMNAME without limal- prefix
#
#  @HEADER-COMMENT@	- writes the SuSE .spec comment
#  @HEADER@		- writes the usual header except BuildArch:,
#  @HEADER-DEVEL@	- writes the -devel subpackage header, similar to @HEADER@
#  @HEADER-DEVEL-1@	- writes the -devel subpackage header, similar to @HEADER@ (used by version >= 1.4)
#  @HEADER-PERL@	- writes the -perl subpackage header, similar to @HEADER@
#  @HEADER-INSTSYS@	- writes the -instsys subpackage header, similar to @HEADER@
#  @PREP@		- writes %prep with %setup
#  @BUILD@		- writes %build with usual make
#  @BUILD-QT@		- writes %build with usual make for qt stuff
#  @BUILD-PO@		- writes %build with special make for translations
#  @INSTALL@		- writes %install with usual make install and make check
#  @INSTALL-PO@		- writes %install with special make install for translations
#  @POST@      - writes %post with call to ldconfig
#  @POSTUN@      - writes %postun with call to ldconfig
#  @CLEAN@		- writes %clean with removing RPM_BUILD_ROOT
#####
# Authors: Jan Holesovsky <kendy@suse.cz>, 2001
#          Arvin Schnell <arvin@suse.de>, 2002

# Import the content of some files
$RPMNAME = `cat RPMNAME`;
$RPMARCH = `cat RPMARCH 2>/dev/null`;
$RPMLIB  = $RPMNAME; $RPMLIB  =~ s/^[^-]*-//g;
$VERSION = `cat VERSION`;
$MAINTAINER = `cat MAINTAINER`;
$LICENSE =  "GPL-2.0+";
$PREFIX = "/usr";

# Clean them and check them
chomp $RPMNAME;
chomp $RPMARCH;
chomp $RPMLIB;
chomp $VERSION;
chomp $MAINTAINER;
if ($VERSION eq "" || $RPMNAME eq "" || $MAINTAINER eq "")
{
    die "Start this only in LiMaL toplevel project directories!\n";
}

# Check if we need a -BuildArchitectures tag
$BUILD_ARCHS = '';
if ($RPMARCH =~ /\S+/)
{
	$BUILD_ARCHS = "BuildArchitectures: $RPMARCH";
}

# Default values
$limaldatadir = "$PREFIX/share/limal";

if ($#ARGV > 0)
{
    # Isn't the following too anti-perl? :-)
    my $message = `sed -e '/^#####/,/^#####/!d' -e 's/^#*//' $0`;
    die "$message";
}

# Definition of the macros
%macros =
(
    "\@RPMNAME\@" => "$RPMNAME",

    "\@RPMARCH\@" => "$RPMARCH",

    "\@RPMLIB\@" => "$RPMLIB",

    "\@VERSION\@" => "$VERSION",

    "\@MAINTAINER\@" => "$MAINTAINER",

    "\@HEADER-COMMENT\@" =>
"#
# spec file for package $RPMNAME (Version $VERSION)
#
# Copyright (c) 2006-2011 SUSE LINUX Products GmbH, Nuernberg, Germany.
#
# All modifications and additions to the file contributed by third parties
# remain the property of their copyright owners, unless otherwise agreed
# upon. The license for this file, and modifications and additions to the
# file, is the same license as for the pristine package itself (unless the
# license for the pristine package is not an Open Source License, in which
# case the license is the MIT License). An \"Open Source License\" is a
# license that conforms to the Open Source Definition (Version 1.9)
# published by the Open Source Initiative.
#
# Please submit bugfixes or comments via http://bugs.opensuse.org/
#

# norootforbuild",
#",

# The group tag is only a dummy entry. The correct one is inserted by
# prepare_spec.

    "\@HEADER\@" =>
"Name:		$RPMNAME
Version:	$VERSION
Release:	0
License:	$LICENSE
Group:		Development/Libraries/C and C++
BuildRoot:	%{_tmppath}/%{name}-%{version}-build
$BUILD_ARCHS
Source0:	$RPMNAME-$VERSION.tar.bz2

prefix:		/usr
\%define		swiglibdir $limaldatadir/typemaps 
",

# The group tag is only a dummy entry. The correct one is inserted by
# prepare_spec.

    "\@HEADER-DEVEL\@" =>
"\%package devel
Requires:	$RPMNAME = %version
Requires:	limal-devel, libstdc++-devel, blocxx-devel, pcre-devel

Group:		Development/Libraries/C and C++",

    "\@HEADER-DEVEL-1\@" =>
"\%package devel
Requires:	$RPMNAME = %version
Requires:	limal-devel

Group:		Development/Libraries/C and C++",

    "\@HEADER-PERL\@" =>
"\%package perl
Requires:       limal-perl
\%if 0\%{?fedora_version} >= 7
Requires: perl(:MODULE_COMPAT_\%(eval \"`\%{__perl} -V:version`\"; echo \$version))
%else
Requires:       perl = \%{perl_version}
%endif
Group:		Development/Libraries/Perl",

    "\@HEADER-INSTSYS\@" =>
"\%package instsys
Group:		SuSE internal",

##### PREP #####

    "\@PREP\@" =>
"\%prep
\%setup -n $RPMNAME-$VERSION",

##### BUILD #####

    "\@BUILD\@" =>
"\%build
%{prefix}/bin/limaltool limalautoconf
%{prefix}/bin/limaltool limalautomake
autoreconf --force --install --verbose

export CFLAGS=\"\$RPM_OPT_FLAGS -DNDEBUG\"
export CXXFLAGS=\"\$RPM_OPT_FLAGS -DNDEBUG\"

\%{?suse_update_config:\%{suse_update_config -f}}
# workaround for fedora gettext check on x86_64
sed -i -e 's/return (int) gettext/return (long) gettext/g' ./configure
./configure --libdir=%{_libdir} --prefix=%{prefix} --mandir=%{_mandir} \\
            --sysconfdir=/etc --localstatedir=/var
make %{?jobs:-j%jobs}",

    "\@BUILD-QT\@" =>
"\%build
%{prefix}/bin/limaltool limalautoconf
%{prefix}/bin/limaltool limalautomake
autoreconf --force --install

export CFLAGS=\"\$RPM_OPT_FLAGS -DNDEBUG\"
export CXXFLAGS=\"\$RPM_OPT_FLAGS -DNDEBUG\"

\%{?suse_update_config:\%{suse_update_config -f}}
./configure --prefix=%{prefix} --libdir=%{_libdir} --mandir=%{_mandir} \\
            --sysconfdir=/etc --localstatedir=/var \\
	    --with-qt-libdir=/usr/lib/qt3/%{_lib} \\
	    --with-qt-dir=/usr/lib/qt3
make %{?jobs:-j%jobs}",

    "\@BUILD-PO\@" =>
"\%build
autoreconf --force --install
\%{?suse_update_config:\%{suse_update_config -f}}
./configure --prefix=%{prefix} --libdir=%{_libdir}
\%if \%ul_version > 0
for f in po/*.po ; do
  cp \$f \$f.orig
  awk -f \@ybindir\@/cleanuppo.awk \$f.orig >\$f
done
\%endif
\%if \%sles_version == 8
for f in po/*.po ; do
  cp \$f \$f.orig
  awk -f \@ybindir\@/cleanuppo.awk -v sles=8 \$f.orig >\$f
done
\%endif
make %{?jobs:-j%jobs}",

##### INSTALL #####

    "\@INSTALL\@" =>
"\%install
make install DESTDIR=\"\$RPM_BUILD_ROOT\"
[ -e \"%{prefix}/share/limal/devtools/NO_MAKE_CHECK\" ] || \\
           LIMALDATADIR=\"\$RPM_BUILD_ROOT$limaldatadir\"  \\
# required for perl test cases
export LD_LIBRARY_PATH=\"\$RPM_BUILD_ROOT/usr/lib/limal\"
make check DESTDIR=\"\$RPM_BUILD_ROOT\"
",

    "\@INSTALL-PO\@" => "%find_lang %name ",

##### POST #####

   "\@POST\@" =>
   "\%post
/sbin/ldconfig
",

##### POSTUN #####

   "\@POSTUN\@" =>
   "\%postun
/sbin/ldconfig
",

##### CLEAN #####

    "\@CLEAN\@" =>
"\%clean
rm -rf \"\$RPM_BUILD_ROOT\""
);


# And some tiny macros. Must be separate since the big macros contain them.
%tiny_macros =
(
    "\@limalbindir\@" => "%{_bindir}",
    "\@limallibdir\@" => "%{_libdir}/limal",
    "\@limaldatadir\@" => "%{_prefix}/share/limal",
    "\@limalincludedir\@" => "%{_includedir}/limal",
    "\@pkgconfigdir\@" => $RPMARCH eq "noarch" ?
                          "%{_prefix}/share/pkgconfig" :
                          "%{_libdir}/pkgconfig",
    "\@libdir\@" => "%{_libdir}",
    "\@includedir\@" => "%{_includedir}",
    "\@pobindir\@" => "%{_prefix}/share/po-tools",
    "\@docdir\@" => "%{_prefix}/share/doc/packages/$RPMNAME",
    "\@logdir\@" => "%{_localstatedir}/log/limal",
    "\@vardir\@" => "%{_localstatedir}/lib/limal",
    "\@mandir\@" => "%{_mandir}",
    "\@localedir\@" => "%{_prefix}/share/locale",
    "\@swiglibdir\@" => '%{swiglibdir}',
);

# The main program
my $warn_defattr = 1;
my $warn_doc     = 0;

$unknown_macro = 0;

while (<STDIN>)
{
    if (! /^##.*/)
    {
	foreach $macro_name ( keys %macros )
	{
	    s/$macro_name/$macros{$macro_name}/g;
	}

	foreach $macro_name ( keys %tiny_macros )
	{
	    s/$macro_name/$tiny_macros{$macro_name}/g;
	}

	if (/^\@.+\@/)
	{
	    chomp;
	    print STDERR "create-spec: ERROR: unknown macro $_\n";
	    $unknown_macro = 1;
	}

	$warn_defattr = 0 if (/^\%defattr/);
	$warn_doc     = 1 if (/\/doc\// && ! /^\%doc/);
	print;
    }
}

if ($warn_defattr)
{
    print STDERR "create-spec: WARNING: No \%defattr in the .spec file!\n";
}
if ($warn_doc)
{
    print STDERR "create-spec: WARNING: Do you use \%doc for documentation?\n";
}

if ($unknown_macro)
{
    print STDERR "create-spec: ERROR: unknown macro - update your limal-devtools\n";
    exit 1;
}
