#!/bin/bash SAVEIFS=$IFS IFS="$(printf '\n\t')" DEFAULT_FNAME=compressed_output DEFAULT_FORMAT=tar.gz SCRIPT="$(basename $0)" function help { printf "Usage: %s [-o|--output FNAME] [-f|--format FORMAT] [file2] ... [-- [OPTS_TO_PASSTHROUGH]]\n" "$SCRIPT" printf "Usage: %s \n" "$SCRIPT" printf "\n" printf "Use corresponding compression software based on output filename\n" printf "If only one input is given, .%s format is assumed, and named as file1.%s\n" "$DEFAULT_FORMAT" "$DEFAULT_FORMAT" exit } function get_ext { _f="$(basename "$1")" echo "${_f##*.}" } function get_fname { _f="$(basename "$1")" echo "${_f%.*}" } function is_supported_format { case "$1" in *.cbt|*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar) return 0 ;; *.7z) return 0 ;; *.zip) return 0 ;; *.rar) return 0 ;; esac return 1 } if test $# -eq 0; then help fi IFS=$SAVEIFS function compress { filelist=() output_format="$DEFAULT_FORMAT" while test $# -gt 0; do case "$1" in -o|--output) shift output_fname="$1" ;; -f|--format) shift output_format="$1" ;; -h|--help) help ;; --verbose) export VERBOSE="true" ;; --) # the rest will be passed to ffmpeg breaknow=true ;; *) # set to file list filelist+=("$1") #set -- "$filelist" "$arg" esac shift if [ -n "$breaknow" ]; then break fi done if [ -z "$output_fname" ]; then # fallback to first file's name output_fname="$(get_fname ${filelist[0]})" # fallback to default [ -z "$output_fname" ] && "$DEFAULT_FNAME" fi # see if output filename has extension, and file format is supported if [ -n "$(get_ext "$output_fname")" ] && is_supported_format "$output_fname"; then target="$output_fname" else target="$output_fname.$output_format" fi case "${target%,}" in *.cbt|*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.tar) tar cavf "$target" $@ ${filelist[@]} ;; # *.lzma) unlzma ./"$target" ;; # *.bz2) bunzip2 ./"$target" ;; # *.cbr|*.rar) unrar x -ad ./"$target" ;; # *.gz) gunzip ./"$target" ;; # *.cbz|*.epub|*.zip) # unzip ./"$target" $unzip_args # ;; # *.z) uncompress ./"$target" ;; *.7z) 7z a "$target" $@ ${filelist[@]} ;; *.zip) zip -r "$target" $@ ${filelist[@]} ;; *.rar) rar a "$target" $@ ${filelist[@]} ;; # *.7z|*.apk|*.arj|*.cab|*.cb7|*.chm|*.deb|*.dmg|*.iso|*.lzh|*.msi|*.pkg|*.rpm|*.udf|*.wim|*.xar) # 7z x ./"$target" ;; # *.xz) unxz ./"$target" ;; # *.exe) cabextract ./"$target" ;; # *.cpio) cpio -id < ./"$target" ;; # *.cba|*.ace) unace x ./"$target" ;; # *.zpaq) zpaq x ./"$target" ;; # *.arc) arc e ./"$target" ;; # *.cso) ciso 0 ./"$target" ./"$target.iso" && \ # extract $target.iso && \rm -f $target ;; *) echo "$SCRIPT: '$target' - unknown archive method" return 1 ;; esac } compress $@