#!/bin/sh -x # # 01. "should check code directly not $?" # shellcheck disable=2181 # # 02. "can't follow . import" # shellcheck disable=1090,1091 # # 03. "possible misspelling" # shellcheck disable=2153 # # 04. "use find .. over ls" # shellcheck disable=2011 # -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-* # =================================================== # 01. Attempt to load global DayZ values # =================================================== echo "Starting, attempting to load globals.sh" GLOBALS_FILE="$PWD/lib/globals.sh" if [ -e "$GLOBALS_FILE" ] ; then chmod +x "$GLOBALS_FILE" . "$GLOBALS_FILE" else die "Could not load: $GLOBALS_FILE" fi # =================================================== # 02. Local script variables # =================================================== # # # Local server file hierarchy: # # c:/DAYZ # ^--/servers # ^--/cache # export ORIGINAL_DIR="$PWD" export LOCAL_DAYZ_FILES="/mnt/c/DAYZ" export CACHE="$LOCAL_DAYZ_FILES/cache" export STOCK_SERVER_CACHE="$CACHE/stock_server" export MOD_CACHE="$CACHE/mods" export SERVERS="$LOCAL_DAYZ_FILES/servers" # (4 hours, in seconds) export RESTART_INTERVAL=$((60 * 60 * 4 + 3)) # =================================================== # 03. init and checks # =================================================== check() { if [ ! -d "$LOCAL_DAYZ_FILES" ] ; then die "dir: $LOCAL_DAYZ_FILES does not exist" fi if [ ! -d "$SERVER_HOME" ] ; then die "dir: $SERVER_HOME does not exist" fi msg "Health checks completed successfully." } init_server() { mkdir -p "$SERVER_HOME" "$CACHE" "$MOD_CACHE" "$STOCK_SERVER_CACHE" :>"$SERVER_HOME/.modstring" check || die "Failed check()" msg "Completed intialization. Using server restart time of $RESTART_INTERVAL seconds." } # =================================================== # 04. Load stock files # =================================================== # load_stock_map_data() { # mkdir -p "$SERVER_HOME/mpmissions/$MAP_FOLDER_NAME" # do_sync --exclude='*storage_1*' \ # "$DAYZ_FILES/stock/maps/$MAP_FOLDER_NAME"/ \ # "$SERVER_HOME/mpmissions/$MAP_FOLDER_NAME" || \ # die "Failed to copy stock map data" # } refresh_local_cache() { msg "Refreshing local stock server cache" if ! islocked_remote_server ; then load_stock_map_data do_sync "$REMOTE_STOCK_SERVER"/ "$STOCK_SERVER_CACHE" unlock_remote_server else msg "Remote server is locked, sleeping..." while islocked_remote_server ; do sleep 15 echo 'Sleeping...' done lock_remote_server do_sync "$REMOTE_STOCK_SERVER"/ "$STOCK_SERVER_CACHE" unlock_remote_server fi msg "Stock server data copied into local server cache!" # ------------------------------------- msg "Refreshing local mods cache" if ! islocked_remote_server ; then lock_remote_server do_sync "$REMOTE_MODS"/ "$MOD_CACHE" unlock_remote_server else msg "Remote server is locked, sleeping..." while islocked_remote_server ; do sleep 15 echo 'Sleeping...' done lock_remote_server do_sync "$REMOTE_MODS"/ "$MOD_CACHE" unlock_remote_server fi msg "Mods data copied into local mod cache!" } setup_environment() { msg "Copying stock server data from local cache into $SERVER_HOME" # list of all stock server folders for folder in addons battleye bliss docs dta keys ; do do_sync "$STOCK_SERVER_CACHE/$folder"/ "$SERVER_HOME/$folder" || die "Failed to copy stock: $folder" done # list of all stock server files for file in ban.txt dayz.gproj dayzsetting.xml whitelist.txt \ DayZServer_x64.exe steam_api64.dll steamclient64.dll \ tier0_s64.dll vstdlib_s64.dll ; do cp -fv "$STOCK_SERVER_CACHE/$file" "$SERVER_HOME/$file" || die "Failed to copy stock: $file" done msg "Copying mod files from local cache into $SERVER_HOME" do_sync "$MOD_CACHE/$MAP_FOLDER_NAME"/ "$SERVER_HOME/mods" || die "Failed to copy mods to $SERVER_HOME" # ~~~~~~~~~~~~~~~~~~~~~~~~~~ TODO: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # msg "Copying DZSA executable" # cp -f "$DAYZ_FILES/software/DZSALModServer.exe" "$SERVER_HOME/" # ~~~~~~~~~~~~~~~~~~~~~~~~~~ TODO: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ } # =================================================== # 05. Start function # =================================================== start_server() { check || die "Failed check()" # wait until dayz-modserver lockfile is released on the NAS if islocked_remote_server ; then msg "Remote file server is currently locked" msg "Sleeping until is no longer locked..." while islocked_remote_server ; do echo "Sleeping..." sleep 10 done msg "Server unlocked! Continuing..." fi cd "$SERVER_HOME" || die "Could not cd to '$SERVER_HOME'" msg "Setting up environment" setup_environment || die "Could not set up environment." } # =================================================== # 06. Main loop # =================================================== main() { cd "$ORIGINAL_DIR" || die "Can no longer cd to $ORIGINAL_DIR" if [ -e ./SERVER.ENV ] ; then . ./SERVER.ENV else die "Could not source ./SERVER.ENV" fi # this must be here and not up top as we must load it from the SERVER.ENV export SERVER_HOME="$SERVERS/$SERVER_NAME" # create directories in $SERVER_HOME # blank the modstring init_server if [ -e ./serverDZ.cfg ] ; then cp -f ./serverDZ.cfg "$SERVER_HOME"/ || die "Failed to copy ./serverDZ into $SERVER_HOME" else die "Could not find ./serverDZ.cfg" fi # sync remote server stock and mod files into local cache refresh_local_cache || die "Failed to refresh local cache." # load stock server, map, mod files into $SERVER_HOME setup_environment || die "Failed to setup environment." start_server "$@" } main "$@" || exit 1