##
##  twiki.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:         twiki
Summary:      Wiki Wiki based Collaboration Platform
URL:          http://twiki.org/
Vendor:       Peter Thoeny
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        CMS
License:      GPL
Version:      4.1.1
Release:      20080101

#   list of sources
Source0:      http://twiki.org/p/pub/Codev/Release/TWiki-%{version}.tgz
Source1:      twiki-apache.conf
Source2:      rc.twiki

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, perl
PreReq:       OpenPKG, openpkg >= 20160101
PreReq:       perl, perl-www, perl-net, perl-mail, perl-crypto, perl-vcs
PreReq:       rcs, patch, diffutils, grep
PreReq:       apache, MTA

%description
    TWiki s a flexible, powerful, and easy to use Web-based
    collaboration platform based on the popular Wiki Wiki principle.

%track
    prog twiki = {
        version   = %{version}
        url       = http://twiki.org/cgi-bin/view/Codev/DownloadTWiki
        regex     = TWiki-(__VER__)\.tgz
    }

%prep
    %setup -q -c

%build

%install
    #   create installation area
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/twiki \
        $RPM_BUILD_ROOT%{l_prefix}/lib/twiki \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/twiki \
        $RPM_BUILD_ROOT%{l_prefix}/var/twiki

    #   install TWiki CGIs and configuration
    %{l_shtool} install -c -m 755 \
        -e 's;/usr/bin/perl;%{l_prefix}/bin/perl;g' \
        -e "s;'\.\./lib';'%{l_prefix}/lib/twiki';g" \
        bin/* $RPM_BUILD_ROOT%{l_prefix}/libexec/twiki/
    ( cd bin && %{l_tar} cf - logos ) | \
    ( cd $RPM_BUILD_ROOT%{l_prefix}/libexec/twiki && %{l_tar} xf - ) || exit $?
    %{l_shtool} install -c -m 644 \
        -e 's;/absolute/path/to/your/lib;%{l_prefix}/lib/twiki;g' \
        -e 's;^# \(@localPerlLibPath = ( .\)/path/to/dir;\1%{l_prefix}/lib/twiki;g' \
        bin/LocalLib.cfg.txt \
        $RPM_BUILD_ROOT%{l_prefix}/etc/twiki/twiki-lib.cfg
    ln  $RPM_BUILD_ROOT%{l_prefix}/etc/twiki/twiki-lib.cfg \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/twiki/LocalSite.cfg
    rm -f $RPM_BUILD_ROOT%{l_prefix}/libexec/twiki/LocalLib.cfg.txt

    #   install TWiki library and configuration
    %{l_shtool} subst \
        -e 's;/usr/sbin/sendmail;%{l_prefix}/sbin/sendmail;g' \
        -e "s;'/usr/bin';'%{l_prefix}/bin';g" \
        -e "s;'/bin:/usr/bin';'%{l_prefix}/bin:/bin:/usr/bin';g" \
        -e 's;/bin/egrep;%{l_prefix}/bin/egrep;g' \
        -e 's;/bin/fgrep;%{l_prefix}/bin/fgrep;g' \
        lib/TWiki.spec
    ( cd lib && %{l_tar} cf - . ) | \
    ( cd $RPM_BUILD_ROOT%{l_prefix}/lib/twiki && %{l_tar} xf - ) || exit $?
    %{l_shtool} install -c -m 644 \
        -e 's;http://your\.domain\.com;http://localhost;g' \
        -e 's;/home/httpd/twiki/pub;%{l_prefix}/var/twiki/pub;g' \
        -e 's;/home/httpd/twiki/templates;%{l_prefix}/etc/twiki/tmpl;g' \
        -e 's;/home/httpd/twiki/data;%{l_prefix}/var/twiki/data;g' \
        -e '/^1;$/d' \
        lib/LocalSite.cfg.txt \
        $RPM_BUILD_ROOT%{l_prefix}/etc/twiki/twiki-site.cfg
    ( echo ""
      echo "\$TWiki::cfg{LocalesDir} = '%{l_prefix}/libexec/twiki/configurelocale';"
      echo "\$TWiki::cfg{Password} = '0NQiQSaRf7IKo';"
      echo "\$TWiki::cfg{Site}{Lang} = 'en';"
      echo "\$TWiki::cfg{Site}{FullLang} = 'en-us';"
      echo "\$TWiki::cfg{Site}{CharSet} = 'iso-8859-15';"
      echo ""
      echo "1;"
    ) >>$RPM_BUILD_ROOT%{l_prefix}/etc/twiki/twiki-site.cfg
    ln  $RPM_BUILD_ROOT%{l_prefix}/etc/twiki/twiki-site.cfg \
        $RPM_BUILD_ROOT%{l_prefix}/lib/twiki/LocalSite.cfg
    rm -f $RPM_BUILD_ROOT%{l_prefix}/lib/twiki/LocalSite.cfg.txt
    find $RPM_BUILD_ROOT%{l_prefix}/lib/twiki -type f -print | xargs chmod 664
    find $RPM_BUILD_ROOT%{l_prefix}/lib/twiki -type d -print | xargs chmod 775

    #   install TWiki initial data
    #   (adjust RCS content for Apache run-time environment)
    find data -name "*,v" -type f -print | xargs \
    %{l_prefix}/bin/perl \
        -pi.orig -e 'NR <= 10 && s/nobody:/%{l_nusr}:/'
    find data -type f -name "*.orig" -print | xargs rm -f
    find data -type f -print | xargs chmod 664
    find data -type d -print | xargs chmod 775
    %{l_tar} cf - data | \
    ( cd $RPM_BUILD_ROOT%{l_prefix}/var/twiki && %{l_tar} xf - ) || exit $?

    #   install TWiki public files
    find pub -type f -print | xargs chmod 664
    find pub -type d -print | xargs chmod 775
    %{l_tar} cf - pub | \
    ( cd $RPM_BUILD_ROOT%{l_prefix}/var/twiki && %{l_tar} xf - ) || exit $?

    #   install TWiki template files
    mv templates tmpl
    find tmpl -type f -print | xargs chmod 664
    find tmpl -type d -print | xargs chmod 775
    %{l_tar} cf - tmpl | \
    ( cd $RPM_BUILD_ROOT%{l_prefix}/etc/twiki && %{l_tar} xf - ) || exit $?

    #   install Apache configuration
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE twiki-apache.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/twiki/

    #   install run-command script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE rc.twiki} \
        $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/twiki/twiki-*' \
        '%config %{l_prefix}/etc/twiki/templates/*' \
        '%attr(-,%{l_nusr},%{l_ngrp}) %{l_prefix}/var/twiki'

%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 "Include $RPM_INSTALL_PREFIX/etc/twiki/twiki-apache.conf"
            ) | $RPM_INSTALL_PREFIX/lib/openpkg/rpmtool config \
                    -a -i "$RPM_INSTALL_PREFIX:twiki" $apacheconf
        fi

        #   display information about steps the admin should perform next
        ( echo "TWiki was successfully installed. You should now start Apache with"
          echo "  \$ $RPM_INSTALL_PREFIX/bin/openpkg rc apache start"
          echo "and then edit the TWiki site-level preferences:"
          echo "    http://localhost/twiki/bin/view/TWiki/TWikiPreferences"
          echo "The top-level TWiki page is:"
          echo "    http://localhost/twiki/bin/view"
        ) | %{l_rpmtool} msg -b -t notice
    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:twiki" $apacheconf
        fi
    fi

