#!/bin/bash

# ============================================
# Codono Exchange - Cron Job Setup
# Automated background job scheduling
# ============================================

set -e

# Define the path to the credentials file for codono
CREDENTIALS_FILE="/opt/credentials.yml"

# Check if credentials file exists
if [ ! -f "$CREDENTIALS_FILE" ]; then
    echo "Credentials file does not exist: $CREDENTIALS_FILE"
    exit 1
fi

# Extract values from the credentials file
DOMAIN=$(grep '^DOMAIN:' "$CREDENTIALS_FILE" | awk '{print $2}')
PHP_PATH='/usr/bin/php7.4'
CRON_KEY=$(grep 'CRON_KEY:' /opt/credentials.yml | cut -d ' ' -f 2)

# Check if DOMAIN was found
if [ -z "$DOMAIN" ]; then
    echo "DOMAIN not found in the credentials file."
    exit 1
fi

# Define groups of cron jobs
declare -A CRON_JOB_GROUPS
CRON_JOB_GROUPS[1]="0" # BTC Deposit
CRON_JOB_GROUPS[2]="1" # CryptoApis Deposit
CRON_JOB_GROUPS[3]="2 3" # Substrate Deposit
CRON_JOB_GROUPS[4]="4 5" # Blockgum Deposit and Withdrawal ID
CRON_JOB_GROUPS[5]="6 7" # Cryptonote
CRON_JOB_GROUPS[6]="11 12" # Coinpayments

# Define the user interface
echo "We will add some mandatory crons automatically for Emails, Charts, Tron, etc."
echo "Select cron jobs to enable: "
echo "1) BTC Deposit"
echo "2) CryptoApis Deposit"
echo "3) Substrate"
echo "4) Blockgum"
echo "5) Cryptonote"
echo "6) Coinpayments"

# Don't let an empty stdin (piped/non-interactive) trip set -e
read -p "Enter numbers separated by spaces (e.g., 1 2 3): " selection || selection=""

# Define the original path (API domain)
INDEX_PATH="/data/wwwroot/${DOMAIN}"

# Define the cron jobs
CRON_JOBS=(
"* * * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Coin/deposit_btctype/securecode/${CRON_KEY}/ > /dev/null"
"* * * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Coin/deposit_cryptoapis/securecode/${CRON_KEY}/ > /dev/null"
"* * * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Coin/substrate_deposit/securecode/${CRON_KEY}/ > /dev/null"
"*/5 * * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Coin/getWithdrawalIdSubstrate/securecode/${CRON_KEY}/ > /dev/null"
"* * * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Coin/blockgum_deposit/securecode/${CRON_KEY}/ > /dev/null"
"*/5 * * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Coin/getWithdrawalIdBlockgum/securecode/${CRON_KEY}/ > /dev/null"
"* * * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Coin/wallet_cryptonote_deposit/securecode/${CRON_KEY}/ > /dev/null"
"* * * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Coin/wallet_cryptonote2_deposit/securecode/${CRON_KEY}/ > /dev/null"
"*/5 * * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Coin/wallet_blockio_deposit/securecode/${CRON_KEY}/ > /dev/null"
"*/5 * * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Coin/wallet_blockio_withdraw/securecode/${CRON_KEY}/ > /dev/null"
"* * * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Coin/esmart_deposit/securecode/${CRON_KEY}/chain/chainNameHere > /dev/null"
"*/5 * * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Coin/wallet_coinpay_deposit/securecode/${CRON_KEY}/ > /dev/null"
"*/5 * * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Coin/wallet_coinpay_withdraw/securecode/${CRON_KEY}/ > /dev/null"
)

# Mandatory cron jobs (always added)
MANDATORY_CRON_JOBS=(
"* * * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Tron/cronDeposits/securecode/${CRON_KEY}/ > /dev/null"
"* * * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Xtrade/cronMe/securecode/${CRON_KEY}/ > /dev/null"
"* * * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Xtrade/otcTrade/securecode/${CRON_KEY}/ > /dev/null"
"* * * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Selfengine/CreateOrderbook/securecode/${CRON_KEY}/ > /dev/null"
"0 9 * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Selfengine/cleanUp/securecode/${CRON_KEY}/ > /dev/null"
"0 9 * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Queue/checkStaking/securecode/${CRON_KEY}/ > /dev/null"
"* * * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Queue/BinanceUpdate/securecode/${CRON_KEY}/ > /dev/null"
"* * * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Queue/ExchangeBinanceUpdate/securecode/${CRON_KEY}/ > /dev/null"
"* * * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Queue/cmcUpdate/securecode/${CRON_KEY}/ > /dev/null"
"* * * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Queue/cmcUpdateRate/securecode/${CRON_KEY}/ > /dev/null"
"* * * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Queue/send_notifications/securecode/${CRON_KEY}/ > /dev/null"
"*/10 * * * * cd ${INDEX_PATH} && ${PHP_PATH} index.php Queue/genInternalCharts/securecode/${CRON_KEY}/ > /dev/null"
)

# Start with mandatory cron jobs
CRON_JOBS_TO_ADD=("${MANDATORY_CRON_JOBS[@]}")

# Process optional selections (empty input is OK — mandatory crons still get installed)
if [ -n "$selection" ]; then
    for i in $selection; do
        if ! [[ "$i" =~ ^[1-6]$ ]]; then
            echo "Invalid selection: $i. Please enter valid numbers (1-6)."
            exit 1
        fi

        # Get the corresponding group of cron jobs for the selection
        if [[ -n ${CRON_JOB_GROUPS[$i]} ]]; then
            for index in ${CRON_JOB_GROUPS[$i]}; do
                CRON_JOBS_TO_ADD+=("${CRON_JOBS[$index]}")
            done
        else
            echo "Selection $i is out of range."
            exit 1
        fi
    done
else
    echo "No optional crons selected. Installing mandatory crons only."
fi

# Use command substitution rather than a pipeline so set -e doesn't trip when
# grep returns 1 on no matches or crontab returns 1 on no existing crontab.
# (Bash 5's pipeline/||true interaction caused this to silently kill the script.)
EXISTING_CRONS="$(crontab -u www-data -l 2>/dev/null | grep -v "${INDEX_PATH}" || true)"

CRON_TMP_FILE=$(mktemp)
echo "Temporary file created at: $CRON_TMP_FILE"

# Save a backup copy of the prior crontab (best-effort)
[ -n "$EXISTING_CRONS" ] && echo "$EXISTING_CRONS" > /tmp/mycron.backup.$(date +%s)

# Seed the temp file with preserved existing entries
[ -n "$EXISTING_CRONS" ] && printf '%s\n' "$EXISTING_CRONS" > "$CRON_TMP_FILE"

# Add new cron jobs to the temporary file
for job in "${CRON_JOBS_TO_ADD[@]}"; do
    echo "$job" >> "$CRON_TMP_FILE"
done

# Install the new cron jobs
crontab -u www-data "$CRON_TMP_FILE"
rm "$CRON_TMP_FILE"

# Reload the cron service to apply changes
if command -v systemctl &> /dev/null; then
    echo "Reloading cron service using systemctl..."
    systemctl reload cron
elif command -v service &> /dev/null; then
    echo "Reloading cron service using service command..."
    service cron reload
else
    echo "Cron service will automatically detect the changes. No manual reload required."
fi

echo "Cron jobs have been updated and reloaded. You can check crons using: crontab -l -u www-data"
