##
##  ratbox.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 version
%define       V_server   3.0.10
%define       V_services 1.2.4
%define       V_respond  1.0

#   package information
Name:         ratbox
Summary:      Internet Relay Chat (IRC) Server
URL:          http://www.ratbox.org/
Vendor:       A. Sethman, L. Hardy, E. Brocklesby et al.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        InstantMessaging
License:      GPL
Version:      %{V_server}
Release:      20160120

#   list of sources
Source0:      http://www.ratbox.org/download/ircd-ratbox-%{V_server}.tar.bz2
Source1:      http://services.ratbox.org/download/ratbox-services-%{V_services}.tgz
Source2:      http://respond.ircd-ratbox.org/download/ratbox-respond-%{V_respond}.tgz
Source3:      rc.ratbox
Source4:      ratbox-server.conf
Source5:      ratbox-services.conf
Source6:      ratbox-resv.conf
Patch0:       ratbox.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, bison, flex, flex::with_old = yes
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  openssl, zlib, sqlite
PreReq:       openssl, zlib, sqlite

%description
    IRCD-Ratbox is an advanced, stable, fast Internet Relay Chat (IRC)
    server. It is an evolution where ircd-hybrid left off. It supports
    the TS5 and TS6 protocols, and is used on EFnet and other IRC
    networks. This package also includes the Ratbox Services add-on.

%track
    prog ratbox = {
        version   = %{V_server}
        url       = http://www.ratbox.org/download.php
        regex     = ircd-ratbox-(__VER__)\.tar\.bz2
    }
    prog ratbox:services = {
        version   = %{V_services}
        url       = http://services.ratbox.org/download.php
        regex     = ratbox-services-(__VER__)\.tgz
    }
    prog ratbox:respond = {
        version   = %{V_respond}
        url       = http://respond.ratbox.org/download.php
        regex     = ratbox-respond-(__VER__)\.tgz
    }

%prep
    %setup -q -c
    %setup -q -T -D -a 1
    %setup -q -T -D -a 2
    %patch -p0

%build
    #   build IRC server
    ( cd ircd-ratbox-%{V_server}
      %{l_shtool} subst \
          -e 's;"/ircd\.;"/server.;g' \
          -e 's;"/ircd";"/ratbox-ircd";g' \
          -e 's;ETCPATH "/server\.pid";LOGPATH "/server.pid";' \
          include/config.h include/config.h.dist
      CC="%{l_cc}" \
      CFLAGS="%{l_cflags -O}" \
      CPPFLAGS="%{l_cppflags}" \
      LDFLAGS="-L`pwd`/adns %{l_ldflags}" \
      ./configure \
          --prefix=%{l_prefix} \
          --with-confdir=%{l_prefix}/etc/ratbox \
          --with-logdir=%{l_prefix}/var/ratbox \
          --with-helpdir=%{l_prefix}/share/ratbox/help \
          --with-moduledir=%{l_prefix}/libexec/ratbox \
          --enable-openssl=%{l_prefix} \
          --enable-zlib \
          --with-zlib-path=%{l_prefix} \
          --enable-small-net \
          --enable-shared-modules \
          --enable-services
      %{l_make} %{l_mflags -O}
      ( cd contrib
        %{l_make} %{l_mflags -O}
      ) || exit $?
    ) || exit $?

    #   build IRC services
    ( cd ratbox-services-%{V_services}
      %{l_shtool} subst \
          -e 's;ratbox-services\.conf;services.conf;' \
          -e 's;ratbox-services\.pid;services.pid;' \
          -e 's;ratbox-services\.log;services.log;' \
          -e 's;ratbox-services\.db;services.db;' \
          include/config.h
      CC="%{l_cc}" \
      CFLAGS="%{l_cflags -O `pkg-config sqlite3 --cflags-only-other`}" \
      CPPFLAGS="%{l_cppflags} `pkg-config sqlite3 --cflags-only-I`" \
      LDFLAGS="%{l_ldflags} `pkg-config sqlite3 --libs-only-other`" \
      LIBS="%{l_ldflags} `pkg-config sqlite3 --libs-only-l`" \
      LEX="flex-old" \
      ./configure \
          --prefix=%{l_prefix} \
          --sbindir=%{l_prefix}/bin \
          --sysconfdir=%{l_prefix}/etc/ratbox \
          --with-logdir=%{l_prefix}/var/ratbox \
          --with-rundir=%{l_prefix}/var/ratbox \
          --with-helpdir=%{l_prefix}/share/ratbox/services/help \
          --with-langdir=%{l_prefix}/share/ratbox/services/lang \
          --enable-sqlite3=%{l_prefix} \
          --enable-pcre=%{l_prefix} \
          --without-sqlitebuild \
          --without-pcrebuild
      %{l_make} %{l_mflags}
    ) || exit $?

    #   build IRC challenge/response client
    ( cd ratbox-respond-%{V_respond}/ratbox-respond
      libs=""
      case "%{l_platform -t}" in
          *-sunos* ) libs="-lnsl -lsocket" ;;
      esac
      CC="%{l_cc}" \
      CFLAGS="%{l_cflags -O}" \
      CPPFLAGS="%{l_cppflags}" \
      LDFLAGS="%{l_ldflags}" \
      LIBS="$libs" \
      ./configure \
          --prefix=%{l_prefix} \
          --enable-openssl=%{l_prefix}
      %{l_make} %{l_mflags}
    ) || exit $?

%install
    #   install IRC server
    ( cd ircd-ratbox-%{V_server}
      %{l_shtool} subst \
          -e 's;ln -s ;ln $(DESTDIR);g' \
          help/Makefile
      %{l_shtool} mkdir -f -p -m 755 \
          $RPM_BUILD_ROOT%{l_prefix}/bin \
          $RPM_BUILD_ROOT%{l_prefix}/etc/ratbox \
          $RPM_BUILD_ROOT%{l_prefix}/libexec/ratbox \
          $RPM_BUILD_ROOT%{l_prefix}/share/ratbox \
          $RPM_BUILD_ROOT%{l_prefix}/var/ratbox
      chmod a+x install-sh
      %{l_make} %{l_mflags} install DESTDIR=$RPM_BUILD_ROOT
      ( cd contrib
        %{l_make} %{l_mflags} install DESTDIR=$RPM_BUILD_ROOT
      ) || exit $?
      rm -f $RPM_BUILD_ROOT%{l_prefix}/bin/convert*
      for bin in ircd mkpasswd servlink viconf viklines vimotd; do
          mv  $RPM_BUILD_ROOT%{l_prefix}/bin/$bin \
              $RPM_BUILD_ROOT%{l_prefix}/bin/ratbox-$bin
      done
      rm -f $RPM_BUILD_ROOT%{l_prefix}/etc/ratbox/example.efnet.conf
      mv $RPM_BUILD_ROOT%{l_prefix}/etc/ratbox/ircd.motd \
         $RPM_BUILD_ROOT%{l_prefix}/etc/ratbox/server.motd
      mv $RPM_BUILD_ROOT%{l_prefix}/man/man8/ircd.8 \
         $RPM_BUILD_ROOT%{l_prefix}/man/man8/ratbox-ircd.8
      %{l_shtool} mkdir -f -p -m 755 \
          $RPM_BUILD_ROOT%{l_prefix}/share/ratbox/etc
      mv  $RPM_BUILD_ROOT%{l_prefix}/etc/ratbox/example.conf \
          $RPM_BUILD_ROOT%{l_prefix}/share/ratbox/etc/server.conf.sample
      rm -rf $RPM_BUILD_ROOT%{l_prefix}/include
    ) || exit $?

    #   install IRC services
    ( cd ratbox-services-%{V_services}
      chmod a+x autoconf/install-sh
      %{l_make} %{l_mflags} install DESTDIR=$RPM_BUILD_ROOT
      find $RPM_BUILD_ROOT%{l_prefix}/share/ratbox/services -type d -print | xargs chmod 755
      mv  $RPM_BUILD_ROOT%{l_prefix}/etc/ratbox/example.conf \
          $RPM_BUILD_ROOT%{l_prefix}/share/ratbox/etc/services.conf.sample
      %{l_shtool} install -c -m 644 \
          tools/base/schema-sqlite.txt $RPM_BUILD_ROOT%{l_prefix}/share/ratbox/services/
      mv  $RPM_BUILD_ROOT%{l_prefix}/bin/dbupgrade.pl \
          $RPM_BUILD_ROOT%{l_prefix}/bin/ratbox-dbupgrade
      mv  $RPM_BUILD_ROOT%{l_prefix}/bin/definetolength.pl \
          $RPM_BUILD_ROOT%{l_prefix}/bin/ratbox-definetolength
      mv  $RPM_BUILD_ROOT%{l_prefix}/bin/ircd-shortcut.pl \
          $RPM_BUILD_ROOT%{l_prefix}/bin/ratbox-ircd-shortcut
      rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/ratbox/services/help/ru
      rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/ratbox/services/help/bg
      rm -f $RPM_BUILD_ROOT%{l_prefix}/share/ratbox/services/lang/ru.lang
      rm -f $RPM_BUILD_ROOT%{l_prefix}/share/ratbox/services/lang/bg.lang
    ) || exit $?

    #   install IRC challenge/response client
    ( cd ratbox-respond-%{V_respond}
      %{l_shtool} install -c -m 755 \
          makekeypair $RPM_BUILD_ROOT%{l_prefix}/bin/ratbox-makekeypair
      %{l_shtool} install -c -s -m 755 \
          ratbox-respond/ratbox-respond $RPM_BUILD_ROOT%{l_prefix}/bin/
    ) || exit $?

    #   post-adjust installation
    find $RPM_BUILD_ROOT%{l_prefix} -type d -name "*-old" -print | xargs rmdir >/dev/null 2>&1 || true
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
    chmod 755 $RPM_BUILD_ROOT%{l_prefix}/bin/*
    chmod 644 $RPM_BUILD_ROOT%{l_prefix}/etc/ratbox/*
    chmod 755 $RPM_BUILD_ROOT%{l_prefix}/etc/ratbox
    chmod 755 $RPM_BUILD_ROOT%{l_prefix}/var/ratbox

    #   install default configuration
    chmod 644 $RPM_BUILD_ROOT%{l_prefix}/share/ratbox/etc/*
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE ratbox-server.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/ratbox/server.conf
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE ratbox-services.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/ratbox/services.conf
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE ratbox-resv.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/ratbox/resv.conf
    ( echo "Welcome to the Example IRC service"
      echo "running on server irc.example.com."
    ) >$RPM_BUILD_ROOT%{l_prefix}/etc/ratbox/server.motd

    #   install run-command script
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.ratbox} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/ratbox/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/ratbox'

%files -f files

%clean

%post
    #   on initial installation, create database
    if [ ! -f $RPM_INSTALL_PREFIX/var/ratbox/services.db ]; then
        $RPM_INSTALL_PREFIX/bin/sqlite3 \
            $RPM_INSTALL_PREFIX/var/ratbox/services.db \
            <$RPM_INSTALL_PREFIX/share/ratbox/services/schema-sqlite.txt
        chown %{l_rusr}:%{l_rgrp} $RPM_INSTALL_PREFIX/var/ratbox/services.db
    fi

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

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

