##
##  phabricator.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:         phabricator
Summary:      Software Engineering Platform
URL:          http://phabricator.org/
Vendor:       Phacility, Inc.
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        SCM
License:      Apache
Version:      20200718
Release:      20200718

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/phabricator/phabricator-%{version}.tar.gz
Source1:      http://download.openpkg.org/components/versioned/phabricator/libphutil-%{version}.tar.gz
Source2:      http://download.openpkg.org/components/versioned/phabricator/arcanist-%{version}.tar.gz
Source3:      phabricator-apache.conf
Source4:      phabricator-setup.sh
Source5:      rc.phabricator
Source6:      local.json
Source7:      preamble.php

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, rsync
PreReq:       OpenPKG, openpkg >= 20160101
PreReq:       apache
PreReq:       apache-php
PreReq:       apache-php::with_json = yes
PreReq:       apache-php::with_session = yes
PreReq:       apache-php::with_mbstring = yes
PreReq:       apache-php::with_ctype = yes
PreReq:       apache-php::with_iconv = yes
PreReq:       apache-php::with_mhash = yes
PreReq:       apache-php::with_ssl = yes
PreReq:       apache-php::with_curl = yes
PreReq:       apache-php::with_mysql = yes
PreReq:       apache-php::with_gd = yes
PreReq:       apache-php::with_fileinfo = yes
PreReq:       apache-php::with_posix = yes
PreReq:       php
PreReq:       php::with_json = yes
PreReq:       php::with_session = yes
PreReq:       php::with_mbstring = yes
PreReq:       php::with_ctype = yes
PreReq:       php::with_iconv = yes
PreReq:       php::with_mhash = yes
PreReq:       php::with_ssl = yes
PreReq:       php::with_curl = yes
PreReq:       php::with_mysql = yes
PreReq:       php::with_gd = yes
PreReq:       php::with_fileinfo = yes
PreReq:       php::with_posix = yes
PreReq:       php::with_pcntl = yes
PreReq:       git

%description
    Phabricator is a collection of web applications which help software
    companies build better software. Phabricator includes applications
    for: reviewing and auditing source code; hosting and browsing
    repositories; assembling a party to venture forth; tracking bugs;
    hiding stuff from coworkers; and also some other things.

%track
    prog phabricator:phabricator = {
        version   = %{version}
        url       = http://download.openpkg.org/components/versioned/phabricator/
        regex     = phabricator-(__VER__)\.tar\.gz
    }
    prog phabricator:libphutil = {
        version   = %{version}
        url       = http://download.openpkg.org/components/versioned/phabricator/
        regex     = libphutil-(__VER__)\.tar\.gz
    }
    prog phabricator:arcanist = {
        version   = %{version}
        url       = http://download.openpkg.org/components/versioned/phabricator/
        regex     = arcanist-(__VER__)\.tar\.gz
    }

%prep
    %setup -q -n phabricator
    %setup -q -n phabricator -T -D -a 1
    %setup -q -n phabricator -T -D -a 2

%build
    #   patch sources
    %{l_shtool} subst %{l_value -s -a} \
        src/infrastructure/storage/management/workflow/PhabricatorStorageManagementWorkflow.php
    %{l_shtool} subst \
        -e 's;#!/usr/bin/env php;#!%{l_prefix}/bin/php;g' \
        -e '2aset_include_path("%{l_prefix}/lib/phabricator:".get_include_path());' \
        `find scripts -name "*.php" -print`

%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/phabricator/local \
        $RPM_BUILD_ROOT%{l_prefix}/lib/phabricator \
        $RPM_BUILD_ROOT%{l_prefix}/var/phabricator/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/phabricator/pid \
        $RPM_BUILD_ROOT%{l_prefix}/var/phabricator/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/phabricator/run/session \
        $RPM_BUILD_ROOT%{l_prefix}/var/phabricator/repo \
        $RPM_BUILD_ROOT%{l_prefix}/var/phabricator/files

    #   install program files
    rsync -ra [a-z]* \
        $RPM_BUILD_ROOT%{l_prefix}/lib/phabricator/

    #   move away configuration
    (   cd $RPM_BUILD_ROOT%{l_prefix}/lib/phabricator/conf
        rm -rf local; ln -s ../../../etc/phabricator/local local
    ) || exit $?

    #   provide binary wrapper scripts
    for bin in `cd bin && echo *`; do
        (   echo "#!/bin/sh"
            echo "PATH=\"%{l_prefix}/bin:\$PATH\"; export PATH"
            echo "%{l_prefix}/bin/php \\%{l_nil}"
            echo "    -d include_path=\"%{l_prefix}/lib/phabricator:%{l_prefix}/lib/php\" \\%{l_nil}"
            echo "    %{l_prefix}/lib/phabricator/bin/$bin \\%{l_nil}"
            echo "    \${1+\"\$@\"}"
        ) >$bin
        %{l_shtool} install -c -m 755 \
            $bin $RPM_BUILD_ROOT%{l_prefix}/sbin/phabricator-$bin
    done
    (   echo "#!/bin/sh"
        echo "PATH=\"%{l_prefix}/bin:\$PATH\"; export PATH"
        echo "%{l_prefix}/bin/php \\%{l_nil}"
        echo "    -d include_path=\"%{l_prefix}/lib/phabricator:%{l_prefix}/lib/php\" \\%{l_nil}"
        echo "    %{l_prefix}/lib/phabricator/arcanist/scripts/arcanist.php \\%{l_nil}"
        echo "    \${1+\"\$@\"}"
    ) >arc
    %{l_shtool} install -c -m 755 \
        arc $RPM_BUILD_ROOT%{l_prefix}/sbin/phabricator-arc

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

    #   install default Apache webserver 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 phabricator-apache.conf} $RPM_BUILD_ROOT%{l_prefix}/etc/phabricator/

    #   install default Phabricator configuration
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE local.json} $RPM_BUILD_ROOT%{l_prefix}/etc/phabricator/local/

    #   install default Phabricator preamble script
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE preamble.php} $RPM_BUILD_ROOT%{l_prefix}/lib/phabricator/support/

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

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

%files -f files

%clean

%post
    #   after upgrade, restart service
    if [ $1 -eq 1 ]; then
        ( echo "1. To complete this installation of Phabricator please start MySQL and"
          echo "   initialize the Phabricator database like this:"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc mysql start"
          echo "     \$ $RPM_INSTALL_PREFIX/sbin/phabricator-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, Phabricator 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/phabricator/phabricator-apache.conf"
          echo ""
          echo "3. After these post-installation steps, start Phabricator by running"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc phabricator start"
          echo "   and initialize the Phabricator database by connecting to:"
          echo "     http://127.0.0.1:8081/"
          echo ""
          echo "4. You usually want to post-adjust the configuration, especially"
          echo "   to finally set hosting URL (which cannot have a path part):"
          echo "     \$ $RPM_INSTALL_PREFIX/sbin/phabricator-config set \\%{l_nil}"
          echo "     phabricator.base-uri 'http://phabricator.example.com/'"
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        eval `%{l_rc} phabricator status 2>/dev/null`
        [ ".$phabricator_active" = .yes ] && %{l_rc} phabricator stop
        $RPM_INSTALL_PREFIX/sbin/phabricator-setup upgrade
        [ ".$phabricator_active" = .yes ] && %{l_rc} phabricator start
    fi
    exit 0

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

