#!/bin/bash

#Global Variables
MAP_TEMP="/tmp/css_maps"

#functions
function coloredEcho(){
    local exp=$1;
    local color=$2;
    local bold=$3
    if ! [[ $color =~ '^[0-9]$' ]] ; then
       case $(echo $color | tr '[:upper:]' '[:lower:]') in
        black) color=0 ;;
        red) color=1 ;;
        green) color=2 ;;
        yellow) color=3 ;;
        blue) color=4 ;;
        magenta) color=5 ;;
        cyan) color=6 ;;
        white|*) color=7 ;; # white or invalid color
       esac
    fi
    tput setaf $color;
    tput $bold;
    echo $exp;
    tput sgr0;
}

WelcomeMessage(){
 clear
 coloredEcho "CSS Map Installer Script - v0.1 beta" yellow bold
 coloredEcho "Developed by JasonBourne | Special thanks to Orion!" yellow bold
 coloredEcho "---------------------------------------------------" yellow bold
}

CheckDependencies(){
 for pkg in $1
 do
  REQUIRED_PKG="$pkg"
  PKG_OK=$(dpkg-query -W --showformat='${Status}\n' $REQUIRED_PKG|grep "install ok installed")
  echo "Checking for $REQUIRED_PKG: $PKG_OK"
  if [ "" = "$PKG_OK" ]; then
    echo "No $REQUIRED_PKG. Setting up $REQUIRED_PKG."
    sudo apt-get update
    sudo apt-get --yes install $REQUIRED_PKG 
  fi
 done
}

CheckDirectoryExists(){
 DIRECTORY=$1
 if [[ -d "$DIRECTORY" ]]; then
  echo "$DIRECTORY directory was found!"
  return $DIRECTORY
 else
  coloredEcho "$DIRECTORY directory was not found!" red bold
  return $DIRECTORY
 fi
}

CheckFileIntegrity(){
 MAP_PATH=$1
 MAP_FILE_EXTENSION="${MAP_PATH##*.}"
 
 case "${MAP_FILE_EXTENSION}" in
  zip) sudo unzip -t $MAP_PATH;;
  rar) sudo unrar t $MAP_PATH;;
 esac
}

UncompressFile(){
 MAP_PATH=$1
 MAP_FILE_EXTENSION="${MAP_PATH##*.}"

 case "${MAP_FILE_EXTENSION}" in
  zip) sudo unzip -e $MAP_PATH -d $MAP_TEMP;;
  rar) sudo unrar -e $MAP_PATH $MAP_TEMP;;
 esac
}

FindDirectory(){
PATH=$1
DIRECTORY=$2
NOT_FOUND="0"

 for OBJ in $PATH/*; do
     #[ -d "${OBJ}" ] || continue # if not a directory, skip
     if [ "${OBJ##*/}" = "$DIRECTORY" ]; then
      echo "${OBJ}"
      NOT_FOUND="0"
      break
     else
      NOT_FOUND="1"
     fi
 done
 
 if [ "$NOT_FOUND" = "1" ]; then
  echo "$DIRECTORY folder was not found on $PATH"
 fi
}

#Main
WelcomeMessage

# Get input parameters
while getopts g:m: option
do
 case "${option}" in
  g) GAME_PATH=${OPTARG};;
  m) MAP_PATH=${OPTARG};;
 esac
done

#Show working paths
coloredEcho "Game path: $GAME_PATH" cyan bold
coloredEcho "Map path: $MAP_PATH" cyan bold
echo ""

#Check pkg dependencies
coloredEcho "Checking package dependencies..." blue bold
CheckDependencies "unrar unzip"

# Check folders
coloredEcho "Checking folder game path..." blue bold
GAME_MAPS_PATH=$(FindDirectory $GAME_PATH "maps")
GAME_SOUND_PATH=$(FindDirectory $GAME_PATH "sound")
GAME_MODELS_PATH=$(FindDirectory $GAME_PATH "models")
GAME_MATERIALS_PATH=$(FindDirectory $GAME_PATH "materials")


if [[ "$GAME_MAPS_PATH" != *'not found'* ]]; then
  coloredEcho "$GAME_MAPS_PATH folder found!" green bold;
 else
  coloredEcho "$GAME_MAPS_PATH" red bold;
fi
if [[ "$GAME_SOUND_PATH" != *'not found'* ]]; then
  coloredEcho "$GAME_SOUND_PATH folder found!" green bold;
 else
  coloredEcho "$GAME_SOUND_PATH" red bold;
fi
if [[ "$GAME_MODELS_PATH" != *'not found'* ]]; then
  coloredEcho "$GAME_MODELS_PATH folder found!" green bold;
 else
  coloredEcho "$GAME_MODELS_PATH" red bold;
fi
if [[ "$GAME_MATERIALS_PATH" != *'not found'* ]]; then
  coloredEcho "$GAME_MATERIALS_PATH folder found!" green bold;
 else
  coloredEcho "$GAME_MATERIALS_PATH" red bold;
fi

#Check if the map file is compressed, check integrity and uncompress the files in tmp folder
coloredEcho "Checking integrity and uncompressing map files..." blue bold
CheckFileIntegrity $MAP_PATH
UncompressFile $MAP_PATH

coloredEcho "Checking map files..." blue bold
coloredEcho "Maps files..." magenta bold
find "$MAP_TEMP" -type f -iname "*.bsp" -o -iname "*.txt" -o -iname "*.nav"
coloredEcho "Sound files..." magenta bold
find "$MAP_TEMP" -type f -iname "*.wav" -o -iname "*.mp3"
coloredEcho "Models files..." magenta bold
find "$MAP_TEMP" -type f -iname "*.vtx" -o -iname "*.mdl" -o -iname "*.phy" -o -iname "*.vvd"
coloredEcho "Materials files..." magenta bold
find "$MAP_TEMP" -type f -iname "*.vmt" -o -iname "*.vtf"

#Copy the files ending in the "bsp," "txt" and "nav" extension to the Maps folder
coloredEcho "Copying map files to 'Maps' folder..." blue bold
if [[ -d "$GAME_MAPS_PATH" ]]; then
 find "$MAP_TEMP" -type f -iname "*.bsp" -exec cp -av {} "$GAME_MAPS_PATH" \;
 find "$MAP_TEMP" -type f -iname "*.txt" -exec cp -av {} "$GAME_MAPS_PATH" \;
 find "$MAP_TEMP" -type f -iname "*.nav" -exec cp -av {} "$GAME_MAPS_PATH" \;
fi

#Copy files ending in the "wav" extension to the "Sound" folder
coloredEcho "Copying wav, mp3 files to 'Sound' folder..." blue bold
if [[ -d "$GAME_SOUND_PATH" ]]; then
 find "$MAP_TEMP" -type f -name "*.wav" -exec cp -av {} "$GAME_SOUND_PATH" \;
 find "$MAP_TEMP" -type f -name "*.mp3" -exec cp -av {} "$GAME_SOUND_PATH" \;
fi

#Copy files ending in the "vtx," "mdl," "phy" and "vvd" extensions to the "Models" folder
coloredEcho "Copying vtx, mdl, phy and vvd files to 'Models' folder..." blue bold
if [[ -d "$GAME_MODELS_PATH" ]]; then
 find "$MAP_TEMP" -type f -iname "*.vtx" -exec cp -av {} "$GAME_MODELS_PATH" \;
 find "$MAP_TEMP" -type f -iname "*.mdl" -exec cp -av {} "$GAME_MODELS_PATH" \;
 find "$MAP_TEMP" -type f -iname "*.phy" -exec cp -av {} "$GAME_MODELS_PATH" \;
 find "$MAP_TEMP" -type f -iname "*.vvd" -exec cp -av {} "$GAME_MODELS_PATH" \;
fi

#Copy files ending in the "vmt" or "vtf" extensions to the "Materials" folder
coloredEcho "Copying vmt, vtf files to 'Materials' folder..." blue bold
if [[ -d "$GAME_MATERIALS_PATH" ]]; then
 find "$MAP_TEMP" -type f -iname "*.vmt" -exec cp -av {} "$GAME_MATERIALS_PATH" \;
 find "$MAP_TEMP" -type f -iname "*.vtf" -exec cp -av {} "$GAME_MATERIALS_PATH" \;
fi

coloredEcho "Deleting temporary folders..." blue bold
sudo rm -r $MAP_TEMP

coloredEcho "Process completed successful!" green bold
