#!/bin/sh -ex
#
# values used on both the modserver and individual servers
# ------------------------------------------------------------

################### GLOBAL VARS ##############################
export DAYZ_GAME_ID=221100
export DAYZ_SERVER_ID=223350
export STEAM_USER="yzaddayz"

export DEBUG=false

export REMOTE_DAYZ_TRUENAS_SHARE='\\dayz-truenas.local.wvr.sh\dayz'
export REMOTE_DIR="/mnt/dayz"
export REMOTE_SERVER_LOCKFILE="$REMOTE_DIR/lockfile"
export REMOTE_MAPS="$REMOTE_DIR/stock/maps"
export REMOTE_MODS="$REMOTE_DIR/mods"

export REMOTE_STOCK_SERVER="$REMOTE_DIR/stock/DayZServer"
export REMOTE_STOCK_GAME="$REMOTE_DIR/stock/DayZ"

export CHERNARUS_MAP_NAME=dayzOffline.chernarusplus
export NAMALSK_MAP_NAME=regular.namalsk
export TAKISTAN_MAP_NAME=Offline.TakistanPlus


# NAMALSK_SURVIVAL is what the terrain/map comes with
export NAMALSK_SURVIVAL_MOD_ID=2289461232
export NAMALSK_ISLAND_MOD_ID=2289456201
export TAKISTAN_MOD_ID=2563233742

################### GLOBAL VARS ##############################

################### GLOBAL ALIASES ###########################
# note: OLD? MAYBE NEEDED?
###########alias do_sync='rsync -rvltDhu --delete --partial --progress'
unalias rsync 2>/dev/null ||:
alias do_sync='rsync -avhu --progress --delete --partial --links'
################### GLOBAL ALIASES ###########################

################### GLOBAL FUNCTIONS #########################
msg() {
    printf '\n[*] %s\n\n' "$*"
}
errmsg() {
    >&2 printf '\n[Error]: %s\n\n' "$*"
}
die() {
    errmsg "$*"
    exit 1
}
debug() {
    if [ "$DEBUG" ] ; then
        >&2 printf '>>>>>>>>>DEBUG: %s\n' "$*"
    fi
}
isempty() {
    for arg in "$@" ; do
        # exit in error if file
        if [ -f "$arg" ] ; then
            exit 1
        elif [ -L "$arg" ] ; then
            # if is a symlink, check what it points to
            # note: this only checks one-link level deep
            if [ -d "$(readlink "$arg")" ] ; then
                exit 0
            else
                exit 1
            fi
        elif [ -d "$arg" ] ; then
            :
        else
            # is not a directory or a link to a directory
            exit 1
        fi

        # there should be a way to make this be builtins only
        # using printf and glob expansions, look into later
        if /bin/rmdir "$arg" 2>/dev/null ; then
            /bin/mkdir -p "$arg"
            exit 0
        else
            exit 1
        fi
    done
}
mount_dayz_network_drive() {
    if ! is_network_drive_mounted ; then
        sudo mkdir -p /mnt/dayz
        if [ -e /proc/version ] ; then
            read -r ver < /proc/version
            case $ver in
                # WSL
                *icrosoft*)
                    msg "Detected WSL, mounting via drvfs"
                    sudo mount -t drvfs -o user=samba \
                        "$REMOTE_DAYZ_TRUENAS_SHARE" /mnt/dayz
                    ;;
                *)
                    msg "Detected Linux, mounting via cifs"
                    # shellcheck disable=2046
                    sudo mount -t cifs "$REMOTE_DAYZ_TRUENAS_SHARE" /mnt/dayz \
                        -o rw \
                        -o credentials=/root/sambacreds.txt \
                        -o iocharset=utf8 \
                        -o vers=3.0 \
                        -o uid=$(id -u) \
                        -o gid=$(id -g)
            esac

            unset ver
        else
            errmsg 'could not find /proc/version'
            exit 1
        fi

    fi
    if ! is_network_drive_mounted ; then
        errmsg 'Could not mount network drive'
        exit 1
    fi
}
is_network_drive_mounted() {
    if [ ! -f "$REMOTE_DIR/canary" ] ; then
        return 1
    fi
    # test write
    touch /mnt/dayz/canary.test
    if [ $? -eq 0 ] ; then
        rm /mnt/dayz/canary.test
    else
        errmsg "Mounted network drive but we do not have write access."
        exit 1
    fi
}
# =====================================================================================
islocked_remote_server() {
    if [ -e "$REMOTE_SERVER_LOCKFILE" ] ; then
        # msg "Server is not currently locked!"
        return 0
    else
        return 1
    fi
}
lock_remote_server() {
    msg "Locking remote server."
    if ! echo 'locked' | tee "$REMOTE_SERVER_LOCKFILE" ; then
        die "Could not create: $REMOTE_SERVER_LOCKFILE" 
    fi
}

unlock_remote_server() {
    msg "Unlocking remote server."
    rm -f "$REMOTE_SERVER_LOCKFILE" || die "Could not remove: $REMOTE_SERVER_LOCKFILE"
}
# =====================================================================================
