%define zlib_name zlib
%define zlib_version 1.2.12
%define xgboost_name xgboost
%define xgboost_version v1.4.1
%define dmlc_name dmlc-core
%define dmlc_version v0.5
%define port 7654
%define datapath /var/lib/opengauss
%define apppath %{_prefix}/local/opengauss

Name:           opengauss
Version:        5.0.1
Release:        12
Summary:        openGauss is an open source relational database management system
License:        MulanPSL-2.0 and MIT and BSD and zlib and TCL and Apache-2.0 and BSL-1.0
URL:            https://gitee.com/opengauss/openGauss-server
Source0:        openGauss-server-%{version}.tar.gz
Source2:        %{zlib_name}-%{zlib_version}.tar.gz
Source3:        %{dmlc_name}-%{dmlc_version}.tar.gz
Source4:        %{xgboost_name}-%{xgboost_version}.tar.gz
Source5:        opengauss-bashprofile
Source6:        opengauss.service
Source7:        autostart.sh

Patch0:         og-edit.patch
Patch1:         cmake_compile.patch
Patch2:         compile_2309.patch
Patch3:         openssl3-adptor.patch
Patch20:        zlib.patch
Patch21:        zlib-CVE-2022-37434.patch

BuildRequires:  cmake gcc gcc-c++ openssl-devel python
BuildRequires:  cjson lz4-devel zstd-devel boost-devel cjson-devel
BuildRequires:  libcgroup-devel libcurl-devel unixODBC-devel jemalloc-devel krb5-devel
BuildRequires:  java-1.8.0-openjdk-devel libedit-devel libaio-devel
BuildRequires:  bison flex DCF
BuildRequires:  numactl-devel
%ifarch sw_64
BuildRequires:  libatomic
%endif

%global _privatelibs lib(cjson|ecpg|z|pg|pq)\\.so*
%global __provides_exclude %{_privatelibs}
%global __requires_exclude %{_privatelibs}

Requires: lz4-devel zstd-devel boost-devel cjson-devel
Requires: libcgroup-devel libcurl-devel unixODBC-devel jemalloc-devel 
Requires: java-1.8.0-openjdk-devel libedit-devel libaio-devel
Requires: DCF lsof
Requires: numactl-devel

%description
openGauss kernel : openGauss is an open source relational database management system.

%global debug_package %{nil}

%prep
%setup -q -c -n %{name}-%{version}
%setup -q -D -T -a 2
%setup -q -D -T -a 3
%setup -q -D -T -a 4

pushd openGauss-server-%{version}
%patch0 -p1
%patch1 -p1
%patch2 -p1
%patch3 -p1
popd

pushd %{zlib_name}-%{zlib_version}
%patch20 -p1
%patch21 -p1
popd

%build
build_target=$(pwd)/binarylibs/kernel/dependency

########### build zlib ###########
pushd %{zlib_name}-%{zlib_version}
zlib_dir=${build_target}/zlib1.2.11/comm
CONFIGURE_EXTRA_FLAG="--64"
%ifarch aarch64
    CONFIGURE_EXTRA_FLAG=""
%endif
%ifarch sw_64
    CONFIGURE_EXTRA_FLAG=""
%endif
./configure ${CONFIGURE_EXTRA_FLAG} --prefix=${zlib_dir}
sed -i '21a CFLAGS += -fPIC' Makefile
MAKE_EXTRA_FLAG="-m64"
%ifarch aarch64
    MAKE_EXTRA_FLAG=""
%endif
%ifarch sw_64
    MAKE_EXTRA_FLAG=""
%endif
make CFLAGS="-fPIE -fPIC" SFLAGS="-O2 -fPIC -fstack-protector-strong -Wl,-z,noexecstack -Wl,-z,relro,-z,now ${MAKE_EXTRA_FLAG} -D_LARGEFILE64_SOURCE=1 -DHAVE_HIDDEN" %{?_smp_mflags}
make install
cd contrib/minizip/
make CFLAGS="-O3 -fPIC -I../.." -f Makefile %{?_smp_mflags}
mv libminiz.a libminiunz.a
cp ioapi.h ${zlib_dir}/include/
cp unzip.h ${zlib_dir}/include/
cp libminiunz.a ${zlib_dir}/lib/
chmod 644 ${zlib_dir}/include/ioapi.h ${zlib_dir}/include/unzip.h ${zlib_dir}/lib/libminiunz.a
cp -r ${zlib_dir} ${build_target}/zlib1.2.11/llt
popd

########### build xgboost ###########
pushd %{xgboost_name}-1.4.1
xgboost_dir=${build_target}/xgboost/comm

sed -i '9a set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,-z,now -fPIE -fPIC  -fstack-protector-strong")' ./CMakeLists.txt
sed -i '10a set (CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} -Wl,-z,now -fPIE -fPIC -fstack-protector-strong")' ./CMakeLists.txt
sed -i '11a set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wl,-z,now -fPIE -fPIC -fstack-protector-strong")' ./CMakeLists.txt

rm -rf dmlc-core
cp -r ../dmlc-core-0.5/ ./dmlc-core

rm -rf build_install
mkdir build_install && cd build_install
cmake .. -DCMAKE_INSTALL_PREFIX=${xgboost_dir}
make %{?_smp_mflags}
make install
cp -r ${xgboost_dir} ${build_target}/xgboost/llt
popd

########### build opengauss ###########
pushd openGauss-server-%{version}
opengauss_source_dir=$(pwd)
export ENABLE_LITE_MODE=ON
export DEBUG_TYPE=release
export THIRD_BIN_PATH=${build_target}/../../../binarylibs
export LD_LIBRARY_PATH=$THIRD_BIN_PATH/kernel/dependency/zlib1.2.11/comm/lib:$LD_LIBRARY_PATH
export LD_LIBRARY_PATH=$THIRD_BIN_PATH/kernel/dependency/xgboost/comm/lib:$LD_LIBRARY_PATH
export PREFIX_HOME=${opengauss_source_dir}/mppdb_temp_install

mkdir -p %{buildroot}/var/lib/opengauss/data
mkdir -p %{buildroot}/%{apppath}/script

mkdir -p tmp_build
cd tmp_build
cmake .. -DENABLE_MULTIPLE_NODES=OFF -DENABLE_PRIVATEGAUSS=OFF -DENABLE_THREAD_SAFETY=ON -DENABLE_LITE_MODE=ON -DENABLE_OPENEULER_MAJOR=ON -DWITH_OPENEULER_OS=ON && 
make %{?_smp_mflags}
make install

# separate_debug_symbol.sh dir
cd ${opengauss_source_dir}/build/script
chmod +x ./separate_debug_information.sh
./separate_debug_information.sh
rm -rf ${opengauss_source_dir}/mppdb_temp_install/packages
rm -rf ${opengauss_source_dir}/mppdb_temp_install/symbols

# copy binarylibs packages to /usr/local/opengauss/
cp -r ${opengauss_source_dir}/mppdb_temp_install/* %{buildroot}/usr/local/opengauss
sed -i "/wal_insert_status_entries/d" ${opengauss_source_dir}/build/script/opengauss_config_file_mini
cp ${opengauss_source_dir}/build/script/opengauss_config_file_mini %{buildroot}/%{apppath}/share/postgresql/
popd

# opengauss datanode dir.
install -d -m 700 $RPM_BUILD_ROOT%{?_localstatedir}/lib/opengauss/data

# opengauss .bash_profile
install -m 644 %{SOURCE5} $RPM_BUILD_ROOT%{?_localstatedir}/lib/opengauss/.bash_profile
# auto start files
install -m 644 %{SOURCE6} %{buildroot}/%{apppath}/script/opengauss.service
install -m 700 %{SOURCE7} %{buildroot}/%{apppath}/script/autostart.sh

%pre
/usr/sbin/groupadd -r opengauss >/dev/null 2>&1 || :
/usr/sbin/useradd -M -N -g opengauss -r -d %{datapath} -s /bin/bash -c "openGauss Server" opengauss >/dev/null 2>&1 || :

# for install step
if [ $1 = 1 ]; then
    portinfo=$(lsof -i:%{port})
    if [ "${portinfo}" != "" ]; then
        echo "The port[%{port}] is occupied. Please use command 'lsof -i:%{port} to check it.'"
        exit 1
    fi
fi

# upgrade installation
if [ $1 = 2 ]; then
    pushd openGauss-server-%{version}/liteom
    sh upgrade_GAUSSv5.sh upgrade_pre
    popd
fi

%post
# set opengauss mini config parameters
set_mini_configparam(){
    config_mini_file=%{apppath}/share/postgresql/opengauss_config_file_mini
    echo "port = %{port}" >> ${config_mini_file}

    config_sample_file=%{apppath}/share/postgresql/postgresql.conf.sample
    if [[ -f $config_mini_file ]]
    then
        if [[ ! -f "$config_sample_file" ]]
        then
            echo "postgresql.conf.sample does not exist"
        else
            while IFS= read -r line; do
                SUBSTRING=$(echo $line | cut -d'=' -f 1)"= "
                if grep -q "$SUBSTRING" $config_sample_file ; then
                    sed -i "/$SUBSTRING/c$line" $config_sample_file
                else
                    echo $line >> $config_sample_file
                fi
            done < $config_mini_file
        fi
    fi
}

start_opengauss(){
    process_id=$(ps -ef | grep /usr/local/opengauss/bin/gaussdb | grep /var/lib/opengauss/data | grep -v grep | awk '{print $2}')
    if [ "$process_id" != "" ]; then
        echo "A process of opengauss already exists. Use command (ps -ef | grep /var/lib/opengauss/data) to confirm."
        echo "Please kill the process and reinstall opengauss."
        return 0
    fi

    if [ "`ls -A /var/lib/opengauss/data`" != "" ]; then
        echo "Datanode dir(/var/lib/opengauss/data) is not empty."
        echo "Please delete dir and reinstall opengauss."
        return 0
    fi
    
    result=$(su - opengauss -c "source ~/.bash_profile; gs_initdb -D /var/lib/opengauss/data -U opengauss --nodename=single_node")
    if [ $? -ne 0 ]; then
        echo "Init openGauss database failed."
        exit $result
    else
        echo "Init openGauss database success."
    fi
}

add_service(){
    cp %{apppath}/script/opengauss.service /usr/lib/systemd/system/
    systemctl daemon-reload
}

copy_bashrc(){
    copyResult=$(su - opengauss -c "echo 'export GAUSSHOME=/usr/local/opengauss/' >> ~/.bashrc &&
                                    echo 'export LD_LIBRARY_PATH=/usr/local/opengauss/lib:$LD_LIBRARY_PATH' >> ~/.bashrc &&
                                    echo 'export PATH=/usr/local/opengauss/bin:$PATH' >> ~/.bashrc &&
                                    echo 'export PGDATA=/var/lib/opengauss/data' >> ~/.bashrc &&
                                    echo 'export PORT=7654' >> ~/.bashrc && source ~/.bashrc")
    if [ $? -ne 0 ]; then
        exit $result
    fi
}

# for install step
if [ $1 = 1 ]; then
    chmod -R 755 /usr/local/opengauss
    chown -R opengauss:opengauss /usr/local/opengauss
    set_mini_configparam
    start_opengauss
    add_service
    copy_bashrc
fi

# upgrade installation
if [ $1 = 2 ]; then
    pushd openGauss-server-%{version}/liteom
    sh upgrade_GAUSSv5.sh upgrade_bin
    servicename=/usr/lib/systemd/system/opengauss.service
        if [ -f $servicename ]; then
            systemctl restart opengauss.service
        fi
    sh upgrade_GAUSSv5.sh upgrade_post
    sh upgrade_GAUSSv5.sh upgrade_commit
    popd
fi

%postun
remove_service(){
    servicename=/usr/lib/systemd/system/opengauss.service
    if [ -f $servicename ]; then
        systemctl disable opengauss.service
        rm $servicename
    fi
}
clear_database(){
    pid=$(ps -ef | grep /var/lib/opengauss/data | grep -v grep | awk '{print $2}')
    if [ "$pid" != "" ]; then
        kill -9 $pid
    fi
    if [ -d /var/lib/opengauss/data ]; then
        rm -rf /var/lib/opengauss/data
    fi
    if [ -d /usr/local/opengauss ]; then
        rm -rf /usr/local/opengauss
    fi
}
if [ "$1" = "0" ]; then
    remove_service
    clear_database
fi

%files
%defattr (-,root,root)
%{apppath}
%doc
%{?_localstatedir}/lib/opengauss
%attr(700,opengauss,opengauss) %dir %{?_localstatedir}/lib/opengauss
%attr(700,opengauss,opengauss) %dir %{?_localstatedir}/lib/opengauss/data
%attr(644,opengauss,opengauss) %config(noreplace) %{?_localstatedir}/lib/opengauss/.bash_profile


%changelog
* Fri May 24 2024 zhangzhihui <zhangzhihui42@huawei.com> - 5.0.1-12
- upgrade function

* Mon May 6 2024 liuheng <liuheng76@huawei.com> - 5.0.1-12
- Update version to 5.0.1

* Fri Aug 18 2023 panchenbo <panchenbo@kylinsec.com.cn> - 2.1.0-11
- add sw_64 support

* Mon Aug 14 2023 zhangxubo <zhangxubo1@huawei.com> - 2.1.0-10
- Fix bugs: #I7QJRO

* Tue Jun  6 2023 dillon chen<dillon.chen@gmail.com> - 2.1.0-9
- Fix bugs: #I677QX； add lsof

* Mon Apr 10 2023 panchenbo <panchenbo@kylinsec.com.cn> - 2.1.0-8
- add /etc/kylinsec-release support for KylinSec

* Sat Feb 11 2023 zhangxubo <zhangxubo1@huawei.com> - 2.1.0-7
- Fix bugs: #I6ED94

* Wed Mar 30 2022 ouyangminxiang <ouyangminxiang@kylinsec.com.cn> - 2.1.0-6
- add kylinsec platform 

* Sat Mar 12 2022 zhangxubo <zhangxubo1@huawei.com> - 2.1.0-5
- Fix bugs: #I4VDYQ #I4V624 #I4UOUU #I4TCDB #I4TCB8

* Thu Jan 27 2022 zhangxubo <zhangxubo1@huawei.com> - 2.1.0-4
- Fix bugs: #I4SDWX

* Sat Jan 22 2022 zhangxubo <zhangxubo1@huawei.com> - 2.1.0-3
- Fix bugs: #I4RVNX

* Mon Dec 13 2021 zhangxubo <zhangxubo1@huawei.com> - 2.1.0-2
- Fix bugs: Mask dependencies acquired during installation

* Wed Dec 1 2021 zhangxubo <zhangxubo1@huawei.com> - 2.1.0-1
- Package init
