#!/usr/bin/env sh

# Extracts the archive.
# The process can vary depending on the file extension.

# Dependencies:
# * tar (GNU tar) 1.32

# Extract each given filename.
for f in $@; do
    if [ -n "$(file "$f" | grep -i '7-zip archive data')" ]; then
        7z x "$f"
    elif [ -n "$(file "$f" | grep -i 'zip archive data')" ]; then
        unzip "$f"
    elif [ -n "$(file "$f" | grep -i 'POSIX tar archive')" ]; then
        tar --extract --file $f # or 'tar xf $f'
    elif [ -n "$(file "$f" | grep -i 'gzip compressed data')" ]; then
        tar --extract --gzip --file "$f" # or 'tar xzf $f'
    elif [ -n "$(file "$f" | grep -i 'bzip2 compressed data')" ]; then
        tar --extract --bzip2 --file "$f" # or 'tar xjf $f'
    elif [ -n "$(file "$f" | grep -i 'RAR archive data')" ]; then
        unrar x "$f"
    else
        echo "unrecognized format."
    fi
done
