#!/usr/bin/env bash
set -euo pipefail

if [ $# -eq 2 ]; then
    alg=$1
    target=$2
elif [ $# -eq 3 ]; then
    alg=$1
    str=$2
    target=$3
elif [ $1 == "-h" ] || [ $1 == "--help" ]; then
    echo "sc-compress ALGORITHM [STRENGTH] TARGET"
    echo "Algorithms: xz, zstd"
    echo "Strength: low, medium, high"
    exit 0
else
    exit 1
fi

command="tar -cv -I\""

# add algorithm
if [ $alg == "xz" ]; then
    command+="pxz"
elif [ $alg == "zstd" ]; then
    command+="zstd -T0"
else
    echo "Unsupported option"
    exit 1
fi

# check if something with the strength is set
if [ ! -z "${str+x}" ]; then
    if [ $str == "low" ]; then
        if [ $alg == "xz" ]; then
            command+=" -1"
        elif [ $alg == "zstd" ]; then
            command+=" -1"
        fi
    elif [ $str == "medium" ]; then
        if [ $alg == "xz" ]; then
            command+=" -5"
        elif [ $alg == "zstd" ]; then
            command+=" -10"
        fi
    elif [ $str == "high" ]; then
        if [ $alg == "xz" ]; then
            command+=" -9"
        elif [ $alg == "zstd" ]; then
            command+=" -19"
        fi
    else
        echo "Unsupported option"
        exit 1
    fi
fi

command+="\""

# add archive file
if [ $alg == "xz" ]; then
    command+=" -f ${target}.tar.xz $target"
elif [ $alg == "zstd" ]; then
    command+=" -f ${target}.tar.zst $target"
fi

# output final command
echo $command

# execute command
eval $command

exit 0