#!/usr/bin/rpm
######################################################################
#
# Preamble 
#
# Macro definitions
%define ver    		@version@
%define sovers		@sovers@
%define rel    		1
%define _prefix         /usr
%define _sysconfdir     /etc
%define _defaultdocdir  %_prefix/share/doc/root-%ver
%define _unpackaged_files_terminate_build  0
%define _icontheme      %_prefix/share/icons/hicolor/48x48

######################################################################
#
# Header 
#
Name:          root-system
Version:       %{ver}
Release:       %{rel}
License:       GNU Lesser General Public License
Packager:      Christian Holm Christensen <cholm@nbi.dk>
Distribution:  Applications/Physics
Vendor:        ROOT Team
URL:           http://root.cern.ch
Source:        ftp://root.cern.ch/root/root_v%{ver}.source.tar.gz
Group:         Applications/Physics
BuildRoot:     /tmp/rootrpm
Summary:       Numerical data analysis framework (OO)
BuildRequires: libX11-devel
BuildRequires: libXext-devel
BuildRequires: pcre-devel
BuildRequires: freetype-devel
BuildRequires: openssl-devel
BuildRequires: gsl-devel
@builddepends@
Requires:      @pkglist@
#
# Over all description - not used, but required
#
%description
This package is a meta package to ensure the installation of all
possible ROOT packages on this system. 

ROOT web-site: http://root.cern.ch

%files 
%defattr(-,root,root)
%{_defaultdocdir}/README

######################################################################
#
# Package building section 
#
# Prepare sources - make package output dir (e.g. ../i686)
%prep
%setup -n root

#---------------------------------------------------------------------
# The actual build
#
# Configure the ROOT source tree. You may want to disable some
# features here, though it is STRONGLY DISCOURAGED
#
#
%build
test "x$QTDIR" = "x" || unset QTDIR
test "x$QTLIB" = "x" || unset QTLIB
test "x$QTINC" = "x" || unset QTINC
./configure @confargs@						\
	    --prefix=%_prefix 					\
	    --libdir=%_prefix/lib/root/%{sovers}		\
	    --mandir=%_prefix/share/man/man1			\
	    --docdir=%_defaultdocdir				\
	    --cintincdir=%_prefix/lib/root/%{sovers}		\
	    --etcdir=%_sysconfdir/root				\
            --with-sys-iconpath=%_datadir/pixmaps 		

# Usage of external afterimage disabled until such a time when ROOT can use 
# the normal libAfterImage
#	    --disable-builtin-afterimage		\

make 
make all-cintdlls
make static 
# cat > html.C <<EOF
# {
#   THtml html;
#   html.SetProductName("ROOT");
#   html.SetHomepage("http://root.cern.ch");
#   html.LoadAllLibs();
#   html.MakeAll();
# }
# EOF
# LD_LIBRARY_PATH=`pwd`/lib:`pwd`/cint/cint/include:`pwd`/cint/cint/stl:$LD_LIBRARY_PATH \
#     ./bin/roota -l -b -q html.C
# rm -f html.C

#---------------------------------------------------------------------
# The installation 
#
# We install in `pwd`/build/package/rpm so that we do not disturb
# the system, and so that non-priviliged user may build the packages. 
#
%install
rm -rf ${RPM_BUILD_ROOT}
make install DESTDIR=${RPM_BUILD_ROOT}
find ${RPM_BUILD_ROOT} -name "CVS" | xargs rm -fr 

# Install desktop entry and icon 
mkdir -p ${RPM_BUILD_ROOT}/%{_prefix}/share/applications
mkdir -p ${RPM_BUILD_ROOT}/%{_prefix}/share/pixmaps
mkdir -p ${RPM_BUILD_ROOT}/%{_icontheme}/apps
mkdir -p ${RPM_BUILD_ROOT}/%{_icontheme}/mimetypes
sed 's,@prefix@,%{_prefix},' \
    < build/package/debian/root-system-bin.desktop.in \
    > ${RPM_BUILD_ROOT}/%{_prefix}/share/applications/root-system-bin.desktop
cp build/package/debian/root-system-bin.xpm \
	${RPM_BUILD_ROOT}/%{_prefix}/share/pixmaps/root-system-bin.xpm
cp build/package/debian/root-system-bin.png \
	${RPM_BUILD_ROOT}/%{_icontheme}/apps/root-system-bin.png
cp build/package/debian/application-x-root.png \
	${RPM_BUILD_ROOT}/%{_icontheme}/mimetypes/application-x-root.png

# Make links from version specific directory to `normal' libary dir
(cd ${RPM_BUILD_ROOT}%{_prefix}/lib/root && \
    for i in %{sovers}/*.so.%{sovers} %{sovers}/libXrd*.so ; do  \
        ln -sf $i . ; \
        ln -sf $i `basename $i .%{sovers}` ; done; \
    ln -sf %{sovers}/cint .)

# Rename proofserv, xrdgsiproxy, and xrdpwdadmin in /usr/bin 
# to end in `.bin' and make wrapper script in /usr/bin 
for i in proofserv xrdgsiproxy xrdpwdadmin xrd xrdcp ; do 		
  if test -f ${RPM_BUILD_ROOT}%{_prefix}/bin/$i && 	\
     test -f build/package/common/$i.in ; then 		
    mv ${RPM_BUILD_ROOT}%{_prefix}/bin/$i 		\
	 ${RPM_BUILD_ROOT}%{_prefix}/bin/$i.bin 
    sed -e 's,@prefix@,%{_prefix},g'			\
	-e 's,@libvers@,%{sovers},g'			\
	< build/package/common/$i.in			\
	> ${RPM_BUILD_ROOT}%{_prefix}/bin/$i
    chmod 755 ${RPM_BUILD_ROOT}%{_prefix}/bin/$i 
  fi
done 


# Prepare SYSV init scripts 
mkdir -p ${RPM_BUILD_ROOT}/%{_sysconfdir}/rc.d/init.d
cp -a etc/daemons/rootd.rc.d \
    ${RPM_BUILD_ROOT}/%{_sysconfdir}/rc.d/init.d/rootd
cp -a etc/daemons/proofd.rc.d \
    ${RPM_BUILD_ROOT}/%{_sysconfdir}/rc.d/init.d/proofd
if test -f etc/daemons/xrootd.rc.d ; then 
    cp -a etc/daemons/xrootd.rc.d \
	${RPM_BUILD_ROOT}/%{_sysconfdir}/rc.d/init.d/xrootd
fi
if test -f etc/daemons/olbd.rc.d ; then 
    cp -a etc/daemons/olbd.rc.d \
	${RPM_BUILD_ROOT}/%{_sysconfdir}/rc.d/init.d/olbd
fi

# Make package lists 
mkdir -p rpm
build/package/lib/makelists.sh rpm ${RPM_BUILD_ROOT} \
    %ver %_prefix %_sysconfdir %_defaultdocdir
echo "%attr(0755, root, root) %{_sysconfdir}/rc.d/init.d/rootd" \
    >> rpm/root-system-rootd.install
echo "%attr(0755, root, root) %{_sysconfdir}/rc.d/init.d/proofd" \
    >> rpm/root-system-proofd.install
touch rpm/ttf-root-installer.install
if test -f rpm/root-system-xrootd.install ; then 
    echo "%attr(0755, root, root) %{_sysconfdir}/rc.d/init.d/xrootd" \
	>> rpm/root-system-xrootd.install
    echo "%attr(0755, root, root) %{_sysconfdir}/rc.d/init.d/olbd" \
	>> rpm/root-system-xrootd.install
fi

# Make the empty proof log file 
mkdir -p ${RPM_BUILD_ROOT}/%{_prefix}/share/root/proof/log
touch ${RPM_BUILD_ROOT}/%{_prefix}/share/root/proof/log/proof.log 

# Now fix up the file lists
for i in rpm/*.install ; do 
    if test ! -f $i ; then continue ; fi
    echo "Fixing $i"
    # -e "s/\.1$/.1${EXT}/g" 
    sed -e "s,%_sysconfdir,%config %_sysconfdir," \
	-e "s,%_defaultdocdir,%doc %_defaultdocdir," \
	< $i > $i.tmp
    mv $i.tmp $i
done

# Remove bad files - this is to quiet RPM about installed but unpackaged files
rm -f ${RPM_BUILD_ROOT}%{_sysconfdir}/root/rootd			\
      ${RPM_BUILD_ROOT}%{_prefix}/lib/root/%{sovers}/libAfterImage.a	\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/BlackChancery.ttf	\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/LICENSE		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/arial.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/arialbd.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/arialbi.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/ariali.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/ariblk.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/comic.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/comicbd.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/cour.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/courbd.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/courbi.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/couri.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/georgia.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/georgiab.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/georgiai.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/georgiaz.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/impact.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/monotype.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/times.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/timesbd.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/timesbi.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/timesi.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/trebuc.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/trebucbd.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/trebucbi.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/trebucit.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/verdana.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/verdanab.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/verdanai.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/verdanaz.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/webdings.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/root/fonts/wingding.ttf		\
      ${RPM_BUILD_ROOT}%{_prefix}/share/man/man1/xproofd.1*		\
      ${RPM_BUILD_ROOT}%{_sysconfdir}/root/proof/*.sample		\
      ${RPM_BUILD_ROOT}%{_sysconfdir}/root/proof/.cvsignore		\
      ${RPM_BUILD_ROOT}%{_prefix}/bin/thisroot*				\
      ${RPM_BUILD_ROOT}%{_prefix}/include/root/*.cw			\
      ${RPM_BUILD_ROOT}%{_prefix}/include/root/*.pri			
rm -rf ${RPM_BUILD_ROOT}%{_sysconfdir}/root/daemons
rm -rf ${RPM_BUILD_ROOT}%{_sysconfdir}/root/proof/utils

# -------------------------------------------------------------------
# Clean up
#
%clean
rm -rf $RPM_BUILD_ROOT

######################################################################
#
# Sub packages
#



