#!/usr/bin/env bash

function help()
{
    echo "Help:"
    echo "-----"
    echo ""
    echo "lch [-h] [-v] [-c] [-i] [FILE [FILE...]]"
    echo ""
    echo "Examples:"
    echo "$ lch -h                       - This help"
    echo "$ lch -v                       - Print version"
    echo "$ lch -c                       - Check compresion systems"
    echo "$ lch -i                       - Install the compressors dependencies"
    echo "$ lch file.zip                 - Decompress"
    echo "$ lch file.zip /home/file      - Compress"
    echo "$ lch files.zip file1 file2    - Compress multiple"
    echo ""
    echo "Packing/unpacking supported extensions:"
    echo "  7z, bz2, gz, rar, tar, tar.bz2, tbz, tbz2, tb2, tar.gz, tgz, tar.xz, txz, zip"
    echo ""
    echo "Unpacking only supported extensions:"
    echo "  arj, cab, dmg, iso, lzh, lzma, rpm, squashfs, vdi, vhd, vmdk, win, xar, z"
    echo ""
    echo "Run 'man lch' for more info."
    echo ""
}

function check()
{
    local a=false
    echo -e "Verification of compression systems:\n"
    echo -e "Systems\t\t\tStatus"
    echo "----------------------------------"
    for i in "7z" "bzip2" "gzip" "rar" "tar" "unrar" "unzip" "zip"
    do
        wheel true &
        WPID=$!
        if ! [ -x "$(command -v $i)" ]
        then
            echo -e "$i\t\t\t\e[91mNot Installed\e[0m"
            a=true
        else
            echo -e "$i\t\t\t\e[32mInstalled\e[0m"
        fi
        disown $WPID
        kill $WPID &> /dev/null
    done

    if $a
    then
        echo -e "\nIn order to use all the extensions install the packages."
        echo -e "Or use 'lch -i' to install all dependences"
    fi
    echo ""
}

function lowercase()
{
    echo "$DEST" | tr 'A-Z' 'a-z'
}

function error_not_install()
{
    echo -e "$@ \e[91mis not installed\e[0m, use 'lch -i' to install and 'lch -c' to check."
    exit 0
}

function executer()
{
    wheel true &
    WPID=$!
    if [ -x "$(command -v $1)" ]
    then
        "$@" 1> /dev/null
    else
        error_not_install $1
    fi
    disown $WPID
    kill $WPID &> /dev/null
}

function complete_install(){
    echo -e "$@:\t\t\t\e[32mComplete\e[0m"
}

function failed_install(){
    echo -e "$@:\t\t\t\e[91mFailed\e[0m"
}

function wheel() {
    sp='/-\|'
    while $1; do
        printf '%.1s\b' "$sp"
        sp=${sp#?}${sp%???}
        sleep 0.1s
    done
}