#!/bin/bash

ProcList="restagent crontask sshsvc captrace data2html"
ProjectL=omc
VERSION=2.2403.1
RelDate=`date +%Y%m%d`
Release=${RelDate}
RelVer=${VERSION}-${RelDate}
Ky10Arch=ky10.aarch64
RpmPkgName=${ProjectL}-${RelVer}.ky10.aarch64.rpm
RpmPkgRename=${ProjectL}-r${RelVer}-ky10.rpm
DebPkgName=${ProjectL}-r${RelVer}-ub*.deb
GitLocalRoot=${HOME}/local.git
EmsBEDir=${GitLocalRoot}/nms_nbi
EmsBuildRoot=${GitLocalRoot}/build.ems
RpmArch=`arch`
RpmsDir=${EmsBuildRoot}/rpmbuild/RPMS/
ReleaseDir=${EmsBuildRoot}/release
DumpToolDir=${EmsBEDir}/tools/misc

if [[ ${RpmArch} =~ "x86_64" ]];then
    RelArch=amd64
elif [[ $RpmArch =~ "aarch64" ]];then
    RelArch=arm64
fi

cd $EmsBEDir
for procName in $ProcList;do
    cd $EmsBEDir/$procName
    echo "Make $procName ..."
    make
done

#Get SQL from database
if [ "$2" = "db" ]; then
    cd ${DumpToolDir}
    chmod +x ${DumpToolDir}/dumpdb.sh
    ${DumpToolDir}/dumpdb.sh
fi

case "$1" in
    rpm)
        cd $EmsBEDir
        make rpm
        mv -f $RpmsDir/$RpmArch/$RpmPkgName $ReleaseDir/"$1"s/$RelArch/$RpmPkgRename
        cd $ReleaseDir/"$1"s/$RelArch
        rm -f omc-md5sum.txt
#        rpm --addsign *.rpm
        md5sum $RpmPkgRename >omc-md5sum.txt
    ;;
    deb)
        cd $EmsBEDir
        make deb
        cd $ReleaseDir/"$1"s/$RelArch
        rm -f omc-md5sum.txt
#        rpm --addsign *.rpm
        md5sum $DebPkgName >omc-md5sum.txt        
    ;;
    *)
        echo "mkpkg"
        echo "Usage: $0 rpm|deb"
    ;;
esac  