pack - Compresses and converts files base64 to copy and paste into a command line session

#!/usr/bin/env bash
help_text="
NAME
    pack - Compresses and converts files base64 to copy / paste into a command line session. 

USAGE
    pack [options] <filename(s)>

OPTIONS
    -f|--force
        Overwrite existing pack if one already exists.

    -h|--help
        Show help text.

    -n|--name <name>
        Specify a name from the pack file.

    -s|--silent
        Suppress terminal output.

DESCRIPTION
    Compresses one or more files and converts these to base64 to copy and paste into a command
    line session with commands to convert these back to the original file.  The output is also
    written to a file, the name of which can be specified using the -n option.  If no name is
    specified, for a single file the output is written to '<filename>.pack', for multiple files
    the output is written to 'multi.pack'.

AUTHOR
    mjnurse.github.io - 2020
"
help_line="Compresses and converts files base64 to copy and paste into a command line session"
web_desc_line="Compresses and converts files base64 to copy and paste into a command line session"

try="Try ${0##*/} -h for more information"
tmp="${help_text##*USAGE}"
usage=$(echo "Usage: ${tmp%%OPTIONS*}" | tr -d "\n" | sed "s/  */ /g")

line="-------------------------------------------------"; line="# $line$line"
silent_yn=n
force_yn=n

if [[ "$1" == "" ]]; then
    echo "${usage}"
    echo "${try}"
    exit 1
fi

while [[ "$1" != "" ]]; do
    case $1 in
        -f|--force) # arg: Overwrite existing pack if one already exists.
            force_yn=y
            ;;
        -h|--help) # arg: Show help text.
            echo "$help_text"
            exit
            ;;
        -n|--name) # arg: <name> Specify a name from the pack file.
            shift
            pack_name="$1"
            ;;
        -s|--silent) # arg: Suppress terminal output.
            silent_yn=y
            ;;
        ?*)
            break
            ;;
    esac
    shift
done

if [[ $# == 1 ]]; then
    pack_name="${pack_name:-${1}}.pack"
else
    pack_name="${pack_name:-multi}.pack"
fi

if [[ -f $pack_name && $force_yn == n ]]; then
    read -p "File '${pack_name}' already exists.  Overwrite [yN]? " yn
    if [[ "${yn,,}" != y ]]; then
        exit
      fi
fi

rm -f $pack_name

multi_yn=n
if [[ $# != 1 ]]; then
    echo "$line" >> $pack_name
    files="$( echo " $* " | sed 's/ pack / /g; s/ [^ ]*\.pack / /g; s/^  *//; s/  *$//;')"
    echo "${files// /, }" | fold -w 88 -s \
        | sed "1s/^/# CONTENTS: /; 2,99s/^/#           /" >> $pack_name
    multi_yn=y
fi

for f in $*; do
    if [[ "$f" != "pack" && ! "$f" =~ .*pack ]]; then
        if [[ $multi_yn == y ]]; then
            echo "${line}" >> $pack_name
            echo "# FILE: $f " >> $pack_name
            echo "${line}" >> $pack_name
        fi
        echo "$(cat $f | gzip -f -9 | base64 -w 999999999 )" | sed "s/^/echo '/" \
            | fold -w 100 >> $pack_name
        echo "' | base64 -d | gunzip > $f; chmod u+x $f" >> $pack_name
    fi
done

if [[ $silent_yn == n ]]; then
    cat $pack_name
    echo
    if command -v clip.exe &> /dev/null; then 
        cat $pack_name | clip.exe
        echo "Copied to clipboard"
    else
        echo "clip.exe not found, not copied to clipboard"
    fi
fi