#!/usr/bin/env bash # How many seconds to sleep before starting the server again, unless it is explicitly specified DEFAULT_SLEEP_SECONDS=3 declare -A DEV DEV[option]="dev" DEV[screen]="steptech" DEV[dir]=".." # Which paper servers can be started with this script SERVERS=( DEV ) # Which configuration to apply to the paper servers CONFIG="proxy" # Velocity function show_help { local cmd=$0 echo "Create a start loop for the proxy server and optionally start a number of paper servers with it" echo "" echo "Usage: $cmd [OPTIONS]" echo "" echo "OPTIONS:" { local fmt=" %s\t%s\t%s\n" printf "$fmt" "-s" "--sleep" "How many seconds to sleep after stopping the server" printf "$fmt" "-n" "--no-loop" "Don't restart the proxy server once it stopped" for server_name in "${SERVERS[@]}"; do declare -n server="$server_name" printf "$fmt" "" "--${server[option]}" "Start the ${server[screen]} server (${server[dir]})" done printf "$fmt" "-h" "--help" "Show this message" } | column -t -s $'\t' exit } function main { local sleep_seconds=$DEFAULT_SLEEP_SECONDS local help=false local loop=true local servers=() # Parse arguments local cmd=$0 while [[ $# -gt 0 ]]; do local arg=$1 case "$arg" in "--sleep" | "-s") sleep_seconds=$2 shift 2 ;; "--no-loop" | "-n") loop=false shift ;; "--help" | "-h") help=true shift ;; *) local option_found=false for server_name in "${SERVERS[@]}"; do declare -n server="$server_name" if [ "$arg" = "--${server[option]}" ]; then servers+=("$server_name") option_found=true fi done if ! $option_found; then err "Unknown argument: $arg" fi shift ;; esac done if $help; then show_help "$cmd" fi # Start servers for server_name in "${servers[@]}"; do declare -n server="$server_name" # Create server screen if it doesn't exist yet local screen="${server[screen]}" screen -ls | grep -q "$screen" || screen -dmS "$screen" # Start the server screen -S "$screen" -X stuff "cd $(pwd)/${server[dir]}; ./start.sh --no-loop --config \"$CONFIG\" --tmp\n" echo "Started paper server in screen $screen" done function cleanup { for server_name in "${servers[@]}"; do declare -n server="$server_name" local screen="${server[screen]}" screen -S "$screen" -X stuff "stop\n" echo "Stopped paper server in screen $screen" done exit } trap cleanup SIGINT while :; do echo "Starting proxy..." ./launch.sh $loop || break echo "Sleeping for $sleep_seconds seconds..." sleep "$sleep_seconds" done cleanup } # Shows an error message and exits the program with code 1 # # Usage: err function err { echo "$1" exit 1 } main "$@"