#!/bin/sh
#
# fsextract - Extract native fscat format to a destination.
#
# Copyright (C) 2005 Oracle.  All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public
# License, version 2, as published by the Free Software Foundation.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# 
# You should have received a copy of the GNU General Public
# License along with this program; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 021110-1307, USA.
#

# From configure
VERSION="0.4.4"
prefix="/usr"
exec_prefix="${prefix}"
sbindir="/sbin"

PROGNAME=fsextract
DRIVER_DIR="${sbindir}"


#
# Processing parameters
#

# When filename is empty, we read from stdin
FILENAME=
# If LISTARG is -t, the extactor will just list the archive
LISTARG=
# The default compression is none
COMPRESSOR=
# How verbose should the extractor be
VERBOSE=

print_usage()
{
    RC="$1"

    if [ "$RC" = 0 ]
    then
        OUT=1
    else
        OUT=2
    fi

cat <<EOF >&${OUT}
Usage: ${PROGNAME} [-t] [-v] [-j|-z] [-f <file>]
EOF

    exit $RC
}

print_version()
{
    echo "${PROGNAME} version ${VERSION}"
    exit 0
}

while getopts ":f:tzjvhV-:" opt
do
    case "$opt" in
    f)
        # Treat '-' as stdin
        if [ "${OPTARG}" != "-" ]
        then
            FILENAME="${OPTARG}"
        fi
        ;;
    t)
	LISTARG="-t"
        ;;
    z)
        COMPRESSOR="gzip"
        ;;
    j)
        COMPRESSOR="bzip2"
        ;;
    v)
        VERBOSE="${VERBOSE} -v"
        ;;
    h)
        print_usage 0
        ;;
    V)
        print_version
        ;;
    "-")
        case "$OPTARG" in
        "help")
            print_usage 0
            ;;
        "version")
            print_version
            ;;
        *)
            echo "${PROGNAME}: Unknown option '-${OPTARG}'" >&2
            print_usage 1
            ;;
        esac
        ;;
    ":")
        echo "${PROGNAME}: Option '-${OPTARG}' requires an argument" >&2
        print_usage 1
        ;;
    "?")
        echo "${PROGNAME}: Unknown option: '-${OPTARG}'" >&2
        print_usage 1
        ;;
    *)
        echo "${PROGNAME}: What the?  Option parsing" >&2
        print_usage 1
        ;;
    esac
done

shift $(expr ${OPTIND} - 1)

DEVICE="$1"

if [ -z "$COMPRESSOR" -a -n "$FILENAME" ]
then
    case "$FILENAME" in
    *.bz2)
        COMPRESSOR=bzip2
        ;;
    *.gz)
        COMPRESSOR=gzip
        ;;
    *)
        COMPRESSOR=cat
        ;;
    esac
fi


EXTRACTOR="${DRIVER_DIR}/fscat_extract"
if [ ! -x "${EXTRACTOR}" ]
then
    echo "${PROGNAME}: Native fscat extractor is not available." >&2
    exit 1
fi

if [ -z "$FILENAME" ]
then
    "${EXTRACTOR}" -d $LISTARG $VERBOSE
else
    "${COMPRESSOR}" -dc "${FILENAME}" | "${EXTRACTOR}" -d $LISTARG $VERBOSE
fi

exit $?
