##
##  htdig.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:         htdig
Summary:      Web Crawler
URL:          http://www.htdig.org/
Vendor:       The ht://Dig Group
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        Searching
License:      GPL
Version:      3.2.0b6
Release:      20080101

#   list of sources
Source0:      http://www.htdig.org/files/htdig-%{version}.tar.gz
Source1:      rc.htdig
Patch0:       htdig.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc, make, flex, perl
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  zlib, openssl
PreReq:       zlib, openssl

%description
    The ht://Dig system is a complete world wide web indexing and
    searching system for a domain or intranet. This system is not meant
    to replace the need for powerful internet-wide search systems
    like Lycos, Infoseek, Google and AltaVista. Instead it is meant
    to cover the search needs for a single company, campus, or even a
    particular sub section of a web site. As opposed to some WAIS-based
    or web-server based search engines, ht://Dig can easily span several
    web servers. The type of these different web servers doesn't matter
    as long as they understand common protocols like HTTP.

%track
    prog htdig = {
        version   = %{version}
        url       = http://www.htdig.org/files/
        regex     = htdig-(__VER__)\.tar\.gz
    }

%prep
    %setup -q
    %patch -p0

%build
    #   configure program
    %{l_shtool} subst \
        -e 's;/cgi-bin/htsearch;/openpkg-cgi/htsearch;g' \
        -e 's;$(VERSION);;' \
        installdir/search.html
    CC="%{l_cc}" \
    CXX="%{l_cxx} -Wno-deprecated" \
    CPPFLAGS="%{l_cppflags}" \
    CFLAGS="%{l_cflags -O}" \
    CXXFLAGS="%{l_cxxflags -O}" \
    ./configure \
        --prefix=%{l_prefix} \
        --with-config-dir=%{l_prefix}/etc/htdig \
        --with-common-dir=%{l_prefix}/etc/htdig \
        --with-database-dir=%{l_prefix}/var/htdig/db \
        --with-default-config-file=%{l_prefix}/etc/htdig/htdig.conf \
        --with-cgi-bin-dir=%{l_prefix}/cgi \
        --with-image-dir=%{l_prefix}/share/htdig/image \
        --with-image-url-prefix=/openpkg-htdig/image \
        --with-search-dir=%{l_prefix}/share/htdig \
        --with-search-form=index.html \
        --with-zlib=%{l_prefix} \
        --with-ssl=%{l_prefix} \
        --with-apache=%{l_prefix}/bin/apache \
        --disable-bigfile \
        --disable-shared

    #   build program
    %{l_make} %{l_mflags}

%install
    #   install program
    %{l_make} %{l_mflags} install AM_MAKEFLAGS="DESTDIR=$RPM_BUILD_ROOT"

    #   strip down installation
    strip $RPM_BUILD_ROOT%{l_prefix}/bin/* >/dev/null 2>&1 || true
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/lib
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/include

    #   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.htdig} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    #   create additional directories
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/var/htdig/run

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   add hook to Apache configuration
        apacheconf="$RPM_INSTALL_PREFIX/etc/apache/apache.conf"
        if [ -f $apacheconf ]; then
            ( echo "Alias /openpkg-htdig $RPM_INSTALL_PREFIX/share/htdig"
            ) | $RPM_INSTALL_PREFIX/lib/openpkg/rpmtool config \
                -a -i "$RPM_INSTALL_PREFIX:htdig" $apacheconf
        fi

        #   display final hints on initial installation
        ( echo "To complete this installation of ht://Dig please edit"
          echo "the configuration file and run ht://Dig once to initially"
          echo "create the database:"
          echo "    \$ vi $RPM_INSTALL_PREFIX/etc/htdig/htdig.conf"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc htdig daily"
          echo "    \$ $RPM_INSTALL_PREFIX/bin/openpkg rc apache stop start"
          echo "Now you should be able to access the search form and"
          echo "perform your first search:"
          echo "    http://localhost/openpkg-htdig/"
        ) | %{l_rpmtool} msg -b -t notice
    fi
    exit 0

%preun
    if [ $1 -eq 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:htdig" $apacheconf
        fi

        #   before erase, remove data
        rm -f $RPM_INSTALL_PREFIX/var/htdig/run/* >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/htdig/db/*  >/dev/null 2>&1 || true
    fi
    exit 0

