##
##  bacula.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and distribute this software for
##  any purpose with or without fee is hereby granted, provided that
##  the above copyright notice and this permission notice appear in all
##  copies.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package information
Name:         bacula
Summary:      Network Backup Tool
URL:          http://www.bacula.org/
Vendor:       Kern Sibbald
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        System
License:      GPL
Version:      5.2.12
Release:      20120914

#   package options
%option       with_server       yes
%option       with_ssl          yes
%option       with_wrap         no
%option       with_dvd          no
%option       with_mtx          no
%option       with_python       no
%option       with_db_sqlite    no
%option       with_db_pgsql     no
%option       with_db_mysql     no

#   package option sanity check
%if "%{with_db_sqlite}" == "no" && "%{with_db_mysql}" == "no" && "%{with_db_pgsql}" == "no"
%undefine     with_db_sqlite
%define       with_db_sqlite    yes
%endif

#   list of sources
Source0:      http://download.sourceforge.net/bacula/bacula-%{version}.tar.gz
Source1:      rc.bacula
Source2:      bexec.sh
Patch0:       bacula.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, make, gcc, gcc::with_cxx = yes
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  readline, zlib
PreReq:       readline, zlib
%if "%{with_ssl}" == "yes"
BuildPreReq:  openssl >= 0.9.8
PreReq:       openssl >= 0.9.8
%endif
%if "%{with_wrap}" == "yes"
BuildPreReq:  tcpwrappers
PreReq:       tcpwrappers
%endif
%if "%{with_db_sqlite}" == "yes"
BuildPreReq:  sqlite
PreReq:       sqlite
%endif
%if "%{with_db_mysql}" == "yes"
BuildPreReq:  mysql
PreReq:       mysql
%endif
%if "%{with_db_pgsql}" == "yes"
BuildPreReq:  postgresql
PreReq:       postgresql
%endif
%if "%{with_dvd}" == "yes"
BuildPreReq:  dvdrw-tools
PreReq:       dvdrw-tools
%endif
%if "%{with_mtx}" == "yes"
BuildPreReq:  mtx
PreReq:       mtx
%endif
%if "%{with_python}" == "yes" || "%{with_dvd}" == "yes"
BuildPreReq:  python
PreReq:       python
%endif

%description
    Bacula is a set of computer programs that permit you (or the system
    administrator) to manage backup, recovery, and verification of
    computer data across a network of computers of different kinds. In
    technical terms, it is a network client/server based backup program.
    Bacula is relatively easy to use and efficient, while offering many
    advanced storage management features that make it easy to find and
    recover lost or damaged files.

%track
    prog bacula = {
        version   = %{version}
        url       = http://sourceforge.net/projects/bacula/files/
        regex     = bacula-(\d+\.\d*[02468]\.\d+)\.tar\.gz
    }

%prep
    %setup -q
    %patch -p0
    rm -f src/lib/tcpd.h

%build
    #   generate a random director password
    password="`openssl rand -base64 33`"

    #   for the same reason remove version informations from config files
    %{l_shtool} subst \
        -e "s;For Bacula release @VERSION@ .*;;" \
        `find . -name "*.conf.in"`

    #   use localhost as default host
    %{l_shtool} subst \
        -e 's;hostname=.*;hostname=localhost;g' \
        configure

    #   configure
    LIBS=
    case "%{l_platform -t}" in
        *-linux*) LIBS="-L/usr/lib/termcap";;
    esac
    CC="%{l_cc}" \
    CFLAGS="%{l_cflags -O}" \
    CPPFLAGS="%{l_cppflags}" \
    LDFLAGS="%{l_ldflags} $LIBS" \
    ./configure \
        --prefix=%{l_prefix} \
        --with-dir-user=%{l_rusr} \
        --with-dir-group=%{l_rgrp} \
        --with-sd-user=%{l_rusr} \
        --with-sd-group=%{l_rgrp} \
        --with-fd-user=%{l_susr} \
        --with-fd-group=%{l_sgrp} \
        --with-dir-password="$password" \
        --with-fd-password="$password" \
        --with-sd-password="$password" \
        --with-mon-dir-password="$password" \
        --with-mon-fd-password="$password" \
        --with-mon-sd-password="$password" \
        --disable-conio \
        --enable-readline \
        --with-readline=%{l_prefix} \
%if "%{with_server}" != "yes"
        --enable-client-only \
%endif
%if "%{with_ssl}" == "yes"
        --with-openssl=%{l_prefix} \
%endif
%if "%{with_wrap}" == "yes"
        --with-tcp-wrappers=yes \
%endif
%if "%{with_db_sqlite}" == "yes"
        --with-sqlite3=%{l_prefix} \
%endif
%if "%{with_db_mysql}" == "yes"
        --with-mysql=%{l_prefix} \
%endif
%if "%{with_db_pgsql}" == "yes"
        --with-postgresql=%{l_prefix} \
%endif
%if "%{with_python}" == "yes" || "%{with_dvd}" == "yes"
        --with-python=%{l_prefix} \
%endif
        --enable-wx-console=no \
        --sysconfdir=%{l_prefix}/etc/bacula \
        --mandir=%{l_prefix}/man \
        --with-scriptdir=%{l_prefix}/libexec/bacula \
        --with-working-dir=%{l_prefix}/var/bacula \
        --with-pid-dir=%{l_prefix}/var/bacula/run \
        --with-subsys-dir=%{l_prefix}/var/bacula/run/subsys \
        --disable-nls

    #   build
    %{l_make} %{l_mflags -O}

%install

    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/bin \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/bacula \
        $RPM_BUILD_ROOT%{l_prefix}/etc/bacula \
        $RPM_BUILD_ROOT%{l_prefix}/etc/bacula/clients \
        $RPM_BUILD_ROOT%{l_prefix}/etc/bacula/scripts \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/var/bacula \
        $RPM_BUILD_ROOT%{l_prefix}/var/bacula/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/bacula/run/subsys \
        $RPM_BUILD_ROOT%{l_prefix}/man/man1 \
        $RPM_BUILD_ROOT%{l_prefix}/man/man8 \
        $RPM_BUILD_ROOT%{l_prefix}/share/bacula/examples \
        $RPM_BUILD_ROOT%{l_prefix}/share/bacula/examples/default-config

    #   install
    %{l_make} %{l_mflags} install DESTDIR=$RPM_BUILD_ROOT

    #   strip down installation
    #   do not strip binaries, to make it easier to diagnose problems
    #   strip $RPM_BUILD_ROOT%{l_prefix}/sbin/* 2>/dev/null || true
    ( cd $RPM_BUILD_ROOT%{l_prefix}/libexec/bacula &&
      for unwanted in bconsole startmysql stopmysql; do
          rm -f $unwanted
      done
    ) || exit $?

    #   install additional files
    %{l_shtool} install -c -m 754 %{l_value -s -a} \
        %{SOURCE bexec.sh} $RPM_BUILD_ROOT%{l_prefix}/libexec/bacula/bexec

    #   wrap binaries to avoid to specify "-c" for each run
    ( cd $RPM_BUILD_ROOT%{l_prefix}/sbin
      for bin in bacula-dir bacula-fd bacula-sd \
                 bconsole bcopy bextract bls bscan dbcheck \
                 tray-monitor wx-console; do
          if [ -x $bin ]; then
              mv $bin $RPM_BUILD_ROOT%{l_prefix}/libexec/bacula
              ln $RPM_BUILD_ROOT%{l_prefix}/libexec/bacula/bexec ./$bin
          fi
      done
    ) || exit $?

    #   install run-command script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e 's,@with_server@,%{with_server},g' \
        %{SOURCE rc.bacula} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%attr(700,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/bacula' \
        '%attr(700,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/bacula/run' \
        '%attr(700,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/bacula/run/subsys' \
        '%attr(-,%{l_musr},%{l_rgrp}) %{l_prefix}/libexec/bacula/dvd-handler' \
        '%attr(-,%{l_musr},%{l_rgrp}) %{l_prefix}/libexec/bacula/mtx-changer' \
%if "%{with_server}" == "yes"
        '%attr(-,%{l_musr},%{l_rgrp}) %{l_prefix}/libexec/bacula/make_catalog_backup' \
        '%attr(-,%{l_musr},%{l_rgrp}) %{l_prefix}/libexec/bacula/delete_catalog_backup'\
        '%config(noreplace) %{l_prefix}/etc/bacula/bacula-dir.conf' \
        '%config(noreplace) %{l_prefix}/etc/bacula/bacula-sd.conf' \
%endif
        '%config(noreplace) %{l_prefix}/etc/bacula/bacula-fd.conf' \
%if "%{with_server}" == "yes"
        '%attr(640,%{l_musr},%{l_rgrp}) %{l_prefix}/etc/bacula/bacula-dir.conf' \
        '%attr(640,%{l_musr},%{l_rgrp}) %{l_prefix}/etc/bacula/bacula-sd.conf' \
%endif
        '%attr(640,%{l_musr},%{l_mgrp}) %{l_prefix}/etc/bacula/bacula-fd.conf'

%files -f files

%clean

%post
    #   create initial database
    if [ ! -f $RPM_INSTALL_PREFIX/var/bacula/bacula.db ]; then
        $RPM_INSTALL_PREFIX/libexec/bacula/make_bacula_tables
        chmod 600 $RPM_INSTALL_PREFIX/var/bacula/bacula.db
        chown %{l_rusr}:%{l_rgrp} $RPM_INSTALL_PREFIX/var/bacula/bacula.db
    fi

    #   after upgrade, restart service
    [ $1 -eq 2 ] || exit 0
    eval `%{l_rc} bacula status 2>/dev/null`
    [ ".$bacula_active" = .yes ] && %{l_rc} bacula restart
    exit 0

%preun
    #   before erase, stop service and remove working files
    [ $1 -eq 0 ] || exit 0
    %{l_rc} bacula stop 2>/dev/null
    rm -rf $RPM_INSTALL_PREFIX/var/bacula/*
    exit 0

