#!/bin/bash -e

echo Stopping daemons
snapctl stop --disable solana.daemon-drone
snapctl stop --disable solana.daemon-bootstrap-leader
snapctl stop --disable solana.daemon-fullnode
snapctl stop --disable solana.daemon-oom-monitor
snapctl stop --disable solana.daemon-net-stats

mode="$(snapctl get mode)"
if [[ -z "$mode" ]]; then
  exit 0
fi

skipSetup="$(snapctl get skip-setup)"
if [[ "$skipSetup" != true ]]; then
  numTokens="$(snapctl get num-tokens)"
  numTokens="${numTokens:+-n $numTokens}"

  setupArgs="$(snapctl get setup-args)"
  "$SNAP"/multinode-demo/setup.sh $numTokens -p $setupArgs
else
  echo Setup skipped
fi

case $mode in
bootstrap-leader+drone)
  snapctl start --enable solana.daemon-drone
  snapctl start --enable solana.daemon-bootstrap-leader
  ;;
bootstrap-leader)
  snapctl start --enable solana.daemon-bootstrap-leader
  ;;
fullnode)
  snapctl start --enable solana.daemon-fullnode
  ;;
*)
  echo "Error: Unknown mode: $mode"
  exit 1
  ;;
esac

snapctl start --enable solana.daemon-oom-monitor
snapctl start --enable solana.daemon-net-stats
