##
##  limesurvey.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_opkg 2.50
%define       V_dist 250plus-build160810

#   package information
Name:         limesurvey
Summary:      Survey System
URL:          http://www.limesurvey.org/
Vendor:       LimeSurvey Project Team
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Web
License:      GPL
Version:      %{V_opkg}
Release:      20161213

#   package options
%option       with_pgsql   yes
%option       with_mysql   no

#   checking for option conflicts
%if "%{with_mysql}" == "no" && "%{with_pgsql}" == "no"
    %{error:one of the build-time options 'with_mysql' or 'with_pgsql' have to be enabled}
%endif
%if "%{with_mysql}" == "yes" && "%{with_pgsql}" == "yes"
    %{error:only one of the build-time options 'with_mysql' or 'with_pgsql' can be enabled at a time}
%endif

#   list of sources
Source0:      http://download.limesurvey.org/Latest_stable_release/limesurvey%{V_dist}.tar.bz2
Source1:      limesurvey-apache.conf
Source2:      limesurvey-setup.sh
Source3:      rc.limesurvey

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
PreReq:       apache
PreReq:       apache-php
%if "%{with_mysql}" == "yes"
PreReq:       apache-php::with_mysql = yes
%endif
%if "%{with_pgsql}" == "yes"
PreReq:       apache-php::with_pgsql = yes
%endif
PreReq:       apache-php::with_session = yes
PreReq:       apache-php::with_mbstring = yes
PreReq:       apache-php::with_iconv = yes
PreReq:       apache-php::with_gd = yes

%description
    LimeSurvey is a Web application to create online surveys. It
    features open/closed surveys, branching, participant administration,
    quotas, WYSIWYG HTML editor, email invitations & reminders,
    assessments, basic statistics and more.

%track
    prog limesurvey = {
        version   = %{V_dist}
        url       = https://www.limesurvey.org/stable-release
        regex     = limesurvey(\d+plus-build\d+)\.tar\.bz2
    }

%prep
    %setup -q -n limesurvey
    chmod -R u+w .

%build
    rm -f styles/*/fonts/*CREDITS
    rm -f styles/*/fonts/*.ttf.txt
    rm -f styles/*CREDITS
    rm -f styles/*.ttf.txt
    rm -f fonts/*CREDITS
    rm -f fonts/*.ttf.txt

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/sbin \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d \
        $RPM_BUILD_ROOT%{l_prefix}/etc/limesurvey \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/limesurvey \
        $RPM_BUILD_ROOT%{l_prefix}/var/limesurvey/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/limesurvey/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/limesurvey/db

    #   install base system
    cp -rp * $RPM_BUILD_ROOT%{l_prefix}/libexec/limesurvey/

    #   adjust default configuration
%if "%{with_mysql}" == "yes"
    l_dbtype="mysql"
%endif
%if "%{with_pgsql}" == "yes"
    l_dbtype="postgres"
%endif
    %{l_shtool} subst \
        -e "s;\\(databasetype[^']*'\\)[^']*\\('\\);\\1${l_dbtype}\\2;" \
        -e "s;\\(databasename[^']*'\\)[^']*\\('\\);\\1limesurvey\\2;" \
        -e "s;\\(databaseuser[^']*'\\)[^']*\\('\\);\\1limesurvey\\2;" \
        -e "s;\\(databasepass[^']*'\\)[^']*\\('\\);\\1limesurvey\\2;" \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/limesurvey/config.php

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

    #   install database setup script
%if "%{with_mysql}" == "yes"
    l_dbtype="mysql"
%endif
%if "%{with_pgsql}" == "yes"
    l_dbtype="pgsql"
%endif
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e "s;@l_dbtype@;$l_dbtype;g" \
        -e "s;@l_bash@;%{l_bash};g" \
        %{SOURCE limesurvey-setup.sh} $RPM_BUILD_ROOT%{l_prefix}/sbin/limesurvey-setup

    #   install custom Apache configuration
    l_hostname=`%{l_shtool} echo -e %h`
    l_domainname=`%{l_shtool} echo -e %d | cut -c2-`
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        -e "s;@l_hostname@;$l_hostname;g" \
        -e "s;@l_domainname@;$l_domainname;g" \
        %{SOURCE limesurvey-apache.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/limesurvey/

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display final hints on initial installation
        ( echo "1. To complete this installation of LimeSurvey please start"
          echo "   the RDBMS and initialize the LimeSurvey database like this:"
%if "%{with_mysql}" == "yes"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc mysql start"
%endif
%if "%{with_pgsql}" == "yes"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc postgresql start"
%endif
          echo "     \$ $RPM_INSTALL_PREFIX/sbin/limesurvey-setup install"
          echo ""
          echo "2. Now start LimeSurvey by running the command"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc limesurvey start"
          echo "   and connect with a browser to the URL:"
          echo "     http://127.0.0.1:8084/limesurvey/admin/install/"
          echo ""
          echo "3. cleanup after installation to enable run-time:"
          echo "   \$ rm -f $RPM_INSTALL_PREFIX/libexec/limesurvey/admin/install"
          echo ""
          echo "4. By default, LimeSurvey runs its own Apache server on IPv4 address"
          echo "   127.0.0.1, TCP port 8084. Please change this by editing the"
          echo "   \"Listen 127.0.0.1:8084\" directive in"
          echo "     $RPM_INSTALL_PREFIX/etc/limesurvey/limesurvey-apache.conf"
          echo ""
          echo "5. Access LimeSurvey via:"
          echo "     http://127.0.0.1:8080/limesurvey/"
        ) | %{l_rpmtool} msg -b -t notice
    fi
    if [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} limesurvey status 2>/dev/null`
        [ ".$limesurvey_active" = .yes ] && %{l_rc} limesurvey restart
    fi
    exit 0

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

