#!/usr/bin/env zsh
export NVM_DIR="$HOME/.nvm"
source "$NVM_DIR/nvm.sh"
nvm use v24.13.0

# start all mempool backends that exist
for site in mainnet mainnet-lightning testnet testnet-lightning testnet4 signet signet-lightning liquid liquidtestnet;do
    [ ! -e "${HOME}/${site}/backend/" ] && continue
    cd "${HOME}/${site}/backend/" && \
    echo "starting mempool backend: ${site}" && \
    screen -dmS "${site}" sh -c 'while true;do npm run start-production;sleep 1;done'
done

# start SSR for services
if [ -e "${HOME}/services" ];then
    cd "${HOME}/services/ssr" && \
    echo "starting SSR daemon: services" && \
    screen -dmS "ssr" sh -c 'while true;do npm run start;sleep 2;done'
fi

# start nginx warm cacher
for site in mainnet;do
    echo "starting mempool cache warmer: ${site}"
    screen -dmS "warmer-${site}" $HOME/mempool/production/nginx-cache-warmer
done

# start SSR cache warmer
for site in mainnet;do
    echo "starting SSR cache warmer: ${site}"
    screen -dmS "warmer-ssr-${site}" $HOME/mempool/production/nginx-cache-warmer-ssr
done

# start nginx hot cacher
for site in mainnet;do
    echo "starting mempool cache heater: ${site}"
    screen -dmS "heater-${site}" $HOME/mempool/production/nginx-cache-heater
done

exit 0
