##
##  cgiirc.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:         cgiirc
Summary:      CGI Interface to IRC
URL:          http://cgiirc.sourceforge.net/
Vendor:       David Leadbeater
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        InstantMessaging
License:      Open Source
Version:      0.5.9
Release:      20101219

#   list of sources
Source0:      http://download.sourceforge.net/cgiirc/cgiirc-%{version}.tar.gz
Source1:      cgiirc.config
Source2:      cgiirc.access

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101, perl, apache

%description
    CGI:IRC is a Perl/CGI program that lets you access Internet Relay
    Chat (IRC) from a web browser, it is designed to be flexible and has
    many uses such as an IRC gateway for an IRC network, a chat-room for
    a website or to access IRC when stuck behind a restrictive firewall.

%track
    prog cgiirc = {
        version   = %{version}
        url       = http://sourceforge.net/projects/cgiirc/files/cgi-irc/
        regex     = cgiirc-(__VER__)\.tar\.gz
    }

%prep
    %setup -q

%build

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/cgiirc \
        $RPM_BUILD_ROOT%{l_prefix}/share/cgiirc

    #   install components
    cp -rp * $RPM_BUILD_ROOT%{l_prefix}/share/cgiirc/
    ( cd $RPM_BUILD_ROOT%{l_prefix}/share/cgiirc
      mv cgiirc.config.full cgiirc.config.sample
      rm -rf cgiirc.config
      rm -f ipaccess.example
      rm -f README client.c
      rm -rf docs
      %{l_shtool} subst \
          -e 's;/usr/bin/perl;%{l_prefix}/bin/perl;g' \
          -e 's;/etc/cgiirc/;%{l_prefix}/etc/cgiirc/;g' \
          *.cgi
      mv formats $RPM_BUILD_ROOT%{l_prefix}/etc/cgiirc/
    ) || exit $?

    #   install default configuration
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE cgiirc.config} %{SOURCE cgiirc.access} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/cgiirc/

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%config %{l_prefix}/etc/cgiirc/*' \
        '%config %{l_prefix}/etc/cgiirc/*/*'

%files -f files

%clean

%post
    if [ ".$1" = .1 ]; then
        #   add hook to Apache configuration
        apacheconf="$RPM_INSTALL_PREFIX/etc/apache/apache.conf"
        if [ -f $apacheconf ]; then
            ( echo "Alias /cgiirc $RPM_INSTALL_PREFIX/share/cgiirc"
              echo "<Directory $RPM_INSTALL_PREFIX/share/cgiirc>"
              echo "    DirectoryIndex irc.cgi"
              echo "    Options +ExecCGI"
              echo "    AllowOverride All"
              echo "</Directory>"
            ) | $RPM_INSTALL_PREFIX/lib/openpkg/rpmtool config \
                    -a -i "$RPM_INSTALL_PREFIX:cgiirc" $apacheconf
        fi
    fi

%postun
    if [ ".$1" = .0 ]; then
        #   remove hook from Apache configuration
        apacheconf="$RPM_INSTALL_PREFIX/etc/apache/apache.conf"
        if [ -f $apacheconf ]; then
            $RPM_INSTALL_PREFIX/lib/openpkg/rpmtool config \
                -r -i "$RPM_INSTALL_PREFIX:cgiirc" $apacheconf
        fi
    fi

