#!/bin/sh
# Called by abrtd when a new file is noticed in upload directory.
# The task of this script is to unpack the file and move
# problem data found in it to abrtd spool directory.
#
# Usage: abrt-handle-upload [-d] ABRT_SPOOL_DIR UPLOAD_DIR FILENAME
#
#   -d             - deletes an uploaded archive
#   ABRT_SPOOL_DIR - a directory where valid uploaded archives are unpacked to
#   UPLOAD_DIR     - a directory where uploaded archives are stored in
#   FILENAME       - an uploaded archive file name

#echo "Started: $0 $*"

print_clean_and_die()
{
    printf "%s\n" "$*"
    #echo delete_on_exit="$delete_on_exit"
    test "$delete_on_exit" && rm -rf -- $delete_on_exit
    exit $die_exitcode
}

die_exitcode=1
delete_on_exit=""

delete_archive=false
if test x"$1" == x"-d"; then delete_archive=true; shift; fi

abrt_dir="$1"
upload_dir="$2"
archive="$3"

test -d "$abrt_dir" || print_clean_and_die "Not a directory: '$abrt_dir'"
test -d "$upload_dir" || print_clean_and_die "Not a directory: '$upload_dir'"
test x"${archive#/}" != x"$archive" && print_clean_and_die "Skipping: '$archive' (starts with slash)"
test x"${archive#.}" != x"$archive" && print_clean_and_die "Skipping: '$archive' (starts with dot)"
test x"${archive#*..}" != x"$archive" && print_clean_and_die "Skipping: '$archive' (contains ..)"
test x"${archive#* }" != x"$archive" && print_clean_and_die "Skipping: '$archive' (contains space)"
# Note: next line has a tab!
test x"${archive#*	}" != x"$archive" && print_clean_and_die "Skipping: '$archive' (contains tab)"

cd -- "$upload_dir" || print_clean_and_die "Can't chdir to '$upload_dir'"

unpacker=""
test x"${archive%.tar.gz}" != x"$archive" && unpacker="gunzip"
test x"${archive%.tar.bz2}" != x"$archive" && unpacker="bunzip2"
test x"${archive%.tar.xz}" != x"$archive" && unpacker="unxz"

test "$unpacker" || print_clean_and_die "Unknown file type: '$archive'"

workingdir=`mktemp -d "abrt_handle_upload.XXXXXXXXXX" --tmpdir=/tmp` || print_clean_and_die "Can't create working directory"
delete_on_exit="$workingdir"

tempdir="$workingdir/remote.`date +%Y-%m-%d-%H:%M:%S.%N`.$$"
working_archive="$workingdir/$archive"

if $delete_archive; then
    mv -- "$archive" "$working_archive" || print_clean_and_die "Can't move '$archive' to '$working_archive'"
else
    cp -- "$archive" "$working_archive" || print_clean_and_die "Can't copy '$archive' to '$working_archive'"
fi

$unpacker -t -- "$working_archive" || print_clean_and_die "Verification error on '$archive'"

echo "Unpacking '$archive'"
mkdir "$tempdir" || print_clean_and_die "Can't create '$tempdir' directory"
$unpacker <"$working_archive" | tar xf - -C "$tempdir" || print_clean_and_die "Can't unpack '$archive'"

# The archive can contain either plain dump files
# or one or more complete problem data directories.
# Checking second possibility first.
if test -f "$tempdir/analyzer" && test -f "$tempdir/time" && test -f "$tempdir/uid"; then
    printf "1" >"$tempdir/remote"
    mv -- "$tempdir" "$abrt_dir"
else
    (
    cd -- "$tempdir" || exit
    for d in *; do
        test -d "$d" || continue
        printf "1" >"$d/remote"
        dst="$abrt_dir/$d"
        test -e "$dst" && dst="$abrt_dir/$d.$$"
        test -e "$dst" && continue
        mv -- "$d" "$dst"
    done
    )
fi

die_exitcode=0
print_clean_and_die "'$archive' processed successfully"
