#!/bin/sh

if [ x"$project" = x"emi1" ]; then
	shared_name="shared"
	shared_provide="libs"
	shared_obsoletes="gridsite-libs < $PATCH_VERSION"
else
	shared_name="libs"
	shared_provide="shared"
	shared_obsoletes="gridsite-shared < $PATCH_VERSION"
fi

# test to see if gsoap-devel (or stdsoap2.h and libgsoapssl) is installed
#
cat <<EOF >gsoap-test.c
#include <stdsoap2.h>
#ifdef SOAP_BEGIN  
main() { return; }
#endif
EOF
make gridsite-delegation.cgi
if [ $? = 0 ] ; then have_gsoap=1 ; fi
rm -f a.out

if [ "$have_gsoap" = "1" ]; then
	gsoapdep=',gsoap-devel'
fi
cat <<EOF >gridsite.spec
#
# Autogenerated by make-gridsite-spec 
#
# You should modify  make-gridsite-spec  and rebuild RPM with  make rpm
# rather than editing this spec file
#
Name: gridsite
Version: ${PATCH_VERSION:-1.x.x}
Release: ${RELEASE_VERSION:-1}%{?dist}
Summary: mod_gridsite module for Apache httpd
License: ASL 2.0 and BSD
Source: %{name}-%{version}.src.tar.gz
URL: https://github.com/CESNET/gridsite
BuildRequires: libxml2-devel%{?_isa},httpd-devel%{?_isa},openssl-devel%{?_isa},pkgconfig
BuildRequires: doxygen${gsoapdep}
BuildRequires: canl-c-devel%{?_isa}
%if %{?fedora}%{!?fedora:0} >= 10 || %{?rhel}%{!?rhel:0} >= 6
BuildRequires:  libcurl-devel%{?_isa}
%else
BuildRequires:  curl-devel%{?_isa}
%endif
BuildRequires: libtool
Provides: gridsite-apache = %{version}-%{release}
Obsoletes: gridsite-apache < %{version}-%{release}
Provides: gridsite-services = %{version}-%{release}
Obsoletes: gridsite-services < %{version}-%{release}

%description
GridSite adds GSI, VOMS and GACL support to Apache 2.0 (mod_gridsite),
a library for manipulating these technologies (libgridsite), and CGI
programs for interactive management of HTTP(S) servers (gridsite-admin.cgi)

This package contains Apache module, CGI binaries, and GridSite WS delegation
service.

See https://github.com/CESNET/gridsite/wiki for details.


%package $shared_name
Summary: GridSite shared library and core documentation
Obsoletes: $shared_obsoletes
Provides: gridsite-$shared_provide = %{version}-%{release}

%description $shared_name
GridSite shared library and core documentation

See https://github.com/CESNET/gridsite/wiki for details.


%package devel
Summary: GridSite .a libraries and .h headers
Requires: gridsite-$shared_name%{?_isa} = %{version}

%description devel
GridSite development libraries

See https://github.com/CESNET/gridsite/wiki for details.


%package commands
Summary: HTTP(S) read/write client and other GridSite commands
Requires: curl

%description commands
htcp is a client to fetch files or directory listings from remote
servers using HTTP or HTTPS, or to put or delete files or directories
onto remote servers using HTTPS. htcp is similar to scp(1), but uses
HTTP/HTTPS rather than ssh as its transfer protocol.

See https://github.com/CESNET/gridsite/wiki for details.

%prep

%setup -q


%build
cd src
make prefix=%{_prefix} CFLAGS="%{optflags}" LDFLAGS="%{?__global_ldflags}"

EOF

if [ $have_gsoap ] ; then
cat <<EOF >>gridsite.spec
make prefix=%{_prefix} gridsite-delegation.cgi htproxyput CFLAGS="%{optflags}" LDFLAGS="%{?__global_ldflags}"

EOF
fi

cat <<EOF >>gridsite.spec

%install
cd src

make install prefix=%{_prefix} libdir=%{_lib} DESTDIR=%{buildroot}
rm -f %{buildroot}/%{_libdir}/*.a
EOF

if [ $have_gsoap ] ; then

cat <<EOF >>gridsite.spec

make install-ws prefix=%{_prefix} libdir=%{_lib} DESTDIR=%{buildroot}
EOF
fi

cat <<EOF >>gridsite.spec
# cgi scripts location
mkdir -p %{buildroot}%{_libexecdir}/%{name}/cgi-bin
mv %{buildroot}%{_sbindir}/*.cgi %{buildroot}%{_libexecdir}/%{name}/cgi-bin/
rmdir %{buildroot}%{_sbindir}


%post $shared_name -p /sbin/ldconfig


%postun $shared_name -p /sbin/ldconfig


%files
%{_mandir}/man8/mod_gridsite.8.gz
%{_libdir}/httpd/modules/mod_gridsite.so
%{_libexecdir}/%{name}/cgi-bin/real-gridsite-admin.cgi
EOF

if [ $have_gsoap ] ; then
cat <<EOF >>gridsite.spec
%{_libexecdir}/%{name}/cgi-bin/gridsite-delegation.cgi
%{_mandir}/man8/gridsite-delegation.8.gz
%{_prefix}/share/doc/gridsite-${MINOR_VERSION:-1.x}/delegation-1.1.0.wsdl
%{_prefix}/share/doc/gridsite-${MINOR_VERSION:-1.x}/gridsite-delegation.8
EOF
fi

cat <<EOF >>gridsite.spec

%files $shared_name
%doc %{_prefix}/share/doc/gridsite-${MINOR_VERSION:-1.x}
%{_libdir}/libgridsite.so.6
%{_libdir}/libgridsite.so.6.*
%{_libdir}/libgridsite_globus.so.6
%{_libdir}/libgridsite_globus.so.6.*
%{_libdir}/libgridsite_nossl.so.6
%{_libdir}/libgridsite_nossl.so.6.*

%files devel
%{_includedir}/gridsite.h
%{_includedir}/gridsite-gacl.h
%{_libdir}/libgridsite.so
%{_libdir}/libgridsite_globus.so
%{_libdir}/libgridsite_nossl.so
%{_libdir}/pkgconfig/*.pc

%files commands
%{_bindir}/htcp
%{_bindir}/htls
%{_bindir}/htll
%{_bindir}/htrm
%{_bindir}/htmkdir
%{_bindir}/htmv
%{_bindir}/htping
%{_bindir}/htfind
%{_bindir}/urlencode
%{_bindir}/findproxyfile
%{_mandir}/man1/htcp.1.gz
%{_mandir}/man1/htrm.1.gz
%{_mandir}/man1/htls.1.gz
%{_mandir}/man1/htll.1.gz
%{_mandir}/man1/htmkdir.1.gz
%{_mandir}/man1/htmv.1.gz
%{_mandir}/man1/htping.1.gz
%{_mandir}/man1/htfind.1.gz
%{_mandir}/man1/urlencode.1.gz
%{_mandir}/man1/findproxyfile.1.gz
EOF

if [ $have_gsoap ] ; then

cat <<EOF >>gridsite.spec

%package service-clients
Summary: GridSite WS htproxyput
Requires: curl

%description service-clients
GridSite WS delegation client, htproxyput

See https://github.com/CESNET/gridsite/wiki for details.

%files service-clients
%{_bindir}/htproxyput
%{_bindir}/htproxydestroy
%{_bindir}/htproxytime
%{_bindir}/htproxyunixtime
%{_bindir}/htproxyrenew
%{_bindir}/htproxyinfo
%{_mandir}/man1/htproxyput.1.gz
%{_mandir}/man1/htproxydestroy.1.gz
%{_mandir}/man1/htproxytime.1.gz
%{_mandir}/man1/htproxyunixtime.1.gz
%{_mandir}/man1/htproxyrenew.1.gz
%{_mandir}/man1/htproxyinfo.1.gz
%{_prefix}/share/doc/gridsite-${MINOR_VERSION:-1.x}/htproxyput.1
%{_prefix}/share/doc/gridsite-${MINOR_VERSION:-1.x}/htproxydestroy.1
%{_prefix}/share/doc/gridsite-${MINOR_VERSION:-1.x}/htproxytime.1
%{_prefix}/share/doc/gridsite-${MINOR_VERSION:-1.x}/htproxyunixtime.1
%{_prefix}/share/doc/gridsite-${MINOR_VERSION:-1.x}/htproxyrenew.1
%{_prefix}/share/doc/gridsite-${MINOR_VERSION:-1.x}/htproxyinfo.1

EOF

LC_TIME=C cat >> gridsite.spec <<EOF

%changelog
* `date '+%a %b %d %Y'` autobuild@`hostname -f` - ${PATCH_VERSION:-1.x.x}-${RELEASE_VERSION:-1}
- automatically generated package
EOF

fi

