#!/usr/bin/env bash
#
# Description: Copy a directory or file to a specified location,
#              compressing it in the process.
#
# 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

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

# Colors
GREEN=$(tput bold && tput setaf 2)
RED=$(tput bold && tput setaf 1)
BLUE=$(tput sgr0 && tput setaf 4)
NC=$(tput sgr0) # No color/turn off all tput attributes
readonly GREEN RED BLUE NC

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

# Used to set default commands to others/enable other functionality
check_command() {
  if [[ -n "$(command -v "$1")" ]]; then
    return 0
  else
    return 1
  fi
}

#######################################
# 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
}

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

if [[ $# -lt 2 ]]; then
  exit_script_on_failure "Please specify (1) a directory to compress and (2) the target directory."
fi

# Get full paths
directoryToCompress="$1"
targetDirectory="$2"

directoryToCompress="$(readlink -f "${directoryToCompress}")"
targetDirectory="$(readlink -f "${targetDirectory}")"

printf "\nSpecify a compression program and level (e.g. \'pzstd -19\'),\nor leave blank to choose a preset compression program next.\n\n"
read -r -p " > " response

compression_program="${response}"
base_program="${compression_program%% *}"
if [[ -n "${base_program}" ]]; then
  case "${base_program}" in
    lz4|xz)
      compression_file_ext="${base_program}"
      ;;
    pigz|gzip)
      compression_file_ext="gz"
      ;;
    zstd|pzstd)
      compression_file_ext="zst"
      ;;
    *)
      exit_script_on_failure "Unknown compression program, can't guess file extension. Submit an issue on Github please!"
  esac
fi

if [[ -z "${response}" ]]; then
  printf "\nWhat kind of compression? Will default to multithreaded compression\nif available. Leave blank to choose (p)zstd -10 (default).\n"
  printf "\n [1] zstd -10 (max -19) (default)"
  printf "\n [2] lz4 -9 (max -12)"
  printf "\n [3] xz -T 0 -6 (max -9)"
  printf "\n [4] gzip -6 (max -9)\n\n"
  read -r -p " > " response
  case "${response}" in
    2)
      compression_program="lz4 -9"
      compression_file_ext="lz4"
      ;;
    3)
      compression_program="xz -T 0 -6"
      compression_file_ext="xz"
      ;;
    4)
      if check_command "pigz"; then
        compression_program="pigz -6"
      else
        compression_program="gzip -6"
      fi
      compression_file_ext="gz"
      ;;
    *)
      # Default
      if check_command "pzstd"; then
        compression_program="pzstd -10"
      else
        compression_program="zstd -10"
      fi
      compression_file_ext="zst"
      ;;
  esac
fi

# Target output file name
targetOutput="${directoryToCompress##*/}.tar.${compression_file_ext}"

if [[ "${GPG_KEY_ID:-}" == "" ]]; then
  response=n
else
  printf "\nSign and encrypt with key %s? " "${GPG_KEY_ID}"
  read -r -p "[y/N] " response
fi

start_time="$(date +%s)"
case "${response}" in
  [yY][eE][sS]|[yY])
    targetOutput="${targetOutput}.gpg"

    printf "\nRunning command:\n%s\n\n" "tar -I \"${compression_program}\" -cf - \"${directoryToCompress}\" | gpg2 -z 0 --default-key ${GPG_KEY_ID} --recipient ${GPG_KEY_ID} --sign --encrypt > \"${targetDirectory}/${targetOutput}\""
    tar -I "${compression_program}" -cf - "${directoryToCompress}" | gpg2 -z 0 --default-key "${GPG_KEY_ID}" --recipient "${GPG_KEY_ID}" --sign --encrypt > "${targetDirectory}/${targetOutput}"
    ;;
  *)
    printf "\nRunning command:\n%s\n\n" "tar -I \"${compression_program}\" -cf \"${targetDirectory}/${targetOutput}\" \"${directoryToCompress}\""
    tar -I "${compression_program}" -cf "${targetDirectory}/${targetOutput}" "${directoryToCompress}"
    ;;
esac
end_time="$(date +%s)"

printf "\nOutput compressed archive %s%s/%s%s%s" "${BLUE}" "${targetDirectory}" "${GREEN}" "${targetOutput}" "${NC}"
# Get the size of the directoryToCompress and the targetOutput file and divide them to get a compression ratio,
# rounded to the nearest hundredth, e.g. 0.91
printf "\nCompression ratio: %.2f" "$(echo "$(du -sc "${targetDirectory}/${targetOutput}" | tail -n1 | awk '{printf $1}') / $(du -sc "${directoryToCompress}" | tail -n1 | awk '{printf $1}')" | bc -l)"
printf "\nTime to compress: %s seconds\n" "$(( end_time - start_time ))"
