#!/usr/bin/env bash
#
# Description: Compresses all files and directories in the current directory,
#              with the specified ($1) compression algorithm.
#
# Homepage: https://codeberg.org/krathalan/miscellaneous-scripts
#
# Copyright (C) 2020-2024 Hunter Peavey
#
# 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 <https://www.gnu.org/licenses/>.

# -----------------------------------------
# -------------- Guidelines ---------------
# -----------------------------------------

# This script follows the Google Shell Style Guide:
# https://google.github.io/styleguide/shell.xml

# This script uses shellcheck: https://www.shellcheck.net/

# See https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
set -Eeuo pipefail

trap "clean_up" EXIT

# -----------------------------------------
# ----------- Program variables -----------
# -----------------------------------------

# Colors
GREEN=$(tput bold && tput setaf 2)
RED=$(tput bold && tput setaf 1)
YELLOW=$(tput bold && tput setaf 3)
BLUE=$(tput sgr0 && tput setaf 4)
PURPLE=$(tput sgr0 && tput setaf 5)
CYAN=$(tput sgr0 && tput setaf 6)
NC=$(tput sgr0) # No color/turn off all tput attributes
GREY=$(tput sgr0 && tput setaf 8)
WHITE=$(tput sgr0 && tput bold)

readonly GREEN RED YELLOW BLUE PURPLE CYAN NC GREY WHITE

# Other
readonly SCRIPT_NAME="${0##*/}"
readonly TMP_DIR="$(mktemp -d -t "${SCRIPT_NAME}.XXXXXXXX")"
readonly TMP_FILE="${TMP_DIR}/compressed_files.txt"

# -----------------------------------------
# ------------- User variables ------------
# -----------------------------------------

# -----------------------------------------
# --------------- Functions ---------------
# -----------------------------------------

clean_up()
{
  rm -rf "${TMP_DIR}"
}

#######################################
# Prints passed error message before premature exit.
# Prints everything to >&2 (STDERR).
# Globals:
#   RED, NC
#   SCRIPT_NAME
# Arguments:
#   $1: error message to print
# Returns:
#   none
#######################################
exit_script_on_failure()
{
  printf "%sError%s: %s\n" "${RED}" "${NC}" "$1" >&2
  exit 1
}

print_info()
{
  printf "%s%s%s\n" "${WHITE}" "$1" "${NC}"
}

print_verbose()
{
  printf "%s%s%s\n" "${GREY}" "$1" "${NC}"
}

print_warning()
{
  printf "%sWarning:%s %s%s\n" "${RED}" "${YELLOW}" "$1" "${NC}"
}

find_compressed_files()
{
  if [[ -z "${1:-}" ]]; then
    exit_script_on_failure "No value passed to find_compressed_files()"
  fi

  rm -f "${TMP_FILE}"
  touch "${TMP_FILE}"

  local -r fileTypes=("zst" "zst" "xz" "7z" "chd" "rvz" "iso" "xci" "nsp" "zip" "gz")
  local commandToRun
  commandToRun="find $(realpath "$1") -iname ""*.${fileTypes[0]}"""

  local whileCounter=0
  for fileType in "${fileTypes[@]}"; do
    if [[ ${whileCounter} == 0 ]]; then
      whileCounter=$(( whileCounter + 1 ))
      continue
    fi

    # shellcheck disable=SC2125
    commandToRun="${commandToRun} -o -iname "*.${fileType}""
  done

  # Disable globbing for find command to work correctly
  set -f
  ${commandToRun} &> "${TMP_FILE}"
  set +f

  fileLength="$(wc -l "${TMP_FILE}" | cut -d' ' -f1)"

  if [[ ${fileLength} -gt 0 ]]; then
    printf "\n"
    print_warning "compressed files found in target directory:"
    printf "%s" "$(<"${TMP_FILE}")"

    printf "\n\n%s==>%s %sProceed with compression?%s\n" "${YELLOW}" "${NC}" "${WHITE}" "${NC}"
    read -r -p "[y/N] " response
    case "$response" in
      [yY][eE][sS]|[yY])
        return
        ;;
      *)
        print_info "No files compressed."
        false
        ;;
    esac
  fi
}

compress_file_dir()
{
  local -r file="$1"

  # Do not compress already compressed files
  if [[ ${file} =~ (zst|xz|7z|chd|xci|nsp|zip|gz)$ ]]; then
    print_info "${file##*/} is already compressed; continuing"
    return
  fi

  # Do not overwrite compressed files
  if [[ -f "${file}.tar.zst" ]]; then
    print_info "${file##*/}.tar.zst already exists; continuing"
    return
  fi

  # Warn about compressed files
  if [[ -d "${file}" ]]; then
    find_compressed_files "${file}" || return
  fi

  print_info "Creating ${file##*/}.tar.zst..."
  tar -I "pzstd -19" -cf "${file%/}.tar.zst" "${file}"
}

# -----------------------------------------
# ---------------- Script -----------------
# -----------------------------------------

[[ "$(whoami)" = "root" ]] &&
  exit_script_on_failure "This script should NOT be run as root (or sudo)!"

if [[ -n "${1:-}" ]]; then
  print_info "Mode: compressing specified file/directory"

  compress_file_dir "$1"
else
  print_info "Mode: compressing all files and directories"

  for file in ./*; do
    compress_file_dir "${file}" || true
  done
fi
