#
# Bacula RPM spec file
#
#
# Copyright (C) 2000-2015 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#

# Platform Build Configuration

# basic defines for every build
%define _release           @RELEASE@
%define _version           @VERSION@
%define depkgs_qt_version  @DEPKGS_QT_VERSION@
%define product            bacula

# Don't strip binaries
%define __os_install_post %{nil}
%define __debug_install_post %{nil}
%define debug_package %{nil}

# this is the Qt version in depkgs_qt
%define qt4ver             @BQT_VERSION@

%define _packager Kern Sibbald <kern@sibbald.com>

%define manpage_ext gz

# Force single file build
%define single_dir 1
%{?single_dir_install:%define single_dir 1}

# Installation Directory locations
%if %{single_dir}
%define _prefix        /opt/bacula
%define _sbindir       /opt/bacula/bin
%define _bindir        /opt/bacula/bin
%define _subsysdir     /opt/bacula/working
%define sqlite_bindir  /opt/bacula/sqlite
%define _mandir        /usr/share/man
%define docs_dir       /opt/bacula/docs
%define archive_dir    /opt/bacula/archive
%define sysconf_dir    /opt/bacula/etc
%define script_dir     /opt/bacula/scripts
%define working_dir    /opt/bacula/working
%define pid_dir        /opt/bacula/working
%define plugin_dir     /opt/bacula/plugins
%define lib_dir        /opt/bacula/lib
%else
%define _prefix        /usr
%define _sbindir       %_prefix/sbin
%define _bindir        %_prefix/bin
%define _subsysdir     /var/lock/subsys
%define sqlite_bindir  %_libdir/bacula/sqlite
%define _mandir        %_prefix/share/man
%define sysconf_dir    /etc/bacula
%define script_dir     %_libdir/bacula
%define working_dir    /var/lib/bacula
%define pid_dir        /var/run
%define plugin_dir     %_libdir/bacula/plugins
%define lib_dir        %_libdir/bacula/lib
%endif

# Daemon user:group Don't change them unless you know what you are doing
%define director_daemon_user    bacula
%define daemon_group            bacula

#--------------------------------------------------------------------------
# it should not be necessary to change anything below here for a release
# except for patch macros in the setup section
#--------------------------------------------------------------------------

%{?contrib_packager:%define _packager %{contrib_packager}}

%{expand: %%define gccver %(rpm -q --queryformat %%{version} gcc)}
%{expand: %%define gccrel %(rpm -q --queryformat %%{release} gcc)}

%define staticqt 1
%{?nobuild_staticqt:%define staticqt 0}

# determine what platform we are building on
%define fedora 0
%define suse 0
%define mdk 0

%if %{_vendor} == redhat
        %define fedora 1
        %define _dist %(cat /etc/redhat-release)
%endif
%if %{_vendor} == suse
        %define suse 1
        %define _dist %(grep -i SuSE /etc/SuSE-release)
%endif
%if %{_vendor} == Mandriva
        %define mdk 1
        %define _dist %(grep Mand /etc/mandrake-release)
%endif
%if ! %{fedora} && ! %{suse} && ! %{mdk}
%{error: Unknown platform. Please examine the spec file.}
exit 1
%endif

%define sqlite 0
%{?build_sqlite:%define sqlite 1}

%define base_package_name bacula

Summary: Bacula - The Network Backup Solution
Name: %{base_package_name}-bat
Version: %{_version}
Release: %{_release}
Group: System Environment/Daemons
License: AGPLv3
BuildRoot: %{_tmppath}/%{name}-root
URL: http://www.bacula.org/
Vendor: The Bacula Team
Packager: %{_packager}
Prefix: %{_prefix}
Distribution: %{_dist}

Source0: http://www.prdownloads.sourceforge.net/bacula/%{product}-%{version}.tar.gz
Source1: http://www.prdownloads.sourceforge.net/bacula/depkgs-qt-%{depkgs_qt_version}.tar.gz

BuildRequires: gcc, gcc-c++, make, autoconf
BuildRequires: libstdc++-devel, zlib-devel
BuildRequires: openssl-devel, fontconfig-devel, libpng-devel, libstdc++-devel, zlib-devel

Requires: openssl
Requires: fontconfig
Requires: libpng
%if 0%{?suse_version} > 1210
Requires: libstdc++.6
%else
Requires: libstdc++
%endif
Requires: zlib
Requires: %{base_package_name}-libs

%if %{suse}
Requires: freetype2
BuildRequires: freetype2-devel
%else
Requires: usermode
Requires: freetype
BuildRequires: freetype-devel
%endif

# Source directory locations
%define depkgs_qt ../depkgs-qt

# define the basic package description
%define blurb Bacula - The Network Backup Solution.
%define blurb2 Bacula is a set of computer programs that permit you (or the system
%define blurb3 administrator) to manage backup, recovery, and verification of computer
%define blurb4 data across a network of computers of different kinds. In technical terms,
%define blurb5 it is a network client/server based backup program. Bacula is relatively
%define blurb6 easy to use and efficient, while offering many advanced storage management
%define blurb7 features that make it easy to find and recover lost or damaged files.

%define group_file /etc/group
%define groupadd /usr/sbin/groupadd

Summary: Bacula - The Network Backup Solution
Group: System Environment/Daemons

%description
%{blurb}

%{blurb2}
%{blurb3}
%{blurb4}
%{blurb5}
%{blurb6}
%{blurb7}

This is the Bacula Administration Tool (bat) graphical user interface package.
It is an add-on to the client or server packages.

# Don't strip symbols
%define debug_package %{nil} 

# Must explicitly enable debug pkg on SuSE
# but not in opensuse_bs
#%if %{suse} && ! 0%{?opensuse_bs}
#%debug_package
#%endif

%prep
%setup -T -n %{product}-%{_version} -b 0
%setup -T -D -n %{product}-%{_version} -b 1

%build


cwd=${PWD}
%if ! %{staticqt}
export QTDIR=$(pkg-config --variable=prefix QtCore)
export QTINC=$(pkg-config --variable=includedir QtCore)
export QTLIB=$(pkg-config --variable=libdir QtCore)
export PATH=${QTDIR}/bin/:${PATH}
%else
# You can use a cache for depkgs-qt
# tar xfz depkgs-qt*gz -C ~/
# cd ~/depkgs-qt
# echo yes | make qt4
# touch %{depkgs_qt_version}       # depkgs version
if [ -f $HOME/depkgs-qt/%{depkgs_qt_version} ]; then
   rm -rf %{depkgs_qt}
   ln -s $HOME/depkgs-qt %{depkgs_qt}
   cd %{depkgs_qt}
else
   cd %{depkgs_qt}
   make qt4 <<EOF
yes
EOF
fi

source ./qt4-paths
rm -f ${QTLIB}/QtCore.so* ${QTLIB}/QtGUI.so*
cd ${cwd}

%endif

# hostname is the build hostname, so use XXX_HOSTNAME_XXX for Address parameter
sed -i s/hostname/basename/ src/console/bconsole.conf.in src/dird/bacula-dir.conf.in src/qt-console/bat.conf.in

# Main Bacula configuration with bat
%configure \
        --prefix=%{_prefix} \
        --sbindir=%{_sbindir} \
        --sysconfdir=%{sysconf_dir} \
        --mandir=%{_mandir} \
        --docdir=%{docs_dir} \
        --with-archivedir=%{archive_dir} \
        --with-scriptdir=%{script_dir} \
        --with-working-dir=%{working_dir} \
        --with-plugindir=%{script_dir} \
        --with-pid-dir=%{pid_dir} \
        --with-subsys-dir=%{_subsysdir} \
        --enable-smartalloc \
        --enable-bat \
        --enable-client-only \
        --with-dir-user=%{director_daemon_user} \
        --with-dir-group=%{daemon_group} \
        --with-sd-user=%{storage_daemon_user} \
        --with-sd-group=%{storage_daemon_group} \
        --with-fd-user=%{file_daemon_user} \
        --with-fd-group=%{daemon_group} \
        --with-basename="XXX_HOSTNAME_XXX" \
        --with-hostname="XXX_HOSTNAME_XXX" \
        --with-dir-password="XXX_REPLACE_WITH_DIRECTOR_PASSWORD_XXX" \
        --with-fd-password="XXX_REPLACE_WITH_CLIENT_PASSWORD_XXX" \
        --with-sd-password="XXX_REPLACE_WITH_STORAGE_PASSWORD_XXX" \
        --with-mon-dir-password="XXX_REPLACE_WITH_DIRECTOR_MONITOR_PASSWORD_XXX" \
        --with-mon-fd-password="XXX_REPLACE_WITH_CLIENT_MONITOR_PASSWORD_XXX" \
        --with-mon-sd-password="XXX_REPLACE_WITH_STORAGE_MONITOR_PASSWORD_XXX" \
        --with-openssl

make

%install
mkdir -p $RPM_BUILD_ROOT/usr/share/applications
mkdir -p $RPM_BUILD_ROOT/usr/share/pixmaps
%if ! %{suse}
mkdir -p $RPM_BUILD_ROOT/etc/pam.d
mkdir -p $RPM_BUILD_ROOT/etc/security/console.apps
mkdir -p $RPM_BUILD_ROOT%{_sbindir}
mkdir -p $RPM_BUILD_ROOT/usr/bin
%endif

cd src/qt-console
make DESTDIR=$RPM_BUILD_ROOT install
cd ../..

rm -rf $RPM_BUILD_ROOT%{_prefix}/share/doc/bacula

%if %{suse}
cp -p src/qt-console/images/bat_icon.png $RPM_BUILD_ROOT/usr/share/pixmaps/bat_icon.png
cp -p scripts/bat.desktop.xsu $RPM_BUILD_ROOT/usr/share/applications/bat.desktop
touch $RPM_BUILD_ROOT%{sysconf_dir}/bat.kdesu
%else
cp -p src/qt-console/images/bat_icon.png $RPM_BUILD_ROOT/usr/share/pixmaps/bat_icon.png
cp -p scripts/bat.desktop.consolehelper $RPM_BUILD_ROOT/usr/share/applications/bat.desktop
cp -p scripts/bat.console_apps $RPM_BUILD_ROOT/etc/security/console.apps/bat
cp -p scripts/bat.pamd $RPM_BUILD_ROOT/etc/pam.d/bat
ln -sf consolehelper $RPM_BUILD_ROOT/usr/bin/bat
%endif

%files
%defattr(-,root,root)
%attr(-, root, %{daemon_group}) %{_sbindir}/bat
%attr(770, root, %{daemon_group}) %dir %{sysconf_dir}
%attr(-, root, %{daemon_group}) %config(noreplace) %{sysconf_dir}/bat.conf
%attr(-, root, %{daemon_group}) %{docs_dir}/*.html
%attr(-, root, %{daemon_group}) %{docs_dir}/*.png
/usr/share/pixmaps/bat_icon.png
/usr/share/applications/bat.desktop

# if user is a member of daemon_group then kdesu will run bat as user
%if %{suse}
%attr(0660, root, %{daemon_group}) %{sysconf_dir}/bat.kdesu
%endif

%if ! %{suse}
# add the console helper files
%config(noreplace,missingok) /etc/pam.d/bat
%config(noreplace,missingok) /etc/security/console.apps/bat
/usr/bin/bat
%endif



%pre
# create the daemon group
HAVE_BACULA=`grep %{daemon_group} %{group_file} 2>/dev/null`
if [ -z "$HAVE_BACULA" ]; then
    %{groupadd} -r %{daemon_group} > /dev/null 2>&1
    echo "The group %{daemon_group} has been added to %{group_file}."
    echo "See the manual chapter \"Running Bacula\" for details."
fi


%post
if [ -d %{sysconf_dir} ]; then
   cd %{sysconf_dir}
   if [ ! -f .rpm.sed ]; then
       (umask 0177
        echo "# This file is used to ensure that all passwords will" > .rpm.sed
        echo "# match between configuration files"                  >> .rpm.sed
       )
       for string in XXX_REPLACE_WITH_DIRECTOR_PASSWORD_XXX XXX_REPLACE_WITH_CLIENT_PASSWORD_XXX XXX_REPLACE_WITH_STORAGE_PASSWORD_XXX XXX_REPLACE_WITH_DIRECTOR_MONITOR_PASSWORD_XXX XXX_REPLACE_WITH_CLIENT_MONITOR_PASSWORD_XXX XXX_REPLACE_WITH_STORAGE_MONITOR_PASSWORD_XXX; do
           pass=`openssl rand -base64 33`
           echo "s@${string}@${pass}@g" >> .rpm.sed
       done
   fi
   host=`hostname -s`
   for file in *.conf; do
      sed -f .rpm.sed $file > $file.new
      sed "s@XXX_HOSTNAME_XXX@${host}@g" $file.new > $file
      rm -f $file.new
   done
fi
/sbin/ldconfig

%clean
[ "$RPM_BUILD_ROOT" != "/" ] && rm -rf "$RPM_BUILD_ROOT"
rm -rf $RPM_BUILD_DIR/depkgs-qt

%changelog
* Sat Aug 1 2009 Kern Sibbald <kern@sibbald.com>
- Split bat into separate bacula-bat.spec
