##
##  sugarcrm.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:         sugarcrm
Summary:      Customer Relationship Management (CRM) System
URL:          http://www.sugarcrm.com/
Vendor:       SugarCRM
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        PLUS
Group:        Web
License:      AGPL
Version:      6.5.20
Release:      20150212

#   package options
%option       with_ldap  yes

#   list of sources
Source0:      http://download.sourceforge.net/sugarcrm/SugarCE-%{version}.zip
Source1:      sugarcrm-apache.conf
Source2:      sugarcrm-setup.sh
Source3:      sugarcrm-cron.sh
Source4:      rc.sugarcrm

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
PreReq:       apache
PreReq:       apache::with_mod_proxy = yes
PreReq:       apache-extfwd
PreReq:       apache-php
PreReq:       apache-php::with_mysql = yes
PreReq:       apache-php::with_sendmail = yes
PreReq:       apache-php::with_mcrypt = yes
PreReq:       apache-php::with_gd = yes
PreReq:       apache-php::with_session = yes
PreReq:       apache-php::with_mbstring = yes
PreReq:       apache-php::with_xml = yes
PreReq:       apache-php::with_simplexml = yes
PreReq:       apache-php::with_zlib = yes
PreReq:       apache-php::with_zip = yes
PreReq:       apache-php::with_imap = yes
PreReq:       apache-php::with_curl = yes
PreReq:       apache-php::with_json = yes
PreReq:       apache-php::with_ctype = yes
PreReq:       apache-php::with_tokenizer = yes
%if "%{with_ldap}" == "yes"
PreReq:       apache-php::with_openldap = yes
%endif
PreReq:       php
PreReq:       php::with_mysql = yes
PreReq:       php::with_sendmail = yes
PreReq:       php::with_mcrypt = yes
PreReq:       php::with_gd = yes
PreReq:       php::with_session = yes
PreReq:       php::with_mbstring = yes
PreReq:       php::with_xml = yes
PreReq:       php::with_simplexml = yes
PreReq:       php::with_zlib = yes
PreReq:       php::with_zip = yes
PreReq:       php::with_imap = yes
PreReq:       php::with_curl = yes
PreReq:       php::with_json = yes
PreReq:       php::with_ctype = yes
PreReq:       php::with_tokenizer = yes
%if "%{with_ldap}" == "yes"
PreReq:       php::with_openldap = yes
%endif
PreReq:       dcron

%description
    This is SugarCRM Community Edition, a full-featured web-based
    Customer Relationship Management (CRM) system. SugarCRM is easy to
    customize and adapts to changing needs.

%track
    prog sugarcrm = {
        version   = %{version}
        url       = http://www.sugarforge.org/content/downloads/index-sugarcrm-sourceforge.php
        regex     = SugarCE-(\d+\.\d+\.\d+)\.zip
    }

%prep
    %setup -q -n SugarCE-Full-%{version}

%build

%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/dcron/crontabs \
        $RPM_BUILD_ROOT%{l_prefix}/etc/sugarcrm \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/sugarcrm \
        $RPM_BUILD_ROOT%{l_prefix}/var/sugarcrm/data \
        $RPM_BUILD_ROOT%{l_prefix}/var/sugarcrm/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/sugarcrm/run/sugarcrm-apache.php-session \
        $RPM_BUILD_ROOT%{l_prefix}/var/sugarcrm/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/sugarcrm/tmp

    #   install application
    cp -rp .[a-z]* * $RPM_BUILD_ROOT%{l_prefix}/libexec/sugarcrm/
    find $RPM_BUILD_ROOT%{l_prefix}/libexec/sugarcrm -name "*.orig" -print | xargs rm -f

    #   move config files to dedicated area
    mv  $RPM_BUILD_ROOT%{l_prefix}/libexec/sugarcrm/config.php \
        $RPM_BUILD_ROOT%{l_prefix}/etc/sugarcrm/config.php
    ln -s \
        ../../etc/sugarcrm/config.php \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/sugarcrm/config.php
    mv  $RPM_BUILD_ROOT%{l_prefix}/libexec/sugarcrm/config_override.php \
        $RPM_BUILD_ROOT%{l_prefix}/etc/sugarcrm/config_override.php
    ln -s \
        ../../etc/sugarcrm/config_override.php \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/sugarcrm/config_override.php

    #   move writeable directories to dedicated area
    for dir in custom cache upload; do
        mv  $RPM_BUILD_ROOT%{l_prefix}/libexec/sugarcrm/$dir \
            $RPM_BUILD_ROOT%{l_prefix}/var/sugarcrm/data/$dir
        ln -s \
            ../../var/sugarcrm/data/$dir \
            $RPM_BUILD_ROOT%{l_prefix}/libexec/sugarcrm/$dir
    done

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

    #   install database setup script
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE sugarcrm-setup.sh} $RPM_BUILD_ROOT%{l_prefix}/sbin/sugarcrm-setup

    #   install cron-job script and configuration
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        %{SOURCE sugarcrm-cron.sh} $RPM_BUILD_ROOT%{l_prefix}/sbin/sugarcrm-cron
    echo "* * * * * su %{l_rusr} -c %{l_prefix}/sbin/sugarcrm-cron" \
        >$RPM_BUILD_ROOT%{l_prefix}/etc/dcron/crontabs/sugarcrm

    #   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 sugarcrm-apache.conf} \
        $RPM_BUILD_ROOT%{l_prefix}/etc/sugarcrm/

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT \
        %{l_files_std} \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/sugarcrm' \
        '%config %attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/etc/sugarcrm/*' \
        '%attr(0750,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/sugarcrm/run/sugarcrm-apache.php-session' \
        '%not %dir %{l_prefix}/etc/dcron' \
        '%not %dir %{l_prefix}/etc/dcron/crontabs' \
        '%attr(-,%{l_susr},%{l_mgrp}) %{l_prefix}/etc/dcron/crontabs/sugarcrm' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/sugarcrm' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/sugarcrm/*' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/var/sugarcrm/*/*' \
        '%config %attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/libexec/sugarcrm/.htaccess' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/libexec/sugarcrm' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/libexec/sugarcrm/config*.php' \
        '%attr(-,%{l_rusr},%{l_rgrp}) %{l_prefix}/libexec/sugarcrm/sugarcrm.log'

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display final hints on initial installation
        ( echo "1. To complete this installation of SugarCRM please start MySQL and"
          echo "   initialize the SugarCRM database like this:"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc mysql start"
          echo "     \$ $RPM_INSTALL_PREFIX/sbin/sugarcrm-setup install"
          echo "   But remember to configure your MySQL installation first."
          echo "   At least set a MySQL administrator password in:"
          echo "     \$ $RPM_INSTALL_PREFIX/etc/mysql/my.pwd"
          echo ""
          echo "2. By default, SugarCRM runs its own Apache server on IPv4 address"
          echo "   127.0.0.1, TCP port 8081. Please change this by editing the"
          echo "   \"Listen 127.0.0.1:8081\" directives in the configuration file"
          echo "   $RPM_INSTALL_PREFIX/etc/sugarcrm/sugarcrm-apache.conf"
          echo ""
          echo "3. After this postinstallation, start SugarCRM by running"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc sugarcrm start"
          echo "   and initialize the SugarCRM database by connecting to:"
          echo "     http://127.0.0.1:8081/sugarcrm/install.php"
          echo ""
          echo "4. Then follow the interactive setup process and use the"
          echo "   following MySQL database information:"
          echo "      Database: sugarcrm"
          echo "      Username: sugarcrm"
          echo "      Password: sugarcrm"
        ) | %{l_rpmtool} msg -b -t notice
    fi
    if [ $1 -eq 2 ]; then
        #   display final hints on update installation
        ( echo "Please run the SugarCRM upgrade procedure by visiting..."
          echo "    http://127.0.0.1:8081/sugarcrm/"
          echo "...and navigate to..."
          echo "    Administration Home / Systems / Upgrade Wizard"
          echo "...in oder to let SugarCRM upgrade the installation."
        ) | %{l_rpmtool} msg -b -t notice

        #   after upgrade, restart service
        eval `%{l_rc} sugarcrm status 2>/dev/null`
        [ ".$sugarcrm_active" = .yes ] && %{l_rc} sugarcrm restart
    fi
    exit 0

%preun
    if [ $1 -eq 0 ]; then
        #   before erase, stop service and remove log files
        %{l_rc} sugarcrm stop 2>/dev/null

        #   remove database
        $RPM_INSTALL_PREFIX/sbin/sugarcrm-setup uninstall >/dev/null 2>&1 || true

        #   remove run-time files
        rm -f $RPM_INSTALL_PREFIX/var/sugarcrm/log/* >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/sugarcrm/run/* >/dev/null 2>&1 || true
        rm -f $RPM_INSTALL_PREFIX/var/sugarcrm/tmp/* >/dev/null 2>&1 || true
    fi
    exit 0

