##
##  dex.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_dex_base 2.44.0
%define       V_dex_snap 20250902

#   package information
Name:         dex
Summary:      OpenID/OAUTH2 Provider
URL:          https://github.com/dexidp/dex
Vendor:       CoreOS
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Networking
License:      Apache
Version:      %{V_dex_base}.%{V_dex_snap}
Release:      20250902

#   package options
%option       with_pgsql  no

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/dex/dex-%{V_dex_snap}.tar.xz
Source1:      rc.dex
Source2:      dex-setup.sh
Source3:      dex.yaml

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, go
PreReq:       OpenPKG, openpkg >= 20160101, openssl
%if "%{with_pgsql}" == "yes"
PreReq:       postgresql
%endif

%description
    DEX is a federated identity management service. It provides OpenID
    Connect (OIDC) and OAuth 2.0 to users, and can proxy to multiple
    remote identity providers (IdP) to drive actual authentication, as
    well as managing local username/password credentials.

%track
    prog dex:release = {
        version   = %{V_dex_base}
        url       = https://github.com/dexidp/dex/releases
        regex     = tree/v(\d+\.\d+\.\d+)
    }
    prog dex:snapshot = {
        version   = %{V_dex_snap}
        url       = http://download.openpkg.org/components/versioned/dex/
        regex     = dex-(__VER__)\.tar\.xz
    }

%prep
    %setup -q -n dex

%build
    #   build program
    export GOPATH=`pwd`
    cd src/github.com/dexidp/dex
    go build -v -ldflags "-X main.version=%{V_dex_base}" -o bin/dex ./cmd/dex/

%install
    #   create directory 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/dex \
        $RPM_BUILD_ROOT%{l_prefix}/share/dex \
        $RPM_BUILD_ROOT%{l_prefix}/var/dex/log \
        $RPM_BUILD_ROOT%{l_prefix}/var/dex/run \
        $RPM_BUILD_ROOT%{l_prefix}/var/dex/db

    #   install program
    %{l_shtool} install -c -s -m 755 \
        src/github.com/dexidp/dex/bin/dex \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/

    #   install web files
    cp -rp src/github.com/dexidp/dex/web/* \
        $RPM_BUILD_ROOT%{l_prefix}/share/dex/

    #   install setup script
%if "%{with_pgsql}" == "yes"
    db_type="pgsql"
%else
    db_type="sqlite"
%endif
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e "s;@db_type@;$db_type;g" \
        %{SOURCE dex-setup.sh} \
        $RPM_BUILD_ROOT%{l_prefix}/sbin/dex-setup

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

    #   install default configuration
%if "%{with_pgsql}" == "yes"
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        -e '/^#SQLITE#/d' \
        -e 's/^#PGSQL#//' \
        %{SOURCE dex.yaml} $RPM_BUILD_ROOT%{l_prefix}/etc/dex/
%else
    %{l_shtool} install -c -m 644 %{l_value -s -a} \
        -e '/^#PGSQL#/d' \
        -e 's/^#SQLITE#//' \
        %{SOURCE dex.yaml} $RPM_BUILD_ROOT%{l_prefix}/etc/dex/
%endif

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

%files -f files

%clean

%post
    if [ $1 -eq 1 ]; then
        #   on initial installation, give hints about next steps
        ( echo "1. To complete this installation of DEX please"
%if "%{with_pgsql}" == "yes"
          echo "   start PostgreSQL and initialize the DEX database like this:"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc postgresql start"
          echo "     \$ $RPM_INSTALL_PREFIX/sbin/dex-setup install"
          echo "   This will create a database named 'dex' in the RDBMS"
          echo "   with the owner 'dex' and password 'dex'."
%else
          echo "   initialize the DEX database like this:"
          echo "     \$ $RPM_INSTALL_PREFIX/sbin/dex-setup install"
%endif
          echo "2. Now start DEX:"
          echo "     \$ $RPM_INSTALL_PREFIX/bin/openpkg rc dex start"
          echo "3. Finally, you can access DEX unter the base URL:"
          echo "     http://127.0.0.1:5556/"
        ) | %{l_rpmtool} msg -b -t notice
    elif [ $1 -eq 2 ]; then
        #   after upgrade, restart service
        eval `%{l_rc} dex status 2>/dev/null`
        [ ".$dex_active" = .yes ] && %{l_rc} dex restart
    fi
    exit 0

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

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

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

