##
##  oracle.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:         oracle
Summary:      Information Resource for ORACLE
URL:          -
Vendor:       The OpenPKG Project
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Meta
License:      PD
Version:      0
Release:      20080101

#   package options
%option       with_oracle_owner   none
%option       with_oracle_base    none
%option       with_oracle_home    none
%option       with_oracle_incdir  none
%option       with_oracle_libdir  none

#   list of sources
Source0:      rc.oracle

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

%description
    This is the information resource package for the ORACLE RDBMS.
    It provides the necessary glue between a full external ORACLE
    installation or the stipped down installation provided by the
    OpenPKG "oracle-barebone" package.

%track
    prog oracle = {
        disabled
        comment   = "just a meta package without external sources"
        version   = 0
        url       = http://www.example.com/
        regex     = .*
    }

%prep

%build

%install

    #   determine ORACLE_OWNER
    l_oracle_owner="%{with_oracle_owner}"
    if [ ".$l_oracle_owner" = .none ]; then
        for user in oracle ora; do
            pw=`(cat /etc/passwd; ypcat passwd) 2>/dev/null | grep "^$user:" | head -1`
            if [ ".$pw" != . ]; then
                l_oracle_owner="$user"
                break
            fi
        done
    fi

    #   determine ORACLE base directory ($ORACLE_BASE)
    l_oracle_base="%{with_oracle_base}"
    owner_dir="/oracle"
    if [ ".$l_oracle_owner" != .none ]; then
        dir=`(cat /etc/passwd; ypcat passwd) 2>/dev/null | grep "^$l_oracle_owner:" |\
             head -1 | sed -e 's;^.*:\([^:][^:]*\):[^:]*$;\1;' | sed -e 's;^[^/].*$;;'`
        if [ ".$dir" != . ]; then
            owner_dir="$dir"
        fi
    fi
    if [ ".$l_oracle_base" = .none ]; then
        for dir in \
            %{l_prefix}/libexec/oracle-barebone \
            %{l_prefix}/share/oracle-barebone \
            $owner_dir \
            /oracle \
            /home/oracle \
            /export/oracle \
            /usr/oracle \
            /usr/local/oracle \
            /opt/oracle \
            /opt/local/oracle \
            /app/oracle \
            /u01/app/oracle \
            /u02/app/oracle \
            /u03/app/oracle \
            /u04/app/oracle
        do
            if [ -d $dir ]; then
                l_oracle_base="$dir"
                break
            fi
        done
        if [ ".$l_oracle_base" = .none ]; then
           ( echo "ORACLE_BASE directory not found on system."
             echo "Please provide correct path with option with_oracle_base."
           ) | %{l_rpmtool} msg -b -t error
           exit 1
        fi
    else
        if [ ! -d "$l_oracle_base" ]; then
           ( echo "ORACLE_BASE directory \"$l_oracle_base\" not existing."
             echo "Please provide correct path with option with_oracle_base."
           ) | %{l_rpmtool} msg -b -t error
           exit 1
        fi
    fi

    #   determine ORACLE home directory ($ORACLE_HOME)
    l_oracle_home="%{with_oracle_home}"
    if [ ".$l_oracle_home" = .none ]; then
        for dir in $l_oracle_base/product/[0-9]* $l_oracle_base/product $l_oracle_base; do
            if [ -d "$dir/rdbms" ]; then
                l_oracle_home="$dir"
                break
            fi
        done
        if [ ".$l_oracle_home" = .none ]; then
           ( echo "ORACLE_HOME directory not found on system."
             echo "Please provide correct path with option with_oracle_home."
           ) | %{l_rpmtool} msg -b -t error
           exit 1
        fi
    else
        if [ ! -d "$l_oracle_home" ]; then
           ( echo "ORACLE_HOME directory \"$l_oracle_base\" not existing."
             echo "Please provide correct path with option with_oracle_home."
           ) | %{l_rpmtool} msg -b -t error
           exit 1
        fi
        if [ ! -d "$l_oracle_home/rdbms" ]; then
           ( echo "ORACLE_HOME directory \"$l_oracle_home\" does not contain \"rdbms\ subdirectory."
             echo "Please provide correct path with option with_oracle_home."
           ) | %{l_rpmtool} msg -b -t error
           exit 1
        fi
    fi

    #   determine ORACLE C header directory
    l_oracle_incdir="%{with_oracle_incdir}"
    if [ ".$l_oracle_incdir" = .none ]; then
        for dir in $l_oracle_home/rdbms $l_oracle_home; do
            file=`find $dir -type f -name "oratypes.h" -print 2>/dev/null | head -1`
            if [ ".$file" != . ]; then
                if [ -f "$file" ]; then
                    l_oracle_incdir=`echo $file | sed -e 's;/oratypes\.h$;;'`
                    break
                fi
            fi
        done
        if [ ".$l_oracle_incdir" = .none ]; then
           ( echo "ORACLE C include directory not found on system."
             echo "Please provide correct path with option with_oracle_incdir."
           ) | %{l_rpmtool} msg -b -t error
           exit 1
        fi
    else
        if [ ! -d "$l_oracle_incdir" ]; then
           ( echo "ORACLE C include directory \"$l_oracle_incdir\" not existing."
             echo "Please provide correct path with option with_oracle_incdir."
           ) | %{l_rpmtool} msg -b -t error
           exit 1
        fi
        if [ ! -f "$l_oracle_incdir/oratypes.h" ]; then
           ( echo "ORACLE C include directory \"$l_oracle_incdir\" does not contain \"oratypes.h\ header."
             echo "Please provide correct path with option with_oracle_incdir."
           ) | %{l_rpmtool} msg -b -t error
           exit 1
        fi
    fi

    #   determine ORACLE C library directory
    l_oracle_libdir="%{with_oracle_libdir}"
    if [ ".$l_oracle_libdir" = .none ]; then
        for dir in $l_oracle_home/lib32 $l_oracle_home/lib $l_oracle_home/*/lib32 $l_oracle_home/*/lib; do
            file=`find $dir -type f -name "sysliblist" -print 2>/dev/null | head -1`
            if [ ".$file" != . ]; then
                if [ -f "$file" ]; then
                    l_oracle_libdir=`echo $file | sed -e 's;/sysliblist$;;'`
                    break
                fi
            fi
        done
        if [ ".$l_oracle_libdir" = .none ]; then
           ( echo "ORACLE C library directory not found on system."
             echo "Please provide correct path with option with_oracle_libdir."
           ) | %{l_rpmtool} msg -b -t error
           exit 1
        fi
    else
        if [ ! -d "$l_oracle_libdir" ]; then
           ( echo "ORACLE C library directory \"$l_oracle_libdir\" not existing."
             echo "Please provide correct path with option with_oracle_libdir."
           ) | %{l_rpmtool} msg -b -t error
           exit 1
        fi
        if [ ! -f "$l_oracle_libdir/sysliblist" ]; then
           ( echo "ORACLE C library directory \"$l_oracle_libdir\" does not contain \"sysliblist\" file."
             echo "Please provide correct path with option with_oracle_libdir."
           ) | %{l_rpmtool} msg -b -t error
           exit 1
        fi
    fi

    #   determine ORACLE client build flags
    l_oracle_cflags="-I$l_oracle_incdir"
    l_oracle_incdir_public=`echo $l_oracle_incdir | sed -e 's;/[^/]*$;/public;'`
    if [ -d "$l_oracle_incdir_public" ]; then
        l_oracle_cflags="$l_oracle_cflags -I$l_oracle_incdir_public"
    fi
    l_oracle_ldflags="-L$l_oracle_libdir -Wl,-R$l_oracle_libdir"
    if [ -d "${l_oracle_libdir}32" ]; then
        l_oracle_ldflags="-L${l_oracle_libdir}32 -Wl,-R${l_oracle_libdir}32 $l_oracle_ldflags"
    fi
    l_oracle_libs="-lclntsh"
    if [ -f "$l_oracle_libdir/libwtc9.so" ]; then
        l_oracle_libs="$l_oracle_libs -lwtc9"
    fi

    #   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;@l_oracle_owner@;${l_oracle_owner};g" \
        -e "s;@l_oracle_base@;${l_oracle_base};g" \
        -e "s;@l_oracle_home@;${l_oracle_home};g" \
        -e "s;@l_oracle_incdir@;${l_oracle_incdir};g" \
        -e "s;@l_oracle_libdir@;${l_oracle_libdir};g" \
        -e "s;@l_oracle_cflags@;${l_oracle_cflags};g" \
        -e "s;@l_oracle_ldflags@;${l_oracle_ldflags};g" \
        -e "s;@l_oracle_libs@;${l_oracle_libs};g" \
        %{SOURCE rc.oracle} $RPM_BUILD_ROOT%{l_prefix}/etc/rc.d/

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

%files -f files

%clean

