#!/bin/sh

# test to see if fuse-devel (or fuse.h and libfuse) is installed
#
cat <<EOF >fuse-test.c
#include <fuse.h>
int main() { struct fuse_context ctx;
return fuse_main(0, (char **) 0, (struct fuse_operations *) 0); }
EOF
make fuse-test
if [ $? = 0 ] ; then have_fuse=1 ; fi

if [ x"$project" = x"glite" ]; then
	globusdep=',vdt_globus_sdk'
else
	globusdep=',globus-gssapi-gsi-devel'
fi

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_fuse" = "1" ]; then
	fusedep=',fuse-devel'
fi
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}
# This next piece of .spec/sed magic puts the build OS version in the release
Release: %(if [ "$RELEASE_VERSION" ] ; then echo $RELEASE_VERSION ; else echo '1' ; fi)%{?dist}
Summary: mod_gridsite module for Apache httpd
License: Modified BSD
Group: System Environment/Daemons
Source: %{name}-%{version}.src.tar.gz
BuildRoot: %{_tmppath}/%{name}-%{version}-%{release}-root
URL: http://www.gridsite.org/
Vendor: GridPP
Buildrequires: libxml2-devel,curl-devel,httpd-devel,openssl-devel,pkgconfig
Buildrequires: doxygen${gsoapdep}${fusedep}${globusdep}
Packager: Andrew McNab <Andrew.McNab@manchester.ac.uk>
Provides: gridsite-apache = %{version}-%{release}
Obsoletes: gridsite-apache <= 1.7.21
Provides: gridsite-services = %{version}-%{release}
Obsoletes: gridsite-services <= 1.7.21

%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 http://www.gridsite.org/ for details.

%package $shared_name
Group: Development/Libraries
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 http://www.gridsite.org/ for details.

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

%description devel
GridSite development libraries

See http://www.gridsite.org/ for details.

%package commands
Group: Applications/Internet
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 http://www.gridsite.org/ for details.

%package gsexec
Group: Applications/Internet
Summary: gsexec binary for the Apache HTTP server

%description gsexec
This package includes the /usr/sbin/gsexec binary which can be installed
to allow the Apache HTTP server to run CGI programs (and any programs
executed by SSI pages) as a user other than the 'apache' user. gsexec
is a drop-in replacement for suexec, with extended functionality for use
with GridSite and Grid Security credentials.

See http://www.gridsite.org/ for details.

%prep

%setup -q

%build
cd src
make prefix=%{_prefix}

EOF

if [ $have_fuse ] ; then
cat <<EOF >>gridsite.spec
make prefix=%{_prefix} slashgrid

EOF
fi

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

EOF
fi

cat <<EOF >>gridsite.spec

%install
rm -rf \$RPM_BUILD_ROOT

cd src

make install prefix=\$RPM_BUILD_ROOT/%{_prefix} libdir=%{_lib}
EOF

if [ $have_fuse ] ; then

cat <<EOF >>gridsite.spec

mkdir -p \$RPM_BUILD_ROOT/etc/rc.d/init.d
make install-slashgrid prefix=\$RPM_BUILD_ROOT/%{_prefix}
EOF
fi

if [ $have_gsoap ] ; then

cat <<EOF >>gridsite.spec

make install-ws prefix=\$RPM_BUILD_ROOT/%{_prefix}
EOF
fi

cat <<EOF >>gridsite.spec
%clean
rm -rf \$RPM_BUILD_ROOT

%post $shared_name
if [ "\$UID" = "0" ] ; then
 /sbin/ldconfig
fi

%postun $shared_name
if [ "\$UID" = "0" ] ; then
 /sbin/ldconfig
fi

%files
%attr(-, root, root) %{_prefix}/share/man/man8/mod_gridsite.8.gz
%attr(-, root, root) %{_prefix}/%{_lib}/httpd/modules/mod_gridsite.so
%attr(-, root, root) %{_prefix}/sbin/real-gridsite-admin.cgi
%attr(-, root, root) %{_prefix}/sbin/gridsite-copy.cgi
%attr(-, root, root) %{_prefix}/sbin/gridsite-storage.cgi
EOF

if [ $have_gsoap ] ; then
cat <<EOF >>gridsite.spec
%attr(-, root, root) %{_prefix}/sbin/gridsite-delegation.cgi
%attr(-, root, root) %{_prefix}/share/man/man8/gridsite-delegation.8.gz
%attr(-, root, root) %{_prefix}/share/doc/gridsite-${MINOR_VERSION:-1.x}/delegation-1.1.0.wsdl
%attr(-, root, root) %{_prefix}/share/doc/gridsite-${MINOR_VERSION:-1.x}/gridsite-delegation.8
EOF
fi

cat <<EOF >>gridsite.spec

%files $shared_name
%attr(-, root, root) %{_prefix}/%{_lib}/libgridsite.so.%{version}
%attr(-, root, root) %{_prefix}/%{_lib}/libgridsite.so.${MINOR_VERSION:-1.x}
%attr(-, root, root) %{_prefix}/%{_lib}/libgridsite.so.${MAJOR_VERSION:-1}
%attr(-, root, root) %{_prefix}/%{_lib}/libgridsite_globus.so.%{version}
%attr(-, root, root) %{_prefix}/%{_lib}/libgridsite_globus.so.${MINOR_VERSION:-1.x}
%attr(-, root, root) %{_prefix}/%{_lib}/libgridsite_globus.so.${MAJOR_VERSION:-1}
%attr(-, root, root) %{_prefix}/%{_lib}/libgridsite_nossl.so.%{version}
%attr(-, root, root) %{_prefix}/%{_lib}/libgridsite_nossl.so.${MINOR_VERSION:-1.x}
%attr(-, root, root) %{_prefix}/%{_lib}/libgridsite_nossl.so.${MAJOR_VERSION:-1}
%attr(-, root, root) %{_prefix}/share/doc/gridsite-${MINOR_VERSION:-1.x}

%files devel
%attr(-, root, root) %{_prefix}/include/gridsite.h
%attr(-, root, root) %{_prefix}/include/gridsite-gacl.h
%attr(-, root, root) %{_prefix}/%{_lib}/libgridsite.a
%attr(-, root, root) %{_prefix}/%{_lib}/libgridsite_globus.a
%attr(-, root, root) %{_prefix}/%{_lib}/libgridsite_nossl.a
%attr(-, root, root) %{_prefix}/%{_lib}/libgridsite.so
%attr(-, root, root) %{_prefix}/%{_lib}/libgridsite_globus.so
%attr(-, root, root) %{_prefix}/%{_lib}/libgridsite_nossl.so
%attr(-, root, root) %{_prefix}/%{_lib}/pkgconfig/*.pc

%files commands
%attr(-, root, root) %{_prefix}/bin/htcp
%attr(-, root, root) %{_prefix}/bin/htls
%attr(-, root, root) %{_prefix}/bin/htll
%attr(-, root, root) %{_prefix}/bin/htrm
%attr(-, root, root) %{_prefix}/bin/htmkdir
%attr(-, root, root) %{_prefix}/bin/htmv
%attr(-, root, root) %{_prefix}/bin/htping
%attr(-, root, root) %{_prefix}/bin/htfind
%attr(-, root, root) %{_prefix}/bin/urlencode
%attr(-, root, root) %{_prefix}/bin/findproxyfile
%attr(-, root, root) %{_prefix}/share/man/man1/htcp.1.gz
%attr(-, root, root) %{_prefix}/share/man/man1/htrm.1.gz
%attr(-, root, root) %{_prefix}/share/man/man1/htls.1.gz
%attr(-, root, root) %{_prefix}/share/man/man1/htll.1.gz
%attr(-, root, root) %{_prefix}/share/man/man1/htmkdir.1.gz
%attr(-, root, root) %{_prefix}/share/man/man1/htmv.1.gz
%attr(-, root, root) %{_prefix}/share/man/man1/htping.1.gz
%attr(-, root, root) %{_prefix}/share/man/man1/htfind.1.gz
%attr(-, root, root) %{_prefix}/share/man/man1/urlencode.1.gz
%attr(-, root, root) %{_prefix}/share/man/man1/findproxyfile.1.gz

%files gsexec
%attr(4510, root, apache) %{_prefix}/sbin/gsexec
%attr(-, root, root) %{_prefix}/share/man/man8/gsexec.8.gz
EOF

if [ $have_fuse ] ; then

cat <<EOF >>gridsite.spec
%package slashgrid
Group: Applications/Internet
Summary: slashgrid daemon
Requires: curl >= 7.12.1,fuse

%description slashgrid
SlashGrid daemon

%post slashgrid
mkdir -p /grid

%preun slashgrid
/sbin/service slashgrid stop ; :

%files slashgrid
%attr(0744, root, root) %{_prefix}/sbin/slashgrid
%attr(0744, root, root) /etc/rc.d/init.d/slashgrid
%attr(0700, root, root) /var/spool/slashgrid
%attr(-, root, root) %{_prefix}/share/man/man8/slashgrid.8.gz

EOF

fi

if [ $have_gsoap ] ; then

cat <<EOF >>gridsite.spec
%package service-clients
Group: Applications/Internet
Summary: GridSite WS htproxyput
Requires: curl

%description service-clients
GridSite WS delegation client, htproxyput

See http://www.gridsite.org/ for details.

%files service-clients
%attr(-, root, root) %{_prefix}/bin/htproxyput
%attr(-, root, root) %{_prefix}/bin/htproxydestroy
%attr(-, root, root) %{_prefix}/bin/htproxytime
%attr(-, root, root) %{_prefix}/bin/htproxyunixtime
%attr(-, root, root) %{_prefix}/bin/htproxyrenew
%attr(-, root, root) %{_prefix}/bin/htproxyinfo
%attr(-, root, root) %{_prefix}/share/man/man1/htproxyput.1.gz
%attr(-, root, root) %{_prefix}/share/man/man1/htproxydestroy.1.gz
%attr(-, root, root) %{_prefix}/share/man/man1/htproxytime.1.gz
%attr(-, root, root) %{_prefix}/share/man/man1/htproxyunixtime.1.gz
%attr(-, root, root) %{_prefix}/share/man/man1/htproxyrenew.1.gz
%attr(-, root, root) %{_prefix}/share/man/man1/htproxyinfo.1.gz
%attr(-, root, root) %{_prefix}/share/doc/gridsite-${MINOR_VERSION:-1.x}/htproxyput.1
%attr(-, root, root) %{_prefix}/share/doc/gridsite-${MINOR_VERSION:-1.x}/htproxydestroy.1
%attr(-, root, root) %{_prefix}/share/doc/gridsite-${MINOR_VERSION:-1.x}/htproxytime.1
%attr(-, root, root) %{_prefix}/share/doc/gridsite-${MINOR_VERSION:-1.x}/htproxyunixtime.1
%attr(-, root, root) %{_prefix}/share/doc/gridsite-${MINOR_VERSION:-1.x}/htproxyrenew.1
%attr(-, root, root) %{_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`
- automatically generated package
EOF

fi

