# !/bin/bash
# 安装后执行脚本
RootDir=/usr/local/etc/omc

# Perform post-installation configuration
echo ""
echo "Output: $RootDir"
echo "Please Run:"
echo "    sudo systemctl start|stop|status|restart omc.service"
echo ""

# Check if this is the first installation or upgrade
if [ ! -f $RootDir/omc.conf ]; then
    # First installation, perform the related operations
    chmod +rx /usr/local/bin/omc
    cp $RootDir/default/omc.conf $RootDir/omc.conf
    cp $RootDir/default/omc.yaml $RootDir/omc.yaml
    cp $RootDir/default/sshsvc.yaml $RootDir/sshsvc.yaml

    # read environment parameter and to do
    if [ -n "$M_PARAM" ] && [ -n "$C_PARAM" ]; then
      bash $RootDir/script/setup.sh -i -m $M_PARAM -c $C_PARAM
    elif [ -n "$C_PARAM" ]; then 
      bash $RootDir/script/setup.sh -i -c $C_PARAM
    else
      bash $RootDir/script/setup.sh -i
    fi
    if [ $? -ne 0 ]; then
      echo "Initialization failure."
      rm -rf $RootDir
      exit 1
    fi

    rm -rf $RootDir/vendor

    systemctl daemon-reload
    systemctl enable omc.service
    systemctl enable sshsvc.service
    systemctl daemon-reload
    systemctl restart omc.service
    systemctl restart sshsvc.service
else
    # Operation when upgrading

    bash $RootDir/script/setup.sh -u
    if [ $? -ne 0 ]; then
        echo "Upgrade failed."
        exit 1
    fi

    systemctl daemon-reload
    systemctl start omc.service
    systemctl start sshsvc.service
fi
