#!/bin/bash

ProcList="restagent crontask sshsvc"
GitLocalRoot=$HOME/local.git
EmsBEDir=$GitLocalRoot/ems.agt
EmsBuildRoot=$GitLocalRoot/build.ems
EmsFEDir=$GitLocalRoot/fe.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

chmod +x ${DumpToolDir}/dumpdb.sh
${DumpToolDir}/dumpdb.sh

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