#!/bin/bash

# Perform the two stages of MPC export if the export flag file is present.
#
# This script performs the local processing mode of MPC export. Only DOs
# present in the EON queue will be processed for export.
#
# Stage 1 Determine exportable DOs and create revision records and set them as pending for export.
# Stage 2 Export detections and orbits associated with pending revision records.
#
# Usage
#
#     mpc-export-eon

export PERL5LIB=/usr/local/MOPS/mpc-export/lib/perl5:$PERL5LIB
export PATH=/usr/local/MOPS/mpc-export/bin:$PATH
export MPC_EXPORT_BIN=/usr/local/MOPS/mpc-export/bin

EXPORT_IS_SET=`get-mpc-export-flag.pl $MOPS_DBINSTANCE`;

# Allow debugging of global processing on a per night basis.
#
# MOPS config:
# mpc_export {
#     global_mode = 1
# }
GLOBAL_IS_SET=`get-global-flag.pl $MOPS_DBINSTANCE`;

echo "DB Instance is $MOPS_DBINSTANCE."

if [ $EXPORT_IS_SET == 1 ]; then
    if [ $GLOBAL_IS_SET == 1 ]; then
        echo "Performing MPC export (Global) on database $MOPS_DBINSTANCE."
        $MPC_EXPORT_BIN/process-dos-all.pl --noprompt $MOPS_DBINSTANCE
        $MPC_EXPORT_BIN/export-pending-mops-objects-to-mpc-datastore.pl --noprompt $MOPS_DBINSTANCE       
    else
        echo "Performing MPC export (EON) on database $MOPS_DBINSTANCE."

        #advance-night-number.pl
    
        $MPC_EXPORT_BIN/process-dos-eon.pl --noprompt $MOPS_DBINSTANCE
        $MPC_EXPORT_BIN/export-pending-mops-objects-to-mpc-datastore.pl --noprompt $MOPS_DBINSTANCE
    fi
else
    echo "Data will NOT be exported to the MPC datastore."
fi
