#!/bin/bash

deb_build_dir=../debbuild
src_pkg_root=../pkg

release_root=../release
release_date=`date +%Y%m%d`

src_ocs_dir=../../ocs
ocs_bin_dir=$src_ocs_dir/bin
ocs_bin_file=agtocs

src_restproxy_root=../../selfcare
src_cproxy_dir=$src_restproxy_root/proxy_c
src_goproxy_dir=$src_restproxy_root/proxy
proxy_bin_dir=$src_restproxy_root/bin
cproxy_bin_file=restproxy
goproxy_bin_file=restproxy_go

usage() {
    echo "Usage: $0 [ocs|scp|ussdgw|cproxy|goproxy] [Option] "
    echo 
    echo "Build OCS package, option as follow:"
    echo "  ocs|scp|ussdgw,                     ocs: OCS package build by dpkg"
    echo "                                      scp: SCP package build by dpkg"
    echo "                                      ussdgw: USSD Gateway package, build by dpkg"
    echo
    echo "  Option:"
    echo "      -t [norfolk|cook|pncc|zed]      Build binary tag, only for ocs, default is pncc"
    echo "      -h, --help                      Display this help and exit"
    echo "  cproxy|goproxy,                     cproxy: C RestProxy package build by dpkg"
    echo "                                      goproxy: Golang RestProxy package build by dpkg" 
    echo
    echo "Example:"
    echo "  $0 ocs -t pncc"
    echo "  $0 scp -t zed "
    echo "  $0 cproxy"
    echo "  $0 goproxy"
    exit 1
}

pkg_type=""
new_args=()
# Traverse all parameters
for arg in "$@"; do
    if [[ "$arg" == "ocs" || "$arg" == "scp" || "$arg" == "ussdgw" || "$arg" == "cproxy" || "$arg" == "goproxy" ]]; then
        pkg_type=$arg
    else
        new_args+=("$arg") # Add non pkg_type parameters to a new parameter list
    fi  
done

if [ -z "$pkg_type" ]; then
    usage
    exit 1    
fi

# Use a new parameter list
set -- "${new_args[@]}"

while getopts "t:h" option; do
    case $option in
        t)
            bin_tag=$OPTARG
            ;;
        h)
            usage
            ;;
        \?)
            echo "Invalid option: -$OPTARG" >&2
            usage
            ;;
    esac
done

# Get the output of uname -a
uname_a=$(uname -a)
# Extract hardware architecture
if [[ $uname_a =~ "x86_64" ]]; then
    deb_arch=amd64
    rpm_arch=x86_64
    rel_arch=amd64
elif [[ $uname_a =~ "aarch64" ]]; then
    deb_arch=arm64
    rpm_arch=aarch64
    rel_arch=arm64
    echo "ARM64 is not supported"
    exit 1
else
    echo "Unsupported hardware architecture"
    exit 1
fi

get_os_info() {
    if [ -f /etc/os-release ]; then
        . /etc/os-release
        echo $ID
    else
        echo "unknown"
    fi
}

get_os_version() {
    if [ -f /etc/os-release ]; then
        . /etc/os-release
        echo $VERSION_ID
    else
        echo "unknown"
    fi
}

os_version=$(get_os_version)
case $(get_os_info) in
    ubuntu|debian)
        if [[ "$os_version" == "22.04" ]]; then
            rel_os=ub22
        elif [[ "$os_version" == "12.04" ]]; then
            rel_os=ub12         
        else
            echo "Unsupported Ubuntu version: $os_version"
            exit 1
        fi
        pkg_ext=deb
        ;;
    centos|rhel)
        rel_os=ct8
        pkg_ext=rpm
        echo "CentOS/RHEL is not supported"
        exit 1
        ;;
    openEuler)
        rel_os=oe20
        pkg_ext=rpm
        echo "openEuler is not supported"
        exit 1
        ;;
    *)
        echo "Unsupported OS"
        exit 1
        ;;
esac

make_ocs_bin()
{
    if [ -z $bin_tag ]; then
        bin_tag="pncc"
    fi

    # 如果是build cook版本且当前为Ubuntu22，则调用Ubuntu 12.04容器打包
    if [ "$bin_tag" = "cook" ] && [ "$rel_os" = "ub22" ]; then
        echo "Detected cook build on Ubuntu 22.04, switching to Ubuntu 12.04 container environment."
        make_cook_ocs_bin_in_ub12
        exit $?
    fi

    cd $src_ocs_dir    
    if [ $bin_tag = "cook" ]; then
        git checkout cook
        git pull
    else
        git checkout main
        git pull
    fi

    echo -n "make clean ... "
    make clean 1>/dev/null 2>&1
    echo "done"
    echo -n "make $bin_tag $pkg_type ... "
    make ${bin_tag} 1>make.log 2>&1
    if [ $? -ne 0 ]; then
        echo "failed"
        echo "Please refer to make.log for details"
        exit 1
    fi 
    echo "done"
    cd $OLDPWD
}

make_cook_ocs_bin_in_ub12()
{
    if [ "$(docker ps -a -f name=ubuntu12-dev -f status=exited -q)" ]; then
        docker start ubuntu12-dev
    fi
    docker exec -it ubuntu12-dev /bin/bash -c "cd ~/ocs.git/build/bin && ./build.sh ocs -t cook"
}

make_cproxy_bin()
{
    cd $src_cproxy_dir
    echo -n "make clean ... "
    make clean 1>/dev/null 2>&1
    echo "done"
    echo -n "configure third lib ... "  
    make 3rdlib_config 1>make.log 2>&1
    if [ $? -ne 0 ]; then
        echo "failed"
        echo "Please refer to make.log for details"
        exit 1
    fi  
    echo "done"
    echo -n "make c restproxy ... "
    make 1>make.log 2>&1
    if [ $? -ne 0 ]; then
        echo "failed"
        echo "Please refer to make.log for details"
        exit 1
    fi 
    echo "done"
    cd $OLDPWD
}

make_goproxy_bin()
{
    cd $src_goproxy_dir
    echo -n "make clean ... "
    make clean 1>/dev/null 2>&1
    echo "done"
    echo -n "make golang restproxy ... "
    make build 1>make.log 2>&1
    if [ $? -ne 0 ]; then
        echo "failed"
        echo "Please refer to make.log for details"
        exit 1
    fi 
    echo "done"
    cd $OLDPWD
}

pre_common_build()
{
    test -d $deb_build_dir || mkdir -p $deb_build_dir
    cd $deb_build_dir
    current_dir=`pwd`
    if [[ "$current_dir" =~ "debbuild" ]]; then
        rm -rf *
    fi
    
    mkdir -p $deb_build_dir/DEBIAN
    cd $OLDPWD
}

pre_ocs_build()
{
    mkdir -p $deb_build_dir/usr/local/subsManage
    mkdir -p $deb_build_dir/usr/local/backup
    mkdir -p $deb_build_dir/usr/local/ocs/bin/conf/default
    mkdir -p $deb_build_dir/usr/local/ocs/log
    mkdir -p $deb_build_dir/usr/local/ocs/tools
    mkdir -p $deb_build_dir/lib/systemd/system
    mkdir -p $deb_build_dir/usr/local/agt/etc/tariff

    release_dir=$release_root/ocs/$rel_arch
    mkdir -p $release_dir
}

pre_scp_build()
{
    mkdir -p $deb_build_dir/usr/local/subsManage
    mkdir -p $deb_build_dir/usr/local/backup
    mkdir -p $deb_build_dir/usr/local/scp/bin/conf/default
    mkdir -p $deb_build_dir/usr/local/scp/log
    mkdir -p $deb_build_dir/usr/local/scp/tools
    mkdir -p $deb_build_dir/lib/systemd/system

    release_dir=$release_root/scp/$rel_arch
    mkdir -p $release_dir
}

pre_ussdgw_build()
{
    mkdir -p $deb_build_dir/usr/local/subsManage
    mkdir -p $deb_build_dir/usr/local/backup
    mkdir -p $deb_build_dir/usr/local/ussdgw/bin/conf/default
    mkdir -p $deb_build_dir/usr/local/ussdgw/log
    mkdir -p $deb_build_dir/usr/local/ussdgw/tools
    mkdir -p $deb_build_dir/lib/systemd/system

    release_dir=$release_root/ussdgw/$rel_arch
    mkdir -p $release_dir
}

pre_cproxy_build()
{
    mkdir -p $deb_build_dir/usr/local/subsManage
    mkdir -p $deb_build_dir/usr/local/backup
    mkdir -p $deb_build_dir/usr/local/restproxy/bin
    mkdir -p $deb_build_dir/usr/local/restproxy/conf/default
    mkdir -p $deb_build_dir/usr/local/restproxy/log
    mkdir -p $deb_build_dir/lib/systemd/system

    release_dir=$release_root/cproxy/$rel_arch
    mkdir -p $release_dir
}

pre_goproxy_build()
{
    mkdir -p $deb_build_dir/usr/local/subsManage
    mkdir -p $deb_build_dir/usr/local/backup
    mkdir -p $deb_build_dir/usr/local/restproxy/bin
    mkdir -p $deb_build_dir/usr/local/restproxy/etc/default
    mkdir -p $deb_build_dir/usr/local/restproxy/log
    mkdir -p $deb_build_dir/lib/systemd/system

    release_dir=$release_root/goproxy/$rel_arch
    mkdir -p $release_dir
}

case $pkg_type in
    ocs)
        pre_common_build
        pre_ocs_build
        make_ocs_bin
        project_name=ocs
        src_pkg_dir=$src_pkg_root/ocs
        

        cp $ocs_bin_dir/$ocs_bin_file $deb_build_dir/usr/local/ocs/bin/ocs
        cp -rf $src_pkg_dir/conf/* $deb_build_dir/usr/local/ocs/bin/conf/default
        cp -rf $src_pkg_root/tools/* $deb_build_dir/usr/local/ocs/tools        
        cp $src_pkg_dir/systemd/ocs.service $deb_build_dir/lib/systemd/system

        cp $src_pkg_dir/DEBIAN/* $deb_build_dir/DEBIAN
        sed -i "s/YYYYMMDD/${release_date}/g" $deb_build_dir/DEBIAN/control
        release_ver=$(grep '^Version:' ${deb_build_dir}/DEBIAN/control | awk '{print $2}')
        deb_file_name=${project_name}-r${release_ver}-${rel_os}.${pkg_ext}
    
        fakeroot dpkg-deb --build $deb_build_dir $release_dir/$deb_file_name
        ;;
    scp)
        pre_common_build
        pre_scp_build
        make_ocs_bin
        project_name=scp
        src_pkg_dir=$src_pkg_root/scp
        
        cp $ocs_bin_dir/$ocs_bin_file $deb_build_dir/usr/local/scp/bin/scp
        cp -rf $src_pkg_dir/conf/* $deb_build_dir/usr/local/scp/bin/conf/default
        cp -rf $src_pkg_root/tools/* $deb_build_dir/usr/local/scp/tools
        cp $src_pkg_dir/systemd/scp.service $deb_build_dir/lib/systemd/system

        cp $src_pkg_dir/DEBIAN/* $deb_build_dir/DEBIAN
        sed -i "s/YYYYMMDD/${release_date}/g" $deb_build_dir/DEBIAN/control
        release_ver=$(grep '^Version:' ${deb_build_dir}/DEBIAN/control | awk '{print $2}')
        deb_file_name=${project_name}-r${release_ver}-${rel_os}.${pkg_ext}

        fakeroot dpkg-deb --build $deb_build_dir $release_dir/$deb_file_name
        ;;
    ussdgw)
        pre_common_build
        pre_ussdgw_build
        make_ocs_bin
        project_name=ussdgw
        src_pkg_dir=$src_pkg_root/ussdgw

        cp $ocs_bin_dir/$ocs_bin_file $deb_build_dir/usr/local/ussdgw/bin/ussdgw
        cp -rf $src_pkg_dir/conf/* $deb_build_dir/usr/local/ussdgw/bin/conf/default
        cp -rf $src_pkg_root/tools/* $deb_build_dir/usr/local/ussdgw/tools
        cp $src_pkg_dir/systemd/ussdgw.service $deb_build_dir/lib/systemd/system

        cp $src_pkg_dir/DEBIAN/* $deb_build_dir/DEBIAN
        sed -i "s/YYYYMMDD/${release_date}/g" $deb_build_dir/DEBIAN/control
        release_ver=$(grep '^Version:' ${deb_build_dir}/DEBIAN/control | awk '{print $2}')
        deb_file_name=${project_name}-r${release_ver}-${rel_os}.${pkg_ext}

        fakeroot dpkg-deb --build $deb_build_dir $release_dir/$deb_file_name
        ;;
    cproxy)
        pre_common_build
        pre_cproxy_build
        make_cproxy_bin
        project_name=restproxy
        src_pkg_dir=$src_pkg_root/cproxy

        cp $proxy_bin_dir/$cproxy_bin_file $deb_build_dir/usr/local/restproxy/bin/restproxy
        cp -rf $src_pkg_dir/conf/* $deb_build_dir/usr/local/restproxy/conf/default
        cp $src_pkg_dir/systemd/restproxy.service $deb_build_dir/lib/systemd/system

        cp $src_pkg_dir/DEBIAN/* $deb_build_dir/DEBIAN
        sed -i "s/YYYYMMDD/${release_date}/g" $deb_build_dir/DEBIAN/control
        release_ver=$(grep '^Version:' ${deb_build_dir}/DEBIAN/control | awk '{print $2}')
        deb_file_name=${project_name}-r${release_ver}-${rel_os}.${pkg_ext}

        fakeroot dpkg-deb --build $deb_build_dir $release_dir/$deb_file_name
        ;;
    goproxy)
        if [ $rel_os == "ub12" ]; then
            echo "Ubuntu 12.04 is not supported"
            exit 1
        fi
        pre_common_build
        pre_goproxy_build
        make_goproxy_bin
        project_name=restproxy
        src_pkg_dir=$src_pkg_root/goproxy

        cp $proxy_bin_dir/$goproxy_bin_file $deb_build_dir/usr/local/restproxy/bin/restproxy
        cp -rf $src_pkg_dir/conf/* $deb_build_dir/usr/local/restproxy/etc/default
        cp $src_pkg_dir/systemd/restproxy.service $deb_build_dir/lib/systemd/system

        cp $src_pkg_dir/DEBIAN/* $deb_build_dir/DEBIAN
        sed -i "s/YYYYMMDD/${release_date}/g" $deb_build_dir/DEBIAN/control
        release_ver=$(grep '^Version:' ${deb_build_dir}/DEBIAN/control | awk '{print $2}')
        deb_file_name=${project_name}-r${release_ver}-${rel_os}.${pkg_ext}

        fakeroot dpkg-deb --build $deb_build_dir $release_dir/$deb_file_name
        ;;                 
    *)
        usage
        ;;
esac