%define __check_files %{nil}
%define __spec_build_pre %{nil}
%define __arch_install_post %{nil}
%define release_version 1
%define release_date %(echo `date +%%Y%%m%%d`)

Name: OMC 
Summary: AGrandTech 5GC OMC
Version: 1.6.1
Release: %{release_date}.%{release_version}
Vendor: AGrandTech
URL: https://www.agrandtech.com/
License: commercial
Group: Applications/Communications
#BuildRoot: $PWD/rpmoutput
%description
AGrandTech's 5GC OMC

%prep
rm -rf ${RPM_BUILD_ROOT}/etc
rm -rf ${RPM_BUILD_ROOT}/lib
rm -rf ${RPM_BUILD_ROOT}/usr
mkdir -p ${RPM_BUILD_ROOT}
cd ${RPM_BUILD_ROOT}
mkdir -p usr/local/omc/
mkdir -p usr/local/db/
mkdir -p etc/nginx/conf.d
mkdir -p lib/systemd/system/

cd ${RPM_BUILD_DIR}
cp -rf omc/.ssh ${RPM_BUILD_ROOT}/usr/local/omc/
cp -rf omc/mmllog ${RPM_BUILD_ROOT}/usr/local/omc/
cp -rf omc/mmlhome ${RPM_BUILD_ROOT}/usr/local/omc/
cp -rf omc/bin ${RPM_BUILD_ROOT}/usr/local/omc/
cp -rf omc/etc ${RPM_BUILD_ROOT}/usr/local/omc/
cp -rf omc/htdocs ${RPM_BUILD_ROOT}/usr/local/omc/
cp -rf omc/log ${RPM_BUILD_ROOT}/usr/local/omc/
cp -rf omc/backup ${RPM_BUILD_ROOT}/usr/local/omc/
cp -rf omc/upload ${RPM_BUILD_ROOT}/usr/local/omc/
cp -rf omc/software ${RPM_BUILD_ROOT}/usr/local/omc/
cp -rf omc/license ${RPM_BUILD_ROOT}/usr/local/omc/
cp -rf omc/database ${RPM_BUILD_ROOT}/usr/local/omc/
cp -rf db/* ${RPM_BUILD_ROOT}/usr/local/db/
cp -f nginx/omc.conf ${RPM_BUILD_ROOT}/etc/nginx/conf.d/
cp -f systemd/restagent.service ${RPM_BUILD_ROOT}/lib/systemd/system/restagent.service
cp -f systemd/crontask.service ${RPM_BUILD_ROOT}/lib/systemd/system/crontask.service
cp -f systemd/sshsvc.service ${RPM_BUILD_ROOT}/lib/systemd/system/sshsvc.service
cp -f systemd/captrace.service ${RPM_BUILD_ROOT}/lib/systemd/system/captrace.service
cp -f systemd/adb.service ${RPM_BUILD_ROOT}/lib/systemd/system/adb.service

%build
#cd %{buildroot}/..
#make

# 安装完成后执行
%post
RedisConfDir=/usr/local/db/bin/conf
OMCEtcDir=/usr/local/omc/etc
CFileList="restconf.yaml crontask.yaml tasks.yaml sshsvc.yaml capconf.yaml"
for CFile in ${CFileList}; do
    if [ ! -e "${OMCEtcDir}/${CFile}" ]; then
        cp ${OMCEtcDir}/default/${CFile} ${OMCEtcDir};
    fi                 
done
if [ ! -e "${RedisConfDir}/redis.conf" ]; then    cp ${RedisConfDir}/default/redis.conf ${RedisConfDir}; fi
if ! id -u omc >/dev/null 2>&1 ; then     useradd -d /opt/omc -m -s /bin/bash -p1000ftp@kp omc; else echo "user omc exist"; fi
mkdir -p /opt/omc/ftp
mkdir -p /opt/omc/ftp/log
chmod +rx /usr/local/omc 
chmod +rx /usr/local/omc/*
chmod +rx /usr/local/db/bin/*
chmod -R 755 /usr/local/omc/htdocs/front
systemctl daemon-reload
systemctl enable restagent.service
systemctl enable crontask.service
systemctl enable sshsvc.service
systemctl enable captrace.service
systemctl enable adb.service

# 卸载时执行
%postun
if [ "$1" = "0" ] ; then
systemctl stop restagent.service
systemctl stop crontask.service
systemctl stop sshsvc.service
systemctl stop captrace.service
#rm -rf /usr/local/omc
rm -rf /etc/nginx/conf.d/omc.conf
systemctl disable restagent.service
systemctl disable crontask.service
systemctl disable sshsvc.service
systemctl disable captrace.service
systemctl disable adb.service
systemctl daemon-reload
else
systemctl daemon-reload
systemctl restart nginx.service
systemctl restart restagent.service
systemctl restart crontask.service
systemctl restart sshsvc.service
systemctl restart captrace.service
systemctl restart adb.service
fi

# 规定那些文件必须放入安装程序中，如果没有就报错
%files
%defattr(-,root,root)
/usr/local/omc/.ssh
/usr/local/omc/mmllog
/usr/local/omc/mmlhome
/usr/local/omc/bin
/usr/local/omc/etc
/usr/local/omc/htdocs
/usr/local/omc/log
/usr/local/omc/backup
/usr/local/omc/upload
/usr/local/omc/software
/usr/local/omc/license
/usr/local/omc/database
/usr/local/db
/etc/nginx/conf.d/omc.conf
/lib/systemd/system/restagent.service
/lib/systemd/system/crontask.service
/lib/systemd/system/sshsvc.service
/lib/systemd/system/captrace.service
/lib/systemd/system/adb.service

