#!/usr/bin/env bash
# vim: set sw=4 sts=4 et :

# Copyright (c) 2006, 2007, 2008 Ciaran McCreesh
#
# Based in part upon ebuild.sh from Portage, which is Copyright 1995-2005
# Gentoo Foundation and distributed under the terms of the GNU General
# Public License v2.
#
# This file is part of the Paludis package manager. Paludis is free software;
# you can redistribute it and/or modify it under the terms of the GNU General
# Public License as published by the Free Software Foundation; either version
# 2 of the License, or (at your option) any later version.
#
# Paludis 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  02111-1307  USA

die()
{
    echo "unpack: error: $@" 1>&2
    if [[ -z ${PALUDIS_FAILURE_IS_NONFATAL} ]]; then
        echo "$(basename ${0} ): making ebuild PID ${EBUILD_KILL_PID} exit with error" 1>&2
        kill -s SIGUSR1 "${EBUILD_KILL_PID}"
    fi
    exit 123
}
assert()
{
    local _pipestatus="${PIPESTATUS[*]}"
    [[ -z "${_pipestatus//[ 0]/}" ]] || die "$@: $_pipestatus"
}

unpack_tar() {
    echo tar xf "${1}" --no-same-owner "${TAR_OPTIONS[@]}" 1>&2
    tar xf "${1}" --no-same-owner "${TAR_OPTIONS[@]}"
}

unpack_tar.gz() {
    echo tar zxf "${1}" --no-same-owner "${TAR_OPTIONS[@]}" 1>&2
    tar zxf "${1}" --no-same-owner "${TAR_OPTIONS[@]}"
}

unpack_tar.bz2() {
    echo tar jxf "${1}" --no-same-owner "${TAR_OPTIONS[@]}" "${UNPACKONLY}" 1>&2
    tar jxf "${1}" --no-same-owner "${TAR_OPTIONS[@]}" "${UNPACKONLY}"
}

unpack_zip() {
    echo unzip -qo "${1}" 1>&2
    unzip -qo "${1}"
}

unpack_gz() {
    echo gzip -dc "${1}" \> "${UNPACKTODIR}/$(basename "${1%.*}" )" 1>&2
    gzip -dc "${1}" > "${UNPACKTODIR}/$(basename "${1%.*}" )"
}

unpack_bz2() {
    echo bzip2 -dc "${1}" \> "${UNPACKTODIR}/$(basename "${1%.*}" )" 1>&2
    bzip2 -dc "${1}" > "${UNPACKTODIR}/$(basename "${1%.*}" )" || die "Couldn't unpack ${1}"
}

unpack_rar() {
    echo unrar x -idq "${1}" 1>&2
    unrar x -idq "${1}"
}

unpack_lha() {
    echo lha xfq "${1}" 1>&2
    lha xfq "${1}"
}

unpack_a() {
    echo ar x "${1}" 1>&2
    ar x "${1}" 1>&2
}

unpack_tar.lzma() {
    echo lzma -dc "${1}" \| tar xf - --no-same-owner "${TAR_OPTIONS[@]}"
    lzma -dc "${1}" | tar xf - --no-same-owner "${TAR_OPTIONS[@]}"
    assert "Couldn't unpack ${1}"
}

unpack_lzma() {
    echo lzma -dc "${1}" \> "${UNPACKTODIR}/$(basename "${1%.*}" )" 1>&2
    lzma -dc "${1}" > "${UNPACKTODIR}/$(basename "${1%.*}" )"
}

unpack_7z() {
    echo 7z x -y "${1}"
    7z x -y "${1}"
}

unpack_tar.xz() {
    echo xz -dc "${1}" \| tar xf - --no-same-owner "${TAR_OPTIONS[@]}"
    xz -dc "${1}" | tar xf - --no-same-owner "${TAR_OPTIONS[@]}"
    assert "Couldn't unpack ${1}"
}

unpack_xz() {
    echo xz -dc "${1}" \> "${UNPACKTODIR}/$(basename "${1%.*}" )" 1>&2
    xz -dc "${1}" > "${UNPACKTODIR}/$(basename "${1%.*}" )"
}

unpack_one()
{
    [[ -z ${1} ]] && die "Bad argument for unpack_one"
    [[ -e ${1} ]] || die "${1} doesn't exist"

    if [[ -n "${UNPACKONLY}" ]] ; then
        case ${1} in
            *.tar.bz2|*.tbz2|*.tbz)
            ;;

            *)
            die "Can't use --only with ${x}"
            ;;
        esac
    fi

    local group split suffix
    for group in ${PALUDIS_UNPACK_SUFFIXES}; do
        split=( $(IFS=,; echo ${group} ) )
        for suffix in ${split[@]}; do
            if [[ ${1} == *.${suffix} ]]; then
                unpack_${split[0]} "${1}" || die "Couldn't unpack ${1}"
                return
            fi
        done
    done

    if [[ -n "${PALUDIS_UNPACK_UNRECOGNISED_IS_FATAL}" ]] ; then
        die "Don't know how to unpack ${x}"
    else
        echo "Skipping unpack for ${1}"
    fi
}

export UNPACKFROMDIR="${!PALUDIS_UNPACK_FROM_VAR}"
export UNPACKTODIR="${PWD}"
unset UNPACKONLY
TAR_OPTIONS=( )

while [[ "${1#--}" != "${1}" ]] ; do
    if [[ "${1}" == "--only" ]] ; then
        shift
        export UNPACKONLY="${1}"
        shift
    elif [[ "${1}" == "--if-compressed" ]] ; then
        export PALUDIS_UNPACK_UNRECOGNISED_IS_FATAL=
        shift
    elif [[ "${1}" == --tar-option=* ]] ; then
        TAR_OPTIONS[${#TAR_OPTIONS[@]}]="${1#*=}"
        shift
    else
        die "Unrecognised option: ${1}"
    fi
done

[[ -z "$@" ]] && die "No arguments given to unpack"

for x in "$@" ; do
    x=$(echo $x )
    echo ">>> Unpacking ${x} to ${UNPACKTODIR}"
    [[ "${x:0:2}" == "./" ]] || x="${UNPACKFROMDIR}/${x}"
    mkdir -p ${UNPACKTODIR}
    cd ${UNPACKTODIR}
    unpack_one "${x}"
done

if [[ -n "${PALUDIS_UNPACK_FIX_PERMISSIONS}" ]] ; then
    find ${UNPACKTODIR} -mindepth 1 -maxdepth 1 ! -type l -print0 | xargs -0 -r chmod -fR a+rX,u+w,go-w
fi

