##
##  keycloak.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:         keycloak
Summary:      Authentication Server
URL:          https://www.keycloak.org/
Vendor:       Red Hat
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Database
License:      LGPL
Version:      26.4.0
Release:      20251002

#   list of sources
Source0:      https://github.com/keycloak/keycloak/releases/download/%{version}/keycloak-%{version}.tar.gz
Source1:      rc.keycloak

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101, java, JAVA-JDK

%description
    Keycloak is an Identity and Access Management solution aimed at
    modern applications and services. It makes it easy to secure
    applications and services with little to no code.

%track
    prog keycloak = {
        version   = %{version}
        url       = https://www.keycloak.org/downloads.html
        regex     = keycloak-(__VER__)\.tar\.gz
    }

%prep
    %setup -q

%build
    #   strip down installation files
    rm bin/*.bat
    rm -f LICENSE.txt License.html
    rm -rf domain
    rm -rf docs
    rm -f standalone/configuration/standalone-ha.xml

    #   remove problematic files
    rm -f "themes/keycloak/common/resources/node_modules/rcue/dist/img/Logo_Horizontal_Reversed.svg alias"

%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/keycloak \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/keycloak \
        $RPM_BUILD_ROOT%{l_prefix}/var/keycloak/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/keycloak/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/keycloak/tmp \
        $RPM_BUILD_ROOT%{l_prefix}/var/keycloak/data

    #   install program
    cp -rp * \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/keycloak/

    #   redirect paths
    (   cd  $RPM_BUILD_ROOT%{l_prefix}/libexec/keycloak/standalone
        mv  configuration/* \
            $RPM_BUILD_ROOT%{l_prefix}/etc/keycloak/
        rm -rf configuration
        ln -s ../../../etc/keycloak configuration
        rm -rf data
        ln -s ../../../var/keycloak/data data
        rm -rf log
        ln -s ../../../var/keycloak/log log
        rm -rf tmp
        ln -s ../../../var/keycloak/tmp tmp
    ) || exit $?

    #   provide wrapper scripts
    (   echo "#!/bin/sh"
        echo "cd %{l_prefix}/libexec/keycloak/bin"
        echo "exec ./standalone.sh \${1+\"\$@\"}"
    ) >$RPM_BUILD_ROOT%{l_prefix}/sbin/keycloak
    chmod 755 $RPM_BUILD_ROOT%{l_prefix}/sbin/keycloak
    (   echo "#!/bin/sh"
        echo "cd %{l_prefix}/libexec/keycloak/bin"
        echo "exec ./kcadm.sh \${1+\"\$@\"}"
    ) >$RPM_BUILD_ROOT%{l_prefix}/sbin/kcadm
    chmod 755 $RPM_BUILD_ROOT%{l_prefix}/sbin/kcadm
    (   echo "#!/bin/sh"
        echo "cd %{l_prefix}/libexec/keycloak/bin"
        echo "exec ./kcreg.sh \${1+\"\$@\"}"
    ) >$RPM_BUILD_ROOT%{l_prefix}/sbin/kcreg
    chmod 755 $RPM_BUILD_ROOT%{l_prefix}/sbin/kcreg

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   on initial installation, give hints about next steps
        ( echo "To complete this installation of Keycloak please start it with"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc keycloak start"
          echo "and then visit"
          echo "    http://127.0.0.1:8080/"
          echo "and set your admistrator username/password."
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} keycloak status 2>/dev/null`
        [ ".$keycloak_active" = .yes ] && %{l_rc} keycloak restart
    fi
    exit 0

%preun
    if [ $1 -eq 0 ]; then
        #   stop service and remove run-time files
        %{l_rc} keycloak stop 2>/dev/null
        rm -f  $RPM_INSTALL_PREFIX/var/keycloak/run/*  >/dev/null 2>&1 || true
        rm -f  $RPM_INSTALL_PREFIX/var/keycloak/log/*  >/dev/null 2>&1 || true
        rm -f  $RPM_INSTALL_PREFIX/var/keycloak/tmp/*  >/dev/null 2>&1 || true
        rm -rf $RPM_INSTALL_PREFIX/var/keycloak/data/* >/dev/null 2>&1 || true
    fi
    exit 0

