##
##  yourls.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:         yourls
Summary:      URL Shortening Service
URL:          http://yourls.org/
Vendor:       Yours Community
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Web
License:      MIT
Version:      1.9.1
Release:      20220530

#   list of sources
Source0:      https://github.com/YOURLS/YOURLS/archive/%{version}.tar.gz
Source1:      yourls-apache.conf
Source2:      yourls-apache.htaccess
Source3:      yourls-setup.sh
Source4:      rc.yourls
Patch0:       yourls.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, apg
PreReq:       OpenPKG, openpkg >= 20160101
PreReq:       apache
PreReq:       apache-php
PreReq:       apache-php::with_mysql = yes
PreReq:       apache-php::with_session = yes
PreReq:       apache-php::with_curl = yes
PreReq:       apache-php::with_xml = yes
PreReq:       apache-php::with_bc = yes
PreReq:       apache-php::with_mbstring = yes
PreReq:       apache-php::with_iconv = yes
PreReq:       mysql

%description
    YOURLS is a set of PHP scripts that will allow you to run Your Own
    URL Shortener. You'll have full control over your data, detailed
    stats, analytics, plugins, and more. It's free.

%track
    prog yourls = {
        version   = %{version}
        url       = https://github.com/YOURLS/YOURLS/releases
        regex     = (\d+\.\d+\.\d+)\.tar\.gz
    }

%prep
    %setup -q -n YOURLS-%{version}
    %patch -p0

%build
    #   remove unnecessary files
    rm -f [A-Z]*.md
    rm -f sample-public-api.txt
    rm -f sample-remote-api-call.txt

    #   replace cookie secret
    secret="`apg -n1 -a1 -m64 -x64 -M NCL`"
    %{l_shtool} subst \
        -e "s;modify this text with something random;$secret;" \
        user/config-sample.php

    #   move files in place
    mv user/config-sample.php user/config.php
    mv sample-public-front-page.txt index.php
    mv sample-robots.txt robots.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/yourls \
        $RPM_BUILD_ROOT%{l_prefix}/lib/yourls \
        $RPM_BUILD_ROOT%{l_prefix}/var/yourls/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/yourls/run/session \
        $RPM_BUILD_ROOT%{l_prefix}/var/yourls/log

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

    #   move YOURLS program files into place
    cp -rp * \
        $RPM_BUILD_ROOT%{l_prefix}/lib/yourls/

    #   move files
    mv  $RPM_BUILD_ROOT%{l_prefix}/lib/yourls/index.php \
        $RPM_BUILD_ROOT%{l_prefix}/etc/yourls/yourls-index.php
    ln -s ../../etc/yourls/yourls-index.php \
        $RPM_BUILD_ROOT%{l_prefix}/lib/yourls/index.php
    mv  $RPM_BUILD_ROOT%{l_prefix}/lib/yourls/user/config.php \
        $RPM_BUILD_ROOT%{l_prefix}/etc/yourls/yourls-config.php
    ln -s ../../../etc/yourls/yourls-config.php \
        $RPM_BUILD_ROOT%{l_prefix}/lib/yourls/user/config.php

    #   install Apache local configuration
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        %{SOURCE yourls-apache.htaccess} $RPM_BUILD_ROOT%{l_prefix}/lib/yourls/.htaccess

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

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   display final hints on initial installation
        ( echo "1. To complete this installation of YOURLS please start MySQL and"
          echo "   initialize the YOURLS database like this:"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc mysql start"
          echo "     \$ $RPM_INSTALL_PREFIX/sbin/yourls-setup install"
          echo ""
          echo "2. By default, YOURLS 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/yourls/yourls-apache.conf"
          echo ""
          echo "3. After this postinstallation, start YOURLS by running"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc yourls start"
          echo "   and initialize the YOURLS database by connecting to:"
          echo "     http://127.0.0.1:8081/"
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} yourls status 2>/dev/null`
        [ ".$yourls_active" = .yes ] && %{l_rc} yourls restart
    fi
    exit 0

%preun
    if [ $1 -eq 0 ]; then
        #   stop service
        %{l_rc} yourls stop 2>/dev/null

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

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

