#!/bin/bash # # Function use in preparation & configuration # # Copyright (C) 2017 AleaJactaEst # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . ###################### # Write output ###################### declare -i DEBUG=0 function msg_debug() { if [[ $DEBUG -ne 0 ]] then echo "$(date "+%Y/%m/%d %H:%M:%S") DEBUG - $*" fi } function msg_info() { echo "$(date "+%Y/%m/%d %H:%M:%S") INFO - $*" } function msg_error() { echo "$(date "+%Y/%m/%d %H:%M:%S") ERROR - $*" >&2 } function set_debug() { if [[ -n "$1" ]] then DEBUG=$1 else DEBUG=0 fi } ###################### # function to change owner for gameserver ###################### function chown_gameserver() { # gameserver:$(id -g -n gameserver) chown $UIDGAMESERVER:$GIDGAMESERVER "$1" || exit 2 } ###################### # function duplicate/link file ###################### function copy_link() { if [[ -z "$1" ]] then msg_error "Missing 1st argument (source)" exit 2 fi if [ ! -f "$1" ] then msg_error "Missing source : '$1'" exit 2 fi mv "$1" "$1.old" || exit 2 cp "$1.old" "$1" || exit 2 chown_gameserver "$1" || exit 2 } function create_link() { if [[ -z "$1" ]] then msg_error "Missing 1st argument (source)" exit 2 fi if [[ -z "$2" ]] then msg_error "Missing 2nd argument (destination)" exit 2 fi filename_l=$(basename "$1") msg_debug "create link $2/$filename_l" if [ -e "$2/$filename_l" ] then rm "$2/$filename_l" || exit 2 elif [ -h "$2/$filename_l" ] then rm "$2/$filename_l" || exit 2 fi if [[ (! -e "$1") && (! -h "$1") ]] then msg_error "Missing source : '$1'" exit 2 fi ln -s "$1" "$2/$filename_l" || exit 2 chown --no-dereference $UIDGAMESERVER:$GIDGAMESERVER "$2/$filename_l" || exit 2 } function create_recursive_link() { if [[ -z "$1" ]] then msg_error "Missing 1st argument (source)" exit 2 fi if [[ -z "$2" ]] then msg_error "Missing 2nd argument (destination)" exit 2 fi msg_debug "link $1 -> $2" for file in $1/* do if [ -d "$file" ] then filename=$(basename "$file") msg_debug "create dir $2/$filename" if [ ! -f "$2/$filename" ] then mkdir -p "$2/$filename" || exit 2 fi chown $UIDGAMESERVER:$GIDGAMESERVER "$2/$filename" || exit 2 create_recursive_link "$file" "$2/$filename" || exit 2 else create_link "$file" "$2" || exit 2 fi done } function create_link_2nd_level() { # Function to create link if [[ -z "$1" ]] then msg_error "Missing 1st argument (source)" exit 2 fi if [[ -z "$2" ]] then msg_error "Missing 2nd argument (destination)" exit 2 fi for file in $1 do create_link "$file" "$2" || exit 2 done } function create_dir_gameserver() { if [ -z "$1" ] then msg_error "Missing 1st argument (source)" exit 2 fi mkdir -p "$1" || exit 2 chown $UIDGAMESERVER:$GIDGAMESERVER "$1" || exit 2 } function create_file_gameserver() { if [ -z "$1" ] then msg_error "Missing 1st argument (source)" exit 2 fi touch "$1" || exit 2 chown $UIDGAMESERVER:$GIDGAMESERVER "$1" || exit 2 } ###################### ### Manage multi process ###################### declare -A JOBS declare -A COMMENTJOBS function init_job() { unset JOBS unset COMMENTJOBS } function launch_job() { msg_debug "Launch job '$2'" eval $1 & JOBS[$!]="$1" COMMENTJOBS[$!]="$2" } function wait_all_job() { local cmd local code=0 for pid in ${!JOBS[@]} do msg_debug "Wait job '${COMMENTJOBS[$pid]}' (pid:$pid) " cmd=${JOBS[${pid}]} wait ${pid} JOBS[${pid}]=$? if [[ ${JOBS[${pid}]} -ne 0 ]] then code=${JOBS[${pid}]} msg_error "'${COMMENTJOBS[$pid]}' Exited with error [return code:$code, pid:${pid}, command:'${cmd}']" fi done return $code } ###################### # ######################