#!/bin/bash
# RSYNC from symlink-directorry to (multiple) destinatiions (use wildcard for mutiple directories).
dest='/home/panel/fgh/users/*/gameserver/*/csgo';

function binary() {
  LANG=C grep '[^ -~]\+' "${1}" | grep -E "^Binary file ${1} matches$" > /dev/null && return 0 || return 1;
}

# Set source directory (key) and target (value) directory.
declare -A sources=(
  [/home/panel/fgh/templates/csgo]="/home/panel/fgh/templates/csgo_sym"
);

# Set tar.gz location (value) for the given source (key) of above.
declare -A targzloc=(
  [/home/panel/fgh/templates/csgo]="/home/panel/fgh/templates/34.tar.gz"
);

# Function to check if the source file is ASCII or BINARY and will make symlinks of BINARY files (from symlink location to raw location) as copies of ASCII files.
function checker() {

  source="${1}";
  target="${2}";
  target_usrgrp="${3}";

  while read -r file ;do

    file_strip=$( sed "s|${source}/||g" <<< "${file}" );

    dir_strip=$( dirname "${file_strip}" )

    [ ! -d "${target}/${dir_strip}" ] && mkdir -p "${target}/${dir_strip}" ;

    if [ ! -L "${target}/${file_strip}" ] && binary "${source}/${file_strip}" || [ -L "${target}/${file_strip}" ] && [ "$(readlink "${target}/${file_strip}")" != "${source}/${file_strip}" ] && binary "${source}/${file_strip}" ;then
      ln -fs "${source}/${file_strip}" "${target}/${file_strip}" ;
      [[ "${target_usrgrp}" =~ .+\:.* ]] && chown -h ${target_usrgrp} "${target}/${file_strip}" ;
      echo "Symlink ${target}/${file_strip} created.";
    elif [ ! -f "${target}/${file_strip}" ] && ! binary "${source}/${file_strip}"; then
      rsync -rauz "${source}/${file_strip}" "${target}/${file_strip}" ;
      echo "Symlink ${target}/${file_strip} converted to file." ;
    elif [ ! -f "${target}/${file_strip}" ] && [ ! -L "${target}/${file_strip}" ]; then
      echo "What to do with ${target}/${file_strip}  ?" ;
    fi

  done

}

for source in ${!sources[@]} ;do

  target="$( cd "${sources[${source}]}" && pwd )";

  [ ! -d "${target}" ] && mkdir -p ${target} ;

  target_usrgrp=$( stat "${target}" | grep -E "^Access:\s+\([0-7]{4}\/\S+\)\s+Uid:\s+\(\s+([0-9]+)\/\s*(\S+)\)\s+Gid:\s+\(\s+([0-9]+)\/\s*(\S+)\)$" | sed -r 's/^Access:\s+\([0-7]{4}\/\S+\)\s+Uid:\s+\(\s*([0-9]+)\/\s*(\S+)\)\s+Gid:\s+\(\s*([0-9]+)\/\s*(\S+)\)$/\2:\4/' );

  # Rsync source (raw version) with target (symlink version)
  find  "${source}" -not \( -type d \) | checker "${source}" "${target}" "${target_usrgrp}"


  # Sync symlink location with all the gameserver locations.
  for destination in $(find ${dest} -maxdepth 0) ;do

    # Find the owner of the root map.
    dest_usrgrp=$( stat "${destination}" | grep -E "^Access:\s+\([0-7]{4}\/\S+\)\s+Uid:\s+\(\s+([0-9]+)\/\s*(\S+)\)\s+Gid:\s+\(\s+([0-9]+)\/\s*(\S+)\)$" | sed -r 's/^Access:\s+\([0-7]{4}\/\S+\)\s+Uid:\s+\(\s*([0-9]+)\/\s*(\S+)\)\s+Gid:\s+\(\s*([0-9]+)\/\s*(\S+)\)$/\2:\4/' );

    # Rsync the symlink dir with all the csgo servers.
#    while read -r line ;do
#      rsync_file=${line/ *->*/};
#      # Set chown on rsynced file.
#      if [ -f "${destination}/${rsync_file}" ] || [ -L "${destination}/${rsync_file}" ] || [ -d "${destination}/${rsync_file}" ]; then
#echo "${destination}/${rsync_file} 12"
##        grep -E "^${target}" <<< "${line}" > /dev/null && 
#        [[ "${dest_usrgrp}" =~ .+\:.* ]] && chown -hR ${dest_usrgrp} "${destination}" && \
#        echo "File ${target}/${rsync_file} rsynced to ${destination}/${rsync_file/${target}/} ." ;
#      fi
#    done <<< "$( rsync -rauzv "${target}/" "${destination}/" 2>&1 )"

    # RSYNC target to destination
    echo "Starting RSYNC from ${target} to ${destination}";

    rsync -rauz "${target}/" "${destination}/" ;

    [[ "${dest_usrgrp}" =~ .+\:.* ]] && chown -hR ${dest_usrgrp} "${destination}" ;

  done

  if [ -n "${targzloc[${source}]}" ]; then

    TARGZ=$( basename "${targzloc[${source}]}" ) ;
    TARGZ_DIR=$( dirname "${targzloc[${source}]}" ) ;

    [ ! -d "${TARGZ_DIR}" ] && mkdir -p ${TARGZ_DIR} ;

    cd "${target}";
  
    tar -czf "${TARGZ_DIR}/.${TARGZ}" * ;
  
    tar_usrgrp=$( stat "${TARGZ_DIR}/${TARGZ}" | grep -E "^Access:\s+\([0-7]{4}\/\S+\)\s+Uid:\s+\(\s+([0-9]+)\/\s*(\S+)\)\s+Gid:\s+\(\s+([0-9]+)\/\s*(\S+)\)$" | sed -r 's/^Access:\s+\([0-7]{4}\/\S+\)\s+Uid:\s+\(\s*([0-9]+)\/\s*(\S+)\)\s+Gid:\s+\(\s*([0-9]+)\/\s*(\S+)\)$/\2:\4/' );
  
    [[ "${dest_usrgrp}" =~ .+\:.* ]] && chown ${tar_usrgrp} "${TARGZ_DIR}/.${TARGZ}" ;
  
    mv "${TARGZ_DIR}/.${TARGZ}" "${TARGZ_DIR}/${TARGZ}" ;

  fi

done
