#!/bin/bash

# Error handling
set -e

# For styling/colorizing output
txtbld=$(tput bold)
txtblue=${txtbld}$(tput setaf 4)
txtgreen=${txtbld}$(tput setaf 2)
txtred=${txtbld}$(tput setaf 1)
txtyellow=${txtbld}$(tput setaf 3)
txtwhite=${txtbld}$(tput setaf 7)

# Dependency check
function dependencycheck() {
    numdependencies="$#"
    dependencies=("$@")
    missingdependencies=0

    for ((i = 0; i < numdependencies; i++)); do
        if ! command -v "${dependencies[$i]}" &>/dev/null; then
            echo "${txtred}  dependency not met: ${dependencies[$i]}${txtwhite}"
            missingdependencies=$((missingdependencies + 1))
        fi
    done

    if [ $missingdependencies -gt 0 ]; then
        exit 1
    fi
}

# Main Dependency check
dependencycheck echo sed

# Intro Prompt
echo "${txtgreen}xtract: a simple shell script for decompressing common file formats"

# Reset output style
tput bold &
tput setaf 7

# Simply copy and paste, changing extension string and extraction method below
if [ "${1}" == "-tar" ]; then
    dependencycheck tar
    echo "${txtblue}decompressing .tar...${txtyellow}"
    # main extraction method
    tar xf "${2}"
    echo ".tar decompression finished!${txtwhite}"
    exit 0
fi

if [ "${1}" == "-tgz" ]; then
    dependencycheck tar gunzip
    echo "${txtblue}decompressing .tgz...${txtyellow}"
    # main extraction method
    tar zxf "${2}"
    echo ".tgz decompression finished!${txtwhite}"
    exit 0
fi

if [ "${1}" == "-gz" ]; then
    dependencycheck gunzip
    echo "${txtblue}decompressing .gz...${txtyellow}"
    # main extraction method
    gunzip --keep "${2}"
    echo ".gz decompression finished!${txtwhite}"
    exit 0
fi

if [ "${1}" == "-zip" ]; then
    dependencycheck unzip
    echo "${txtblue}decompressing .zip...${txtyellow}"
    # main extraction method
    unzip "${2}"
    echo ".zip decompression finished!${txtwhite}"
    exit 0
fi

if [[ "${1}" == "-bzip" || "${1}" == "-bzip2" || "${1}" == "-bz2" || "${1}" == "-bz" ]]; then
    dependencycheck bzip2
    echo "${txtblue}decompressing .bz2...${txtyellow}"
    # main extraction method
    bzip2 -kd "${2}"
    echo ".bz2 decompression finished!${txtwhite}"
    exit 0
fi

if [[ "${1}" == "-lzip" || "${1}" == "-lz" ]]; then
    dependencycheck lzip
    echo "${txtblue}decompressing .lz...${txtyellow}"
    # main extraction method
    lzip -d "${2}"
    echo ".lz decompression finished!${txtwhite}"
    exit 0
fi

if [[ "${1}" == "-rzip" || "${1}" == "-rz" ]]; then
    dependencycheck rzip
    echo "${txtblue}decompressing .rz...${txtyellow}"
    # main extraction method
    rzip -kd "${2}"
    echo ".rz decompression finished!${txtwhite}"
    exit 0
fi

if [ "${1}" == "-xz" ]; then
    dependencycheck xz
    echo "${txtblue}decompressing .xz...${txtyellow}"
    # main extraction method
    xz -kd "${2}"
    echo ".xz decompression finished!${txtwhite}"
    exit 0
fi

if [[ "${1}" == "-zstd" || "${1}" == "-zst" ]]; then
    dependencycheck zstd
    echo "${txtblue}decompressing .zst...${txtyellow}"
    # main extraction method
    zstd -kd "${2}"
    echo ".zst decompression finished!${txtwhite}"
    exit 0
fi

if [[ "${1}" == "-ar" || "${1}" == "-a" ]]; then
    dependencycheck ar
    echo "${txtblue}decompressing .a...${txtyellow}"
    # main extraction method
    ar -x "${2}"
    echo ".a decompression finished!${txtwhite}"
    exit 0
fi

if [[ "${1}" == "-brotli" || "${1}" == "-br" ]]; then
    dependencycheck brotli
    echo "${txtblue}decompressing .br...${txtyellow}"
    # main extraction method
    brotli -d "${2}"
    echo ".br decompression finished!${txtwhite}"
    exit 0
fi

## Taken from reddit for future refactoring of this script:
# function extract()
# {
# if [ -f $1 ] ; then
# case $1 in
# *.tar.bz2)   tar xvjf $1     ;;
# *.tar.gz)    tar xvzf $1     ;;
# *.bz2)       bunzip2 $1      ;;
# *.rar)       unrar x $1      ;;
# *.gz)        gunzip $1       ;;
# *.tar)       tar xvf $1      ;; # NOTE: all calls to tar can be combined (will read automatically)
# *.tbz2)      tar xvjf $1     ;;
# *.tgz)       tar xvzf $1     ;;
# *.zip)       unzip $1        ;;
# *.Z)         uncompress $1   ;;
# *.7z)        7z x $1         ;;
# *)           echo "'$1' cannot be extracted via >extract<" ;;
# esac
# else
# echo "'$1' is not a valid file!"
# fi
# }
