#! /usr/bin/perl -w
#####
#create-spec [-h] < xxx.spec.in > package/xxx.spec
#
#Create a spec file for YaST2 package
#
#Available macros in the xxx.spec.in:
#  @RPMNAME@		- read from file RPMNAME
#  @VERSION@		- read from file VERSION
#  @MAINTAINER@		- read from file MAINTAINER
#
#  @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-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
#  @CLEAN@		- writes %clean with removing RPM_BUILD_ROOT
#####
# Authors: Jan Holesovsky <kendy@suse.cz>, 2001
#          Arvin Schnell <arvin@suse.de>, 2002

use strict;

# Import the content of some files
my $RPMNAME = `cat RPMNAME`;
my $VERSION = `cat VERSION`;
my $MAINTAINER = `cat MAINTAINER`;
my $LICENSE = -f "YAST_LICENSE" ? "YaST License" : "GPL-2.0";

my $PREFIX = "/usr";

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

# Default values
my $yast2dir = "$PREFIX/share/YaST2";
my $execcompdir = "$PREFIX/lib/YaST2";

my $provides_locale = "";
my $LL = "";
my $ISO639 = "";
if ($RPMNAME =~ /^yast2-trans-(.*)/) {
    $provides_locale = "Provides:\tlocale(yast2:$1)\n";
    $LL = "$1";
    $ISO639 = `echo -n $1 | sed -e 's/_.*//'`;
}

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

# Some fields used to be part of @MACROS@ but since the demise of PDB
# they need to be correctly filled manually. So take care to only provide them
# if they are not present.
my $DEFAULTLICENSE = "License:\t$LICENSE\n";
my $DEFAULTGROUP = "Group:\t\tSystem/YaST\n";
my $DEFAULTSUBGROUP = "Group:\t\tDevelopment/Libraries\n";
# needed for modifying the build environment variables
my $DEFAULTBUILDTAG = '%build';
if (! seek(STDIN, 0, 0))
{
    print STDERR "create-spec: WARNING: Input not seekable, magic disabled.\n";
}
else
{
    my $subpac = 0;
    while (<STDIN>)
    {
	$subpac = 1 if (/^\@HEADER-DEVEL@/ || /^\%package/);

	$DEFAULTLICENSE = ""	if (/^License:/);
	$DEFAULTGROUP = ""	if (/^Group:/ && ! $subpac);
	$DEFAULTSUBGROUP = ""	if (/^Group:/ && $subpac);
	$DEFAULTBUILDTAG = ""	if (/^\%build/);
    }
    seek(STDIN, 0, 0);		# rewind
}

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

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

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

    "\@HEADER-COMMENT\@" =>
"#
# spec file for package $RPMNAME
#
# Copyright (c) 2012 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/
#
",

    "\@HEADER\@" =>
"Name:           $RPMNAME
Version:        $VERSION
Release:        0
${DEFAULTLICENSE}${DEFAULTGROUP}
BuildRoot:      %{_tmppath}/%{name}-%{version}-build
Source0:        $RPMNAME-%{version}.tar.bz2

$provides_locale",

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

# not used in today's packages, maybe useful for maintenance
    "\@HEADER-INSTSYS\@" =>
"\%package instsys
Group:          SuSE internal",

##### PREP #####

    "\@PREP\@" =>
"\%prep
\%setup -n $RPMNAME-%{version}",

##### BUILD #####

    "\@BUILD\@" =>
"$DEFAULTBUILDTAG
%{_prefix}/bin/y2tool y2autoconf
%{_prefix}/bin/y2tool y2automake
autoreconf --force --install

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

./configure --libdir=%{_libdir} --prefix=%{_prefix} --mandir=%{_mandir}
# V=1: verbose build in case we used AM_SILENT_RULES(yes)
# so that RPM_OPT_FLAGS check works
make %{?jobs:-j%jobs} V=1",

    "\@BUILD-QT\@" =>
"$DEFAULTBUILDTAG
%{_prefix}/bin/y2tool y2autoconf
%{_prefix}/bin/y2tool y2automake
autoreconf --force --install

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

./configure --libdir=%{_libdir} --prefix=%{_prefix} --with-qt-dir=/usr/lib/qt3 --with-qt-libdir=/usr/lib/qt3/%{_lib} --mandir=%{_mandir}
# V=1: verbose build in case we used AM_SILENT_RULES(yes)
# so that RPM_OPT_FLAGS check works
make %{?jobs:-j%jobs} V=1",

    "\@BUILD-PO\@" =>
"$DEFAULTBUILDTAG
autoreconf --force --install
./configure --libdir=%{_libdir} --prefix=%{_prefix}
make %{?jobs:-j%jobs}",

##### INSTALL #####

    "\@INSTALL\@" =>
"\%install
make install DESTDIR=\"\$RPM_BUILD_ROOT\"
[ -e \"%{_prefix}/share/YaST2/data/devtools/NO_MAKE_CHECK\" ] || Y2DIR=\"\$RPM_BUILD_ROOT$yast2dir\" make check DESTDIR=\"\$RPM_BUILD_ROOT\"
for f in `find \$RPM_BUILD_ROOT/\@desktopdir\@/ -name \"*.desktop\"` ; do
    d=\${f##*/}
    \%suse_update_desktop_file -d ycc_\${d\%.desktop} \${d\%.desktop}
done
",

    "\@INSTALL-PO\@" =>
"\%install
make install DESTDIR=\"\$RPM_BUILD_ROOT\"",

##### CLEAN #####

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


# And some tiny macros. Must be separate since the big macros contain them.
my %tiny_macros =
(
    # FIXME: kind of double definition of paths
    # we should use configure to create a mapping of paths
    # and use it here. (cannot use configure directly because
    # makefile has ${prefix} and spec file has %{prefix}
    # FIXME: $prefix unfunctional
    #"\@yast2dir@" => "\\\${_prefix}/share/YaST2",
    "\@pkgconfigdir\@" => "%{_libdir}/pkgconfig",
    "\@pkgconfigdatadir\@" => "%{_prefix}/share/pkgconfig",

    "\@ybindir\@" => "%{_prefix}/lib/YaST2/bin",
	"\@ystartupdir\@" => "%{_prefix}/lib/YaST2",
    "\@plugindir\@" => "%{_libdir}/YaST2/plugin",
    "\@includedir\@" => "%{_prefix}/include/YaST2",
    "\@docdir\@" => "%{_prefix}/share/doc/packages/$RPMNAME",
    "\@logdir\@" => "/var/log/YaST2",
    "\@vardir\@" => "/var/lib/YaST2",
    "\@mandir\@" => "%{_mandir}",

    "\@execcompdir\@" => "%{_prefix}/lib/YaST2",
    "\@agentdir\@" => "${execcompdir}/servers_non_y2",

    "\@ydatadir\@" => "${yast2dir}/data",
    "\@imagedir\@" => "${yast2dir}/images",
    "\@themedir\@" => "${yast2dir}/theme",
    "\@localedir\@" => "${yast2dir}/locale",
    "\@clientdir\@" => "${yast2dir}/clients",
    "\@moduledir\@" => "${yast2dir}/modules",
    "\@yncludedir\@" => "${yast2dir}/include",
    "\@schemadir\@" => "${yast2dir}/schema",
    "\@scrconfdir\@" => "${yast2dir}/scrconf",
    "\@desktopdir\@" => "%{_prefix}/share/applications/YaST2",
    '@fillupdir@' => '/var/adm/fillup-templates',

    "\@LL\@" => "$LL",
    "\@ISO639\@" => "$ISO639",

);

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

my $unknown_macro = 0;

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

	foreach my $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 yast2-devtools\n";
    exit 1;
}
