%define cjson_name cJSON
%define cjson_version 1.7.13
%define zlib_name zlib
%define zlib_version 1.2.11
%define protobuf_name protobuf
%define protobuf_version 3.11.3
%define orc_name orc-rel-release
%define orc_version 1.6.0
%define port 7654
%define datapath /var/lib/opengauss
%define apppath %{_prefix}/local/opengauss

Name:           opengauss
Version:        2.1.0
Release:        3
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
Source1:        %{cjson_name}-%{cjson_version}.tar.gz
Source2:        %{zlib_name}-%{zlib_version}.tar.gz
Source3:        %{protobuf_name}-%{protobuf_version}.tar.gz
Source4:        %{orc_name}-%{orc_version}.tar.gz
Source5:        opengauss-bashprofile
Source6:        opengauss.service
Source7:        autostart.sh

Patch0:         og-edit.patch
Patch1:         0001-cJSON.patch
Patch20:        0001-zlib.patch
Patch21:        0002-zlib.patch
Patch22:        0003-zlib.patch
Patch23:        makefile-miniunz.patch
Patch40:        0001-orc.patch

BuildRequires:  cmake gcc gcc-c++ openssl-devel python
BuildRequires:  cjson lz4-devel protobuf-devel snappy-devel zstd-devel boost-devel 
BuildRequires:  libcgroup-devel libcurl-devel unixODBC-devel jemalloc-devel 
BuildRequires:  java-1.8.0-openjdk-devel libedit-devel libaio-devel
BuildRequires:  bison flex DCF
%ifnarch aarch64 %{arm}
BuildRequires:  numactl-devel
%endif

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

Requires: lz4-devel protobuf-devel snappy-devel zstd-devel boost-devel 
Requires: libcgroup-devel libcurl-devel unixODBC-devel jemalloc-devel 
Requires: java-1.8.0-openjdk-devel libedit-devel libaio-devel
Requires: DCF
%ifnarch aarch64 %{arm}
Requires: numactl-devel
%endif

%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 1
%setup -q -D -T -a 2
%setup -q -D -T -a 3
%setup -q -D -T -a 4

pushd openGauss-server-%{version}
%patch0 -p1
popd

pushd %{cjson_name}-%{cjson_version}
%patch1 -p1
popd

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

pushd %{orc_name}-%{orc_version}
%patch40 -p1
popd

%build
########### build cjson ###########
build_target=$(pwd)/deps_binarylibs

pushd %{cjson_name}-%{cjson_version}
make
make install PREFIX=${build_target}/cjson
popd

########### build zlib ###########
pushd %{zlib_name}-%{zlib_version}
CONFIGURE_EXTRA_FLAG="--64"
%ifarch aarch64
    CONFIGURE_EXTRA_FLAG=""
%endif
./configure ${CONFIGURE_EXTRA_FLAG} --prefix=${build_target}/zlib
sed -i '21a CFLAGS += -fPIC' Makefile
MAKE_EXTRA_FLAG="-m64"
%ifarch aarch64
    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 -f Makefile.miniunz %{?_smp_mflags}
rm -rf ${build_target}/zlib/lib/pkgconfig
make install -f Makefile.miniunz DESTDIR=${build_target}/zlib
popd

########### build protobuf ###########
pushd %{protobuf_name}-%{protobuf_version}/cmake
sed -i '18a set(CMAKE_CXX_FLAGS \"${CMAKE_CXX_FLAGS} -fPIE -fPIC -fstack-protector-all -Wstack-protector -s -Wl,-z,relro,-z,now -D_GLIBCXX_USE_CXX11_ABI=0\")' CMakeLists.txt
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=${build_target}/protobuf/ -Dprotobuf_BUILD_TESTS=OFF;
make CFLAGS="-fPIE -fPIC" %{?_smp_mflags}
make install
popd

########### build apache-orc ###########
pushd %{orc_name}-%{orc_version}
cp ${build_target}/protobuf  ./c++/libs/protobuf -rp
cp ${build_target}/zlib  ./c++/libs/zlib -rp
export PATH=${build_target}/protobuf/bin:$PATH
export LD_LIBRARY_PATH=$(pwd)/c++/libs:$LD_LIBRARY_PATH
export ORC_LIBS=$(pwd)/c++/libs
sed -i \
-e "32a set (PROTOBUF_INCLUDE_DIRS \"$ORC_LIBS/protobuf/include\")" \
-e "32a set (PROTOBUF_LIBRARIES \"$ORC_LIBS/protobuf/lib/libprotobuf.a\")" \
-e "32a set (PROTOBUF_EXECUTABLE `which protoc`)" \
-e "32a set (SNAPPY_INCLUDE_DIRS \"/usr/include\")" \
-e "32a set (SNAPPY_LIBRARIES \"/usr/lib64/libsnappy.a\")" \
-e "32a set (ZLIB_INCLUDE_DIRS \"$ORC_LIBS/zlib/include\")" \
-e "32a set (ZLIB_LIBRARIES \"$ORC_LIBS/zlib/lib/libz.a\")" \
-e "32a set (LZ4_INCLUDE_DIRS \"/usr/include\")" \
-e "32a set (LZ4_LIBRARIES \"/usr/lib64/liblz4.a\")" CMakeLists.txt
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX=${build_target}/liborc -DBUILD_JAVA=OFF -DBUILD_SHARED_LIBS=OFF
make %{?_smp_mflags}
make install
cp c++/src/orc_proto.pb.h ${build_target}/liborc/include
cp c++/src/Adaptor.hh ${build_target}/liborc/include/orc/
cp ../c++/src/Exceptions.hh ${build_target}/liborc/include/orc/
popd

########### build opengauss ###########
pushd openGauss-server-%{version}
chmod +x ./configure
./configure \
CC=g++ CFLAGS='-O2' \
--prefix=%{apppath} \
--3rd=${build_target} \
--enable-thread-safety \
--with-readline \
--without-zlib \
--without-gssapi \
--without-krb5 \
--enable-lite-mode \
--with-openeuler-os
%make_build  %{?_smp_mflags}
popd

%install
pushd openGauss-server-%{version}
%make_install -s %{?_smp_mflags}
mkdir -p %{buildroot}/var/lib/opengauss/data
mkdir -p %{buildroot}/%{apppath}/script
cp 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 -g 39 -o -r opengauss >/dev/null 2>&1 || :
/usr/sbin/useradd -M -N -g opengauss -o -r -d %{datapath} -s /bin/bash \
        -c "openGauss Server" -u 39 opengauss >/dev/null 2>&1 || :

%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."
        echo $result
    else
        echo "Init openGauss database success."
        result=`su - opengauss -c "source ~/.bash_profile; gs_ctl start -D /var/lib/opengauss/data"`
        if [ $? -ne 0 ]; then
            echo "Start openGauss database failed."
            echo $result
        else
            echo "Start openGauss database success."
        fi
    fi
}

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

# for install step
if [ $1 = 1 ]; then
    set_mini_configparam
    add_service
    start_opengauss
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 [ "$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
* 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
