##
##  pam.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:         pam
Summary:      Information Resource for PAM
URL:          -
Vendor:       The OpenPKG Project
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        BASE
Group:        Meta
License:      PD
Version:      0
Release:      20110219

#   list of sources
Source0:      pamtool
Source1:      rc.pam

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101
PreReq:       OpenPKG, openpkg >= 20160101
Provides:     PAM

%description
    This is the information resource package for PAM.

%track

%prep

%build

%install

    pam_cfgloc=""
    pam_modpfx=""
    pam_incdir=""
    pam_libdir=""

    #   search for configuration location
    for cfgloc in \
        /etc/pam.d \
        /etc/pam.conf \
    ; do
        if [ -f $cfgloc -o -d $cfgloc ]; then
            pam_cfgloc="$cfgloc"
            break
        fi
    done

    #   search for module prefix
    if [ -d $pam_cfgloc ]; then
        mod=`cat $pam_cfgloc/* 2>/dev/null | grep -i '^#*[ 	]*other' | head -1 | awk '{ print $3; }'`
    elif [ -f $pam_cfgloc ]; then
        mod=`cat $pam_cfgloc 2>/dev/null | grep -i '^#*[ 	]*other' | head -1 | awk '{ print $4; }'`
    else
        mod=""
    fi
    case $mod in
        pam_*    ) pam_modpfx="" ;;
        /*/pam_* ) pam_modpfx=`echo $mod | sed 's;^\(/.*/\)pam_[^/]*$;\1;'` ;;
    esac

    #   search for include directory
    for incdir in \
        /usr/include \
        /usr/local/include \
        /opt/include \
    ; do
        if [ -f "$incdir/security/pam_appl.h" ]; then
            pam_incdir="$incdir"
            break
        fi
    done

    #   search for library directory
    for libdir in \
        /lib \
        /usr/lib \
        /usr/local/lib \
        /opt/lib \
    ; do
        for ext in a so sl so.0; do
            if [ -f "$libdir/libpam.$ext" ]; then
                pam_libdir="$libdir"
                break
            fi
        done
        if [ ".$pam_libdir" != . ]; then
            break
        fi
    done

    #   make sure everything was found
    if [ ".$pam_cfgloc" = . ] ||\
       [ ".$pam_incdir" = . ] ||\
       [ ".$pam_libdir" = . ]; then
       set +x
       ( echo "************************************************************"
         echo "**  ERROR: SOME PAM INFORMATION COULD NOT BE DETERMINED!!"
         echo "**  ERROR: SOME PAM INFORMATION COULD NOT BE DETERMINED!!"
         echo "**"
         echo "**  We found out:"
         echo "**    PAM Config  Location:  \"${pam_cfgloc}\""
         echo "**    PAM Module  Prefix:    \"${pam_modpfx}\""
         echo "**    PAM Include Directory: \"${pam_incdir}\""
         echo "**    PAM Library Directory: \"${pam_libdir}\""
         echo "**"
         echo "**  Unfortunately, some information is missing here."
         echo "**"
         echo "**  Be sure to have all system PAM components installed"
         echo "**  in standard locations before building this package!"
         echo "**"
         echo "**  ERROR: SOME PAM INFORMATION COULD NOT BE DETERMINED!!"
         echo "**  ERROR: SOME PAM INFORMATION COULD NOT BE DETERMINED!!"
       echo "************************************************************"
       ) | %{l_rpmtool} msg -b -t error
       exit 1
    fi

    #   install PAM tool
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/sbin
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e 's;@l_platform@;%{l_platform -t};g' \
        %{SOURCE pamtool} $RPM_BUILD_ROOT%{l_prefix}/sbin/

    #   install rc file
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d
    %{l_shtool} install -c -m 755 %{l_value -s -a} \
        -e "s;@pam_cfgloc@;${pam_cfgloc};g" \
        -e "s;@pam_modpfx@;${pam_modpfx};g" \
        -e "s;@pam_incdir@;${pam_incdir};g" \
        -e "s;@pam_libdir@;${pam_libdir};g" \
        %{SOURCE rc.pam} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT %{l_files_std}

%files -f files

%clean

