## ======================================================================

%_rpmconfigdir		%{getconfdir}

## Vendor specific macros
## ======================================================================

%vendor_name		%{?_with_vendor_name}%{!?_with_vendor_name:OSSTech Corp.}
%vendor_nickname	%{?_with_vendor_nickname}%{!?_with_vendor_nickname:OSSTech}
%vendor_url		%{?_with_vendor_url}%{!?_with_vendor_url:https://www.osstech.co.jp/}
%vendor_id		%{?_with_vendor_id}%{!?_with_vendor_id:osstech}
%vendor_info		%{?_with_vendor_info}%{!?_with_vendor_info:OSSTech Corp. <https://www.osstech.co.jp/>}
%vendor_name_prefix	%{vendor_id}-

## ----------------------------------------------------------------------

%vendor_prefix		/opt/%{vendor_id}
%vendor_exec_prefix	%{vendor_prefix}
%vendor_var		%{vendor_prefix}/var
%vendor_bindir		%{vendor_exec_prefix}/bin
%vendor_sbindir		%{vendor_exec_prefix}/sbin
%vendor_libdir		%{vendor_exec_prefix}/%{_lib}
%vendor_libexecdir	%{vendor_exec_prefix}/libexec
%vendor_includedir	%{vendor_prefix}/include
%vendor_sysconfdir	%{vendor_prefix}/etc
%vendor_sharedstatedir	%{vendor_prefix}/var/lib
%vendor_localstatedir	%{vendor_prefix}/var
%vendor_rundir		%{vendor_localstatedir}/run
%vendor_vardir		%{vendor_sharedstatedir}
%vendor_datadir		%{vendor_prefix}/share
%vendor_infodir		%{vendor_prefix}/share/info
%vendor_mandir		%{vendor_prefix}/share/man
%vendor_docdir		%{vendor_prefix}/share/doc
%vendor_svdir		%{vendor_localstatedir}/lib/sv

%_prefix		%{vendor_prefix}
%_exec_prefix		%{vendor_exec_prefix}
%_var			%{vendor_var}
%_bindir		%{vendor_bindir}
%_sbindir		%{vendor_sbindir}
## See OSSTech ticket:41
## "%%{?name_dist:/%%{name_dist}" is for backward compatibility
%_includedir		%{vendor_includedir}%{?dist_name:/%{dist_name}}%{?name_dist:/%{name_dist}}
%_libdir		%{vendor_libdir}%{?dist_name:/%{dist_name}}%{?name_dist:/%{name_dist}}
%_libexecdir		%{vendor_libexecdir}
%_sysconfdir		%{vendor_sysconfdir}
%_sharedstatedir	%{vendor_sharedstatedir}
%_localstatedir		%{vendor_localstatedir}
%_rundir		%{vendor_rundir}
%_vardir		%{vendor_vardir}
%_datadir		%{vendor_datadir}
%_infodir		%{vendor_infodir}
%_mandir		%{vendor_mandir}
%_docdir		%{vendor_docdir}
%_defaultdocdir		%{vendor_docdir}
%_svdir			%{vendor_svdir}

%_initddir		/etc/init.d
# Deprecated misspelling, present for backwards compatibility.
%_initrddir		%{_initddir}
%_pamddir		/etc/pam.d

## ----------------------------------------------------------------------

%with_vendor		1
%vendor_release		%{nil}
%vendor_rpmconfigdir	%{_rpmconfigdir}/%{vendor_id}
%vendor_rpmhome		%{vendor_rpmconfigdir}
%vendor_rpmmacros	%{vendor_rpmconfigdir}/macros

%name_prefix		%{vendor_name_prefix}
%ver_rel		%{?epoch:%{epoch}:}%{version}-%{release}

%platform_python	%{__python3}

## ======================================================================

%__perl                 /usr/bin/perl

%__ld                   %{vendor_bindir}/ld

%__clang                clang -fuse-ld=%{__ld}
%__clangxx              clang++ -fuse-ld=%{__ld}

%___vendor_build_pre \
eval "`env |sed -n 's/^\\([a-zA-Z0-9_]*\\)=.*$/unset \\1 || :/p' |egrep -v ' (_|PATH|PWD|LOGNAME) '`"\
LC_ALL="C"\
PATH="%{vendor_bindir}:%{?___platform_path}%{!?___platform_path:/usr/sbin:/usr/bin:/sbin:/bin}"\
HOME="%{_builddir}"\
export LC_ALL PATH HOME\
%{___build_pre}\
CC="%{__cc}"\
CPP="%{__cpp}"\
CXX="%{__cxx}"\
CPPFLAGS="$CPPFLAGS"\
CFLAGS="${CFLAGS:-%optflags}"\
CXXFLAGS="${CXXFLAGS:-%optflags}"\
FFLAGS="${FFLAGS:-%optflags}"\
#LDFLAGS="-L/XXXX/lib -R/XXXX/lib${LDFLAGS:+ $LDFLAGS}"\
#PKG_CONFIG_PATH="/XXXX/lib/pkgconfig:/XXXX/share/pkgconfig:/usr/lib/pkgconfig${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}"\
PERL="%{__perl}"\
export CC CPP CXX CPPFLAGS CFLAGS CXXFLAGS LDFLAGS PKG_CONFIG_PATH PERL\
%{?___platform_build_pre}\
%{nil}

%___build_template	#!%{___build_shell}\
%{___vendor_build_pre}\
%{nil}
%__spec_prep_pre	%{___vendor_build_pre}
%__spec_build_pre	%{___vendor_build_pre}
%__spec_install_pre	%{___vendor_build_pre}
%__spec_check_pre	%{___vendor_build_pre}
%__spec_clean_pre	%{___vendor_build_pre}

%_install_script_path	%{vendor_bindir}:%{?___platform_path}%{!?___platform_path:/usr/sbin:/usr/bin:/sbin:/bin}

## Override %%__build_id_links not to generate /usr/lib/.build-id/*/*
%_build_id_links	none

%_debuginfo_subpackages 0

## ======================================================================

## RPM: Macros and SPEC to detect "from/to" package version in scriptlet
## https://gist.github.com/fumiyas/67b842325284c8adc186a6d7d541c656
##
## Example:
##
## %pre
## if %{vendor_ver_cmp_opposite < 1.2.3-222.new}; then
##   echo "Upgrade from $(%{vendor_ver_opposite}) (< 1.2.3-222.new)"
## fi
## exit 0
##
## %postun
## if %{vendor_ver_cmp_opposite < 1.2.3-222.new}; then
##   echo "Downgrade to $(%{vendor_ver_opposite}) (< 1.2.3-222.new)"
## fi
## exit 0
##
## NOTE: These macros cannot be used in %posttrans.

## In %pre, %post of the installing package on upgrade or downgrade:
##   Print version string of the uninstalling package
## In %preun, %postun of the uninstalling package on upgrade or downgrade:
##   Print version string of the installing package
%vendor_ver_opposite(n:) \
LC_ALL=C \\\
rpm \\\
  -q \\\
  --queryformat '%%|epoch?{%%{epoch}:}|%%{version}-%%{release}\\n' \\\
  %{!-n:%{name}}%{-n*} \\\
|grep -F -v '%{?epoch:%{epoch}:}%{version}-%{release}' \\\
|grep -v ' is not installed$' \\\
|| :

## Compare 2 version strings with an operator
%vendor_ver_cmp() \
%{platform_python} -c '\
from __future__ import print_function\
import sys\
import rpm\
\
\
def stringToEVR(verstring):\
    i = verstring.find(":")\
    if i == -1:\
        epoch = None\
    else:\
        epoch = verstring[:i]\
    i += 1\
    j = verstring.find("-", i)\
    if j == -1:\
        version = verstring[i:]\
        release = None\
    else:\
        version = verstring[i:j]\
        release = verstring[j + 1:]\
    return (epoch, version, release)\
\
\
v1 = sys.argv[1]\
operator = sys.argv[2]\
v2 = sys.argv[3]\
rc = rpm.labelCompare(stringToEVR(v1), stringToEVR(v2))\
sys.exit(0 if operator == ("<", "==", ">")[rc + 1] else 1)\
'

## In %pre, %post on upgrade or downgrade:
##   Compare the uninstalling package version string and the specified one
## In %preun, %postun on upgrade or downgrade:
##   Compare the installing package version string and the specified one
%vendor_ver_cmp_opposite(n:) \
(\
  ver_opposite=$(%{expand:%%{vendor_ver_opposite %{-n*}}})\
  [[ -z $ver_opposite ]] && exit 1\
  %{vendor_ver_cmp} "$ver_opposite" "%{1}" "%{2}"\
)

## ======================================================================

%vendor_package \
Vendor: %{vendor_info}\
Requires(pre): %{vendor_id}-base >= 3.0\
Provides: %{vendor_id}-package = 3.0\
%{nil}

%vendor_build_env() \
[ -f "%{vendor_rpmconfigdir}/%{1}-build.env" ] && . "%{vendor_rpmconfigdir}/%{1}-build.env"\
%{nil}

## OSSTech GCC uses ld-wrapper script, that emulates SVR4 ld(1)
%vendor_pkg_config_path() \
PKG_CONFIG_PATH="%{vendor_libdir}/%{1}/pkgconfig${PKG_CONFIG_PATH:+:$PKG_CONFIG_PATH}"\
%ifos linux\
LDFLAGS="-Wl,-rpath,%{vendor_libdir}/%{1}${LDFLAGS:+ $LDFLAGS}"\
%else\
LDFLAGS="-Wl,-R%{vendor_libdir}/%{1}${LDFLAGS:+ $LDFLAGS}"\
%endif\
export PKG_CONFIG_PATH LDFLAGS\
%{nil}

## OSSTech GCC uses ld-wrapper script, that emulates SVR4 ld(1)
%vendor_build_flags() \
CPPFLAGS="-I%{vendor_includedir}/%{1}${CPPFLAGS:+ $CPPFLAGS}"\
%ifos linux\
LDFLAGS="-L%{vendor_libdir}/%{1} -Wl,-rpath,%{vendor_libdir}/%{1}${LDFLAGS:+ $LDFLAGS}"\
%else\
LDFLAGS="-L%{vendor_libdir}/%{1} -Wl,-R%{vendor_libdir}/%{1}${LDFLAGS:+ $LDFLAGS}"\
%endif\
export CPPFLAGS LDFLAGS\
## Create a temporary prefix directory in $RPM_BUILD_DIR for a library\
## and set the prefix location into $%%{1}_prefix variable.\
## Some configure scripts expect that *.h and *.so files for a required\
## library are located in %%{_prefix}/include and %%{_prefix}/lib, that is,\
## %%{_prefix}/include/%%{name} and %%{_prefix}/lib/%%{name} are not usable.\
## See also: OSSTech ticket:41\
_vendor_tmp="%{1}_prefix"\
_vendor_tmp="${_vendor_tmp//[^a-z_0-9]/_}"\
eval "${_vendor_tmp}='$RPM_BUILD_DIR/%{name}-%{version}-%{release}-buildrequires-prefix/%{1}'"\
eval "rm -rf \\"\\$${_vendor_tmp}\\""\
eval "mkdir -p \\"\\$${_vendor_tmp}\\""\
eval "ln -s '%{vendor_includedir}/%{1}%{?2}' \\"\\$${_vendor_tmp}/include\\""\
eval "ln -s '%{vendor_libdir}/%{1}%{?2}' \\"\\$${_vendor_tmp}/lib\\""\
unset _vendor_tmp\
[ -f "%{vendor_rpmconfigdir}/%{1}-build.env" ] && . "%{vendor_rpmconfigdir}/%{1}-build.env"\
%{expand:%%{?vendor_%{1}_build_flags}}\
%{nil}

## OSSTech GCC uses ld-wrapper script, that emulates SVR4 ld(1)
%vendor_ld_run_path() \
%ifos linux\
export LDFLAGS="-Wl,-rpath,%{vendor_libdir}/%{1}${LDFLAGS:+ $LDFLAGS}"\
%else\
export LDFLAGS="-Wl,-R%{vendor_libdir}/%{1}${LDFLAGS:+ $LDFLAGS}"\
%endif\
%{nil}

