#! /bin/bash

# $Id: ftar 4993 2008-06-16 12:29:06Z lange $
#*********************************************************************
#
# ftar -- extract tar files using FAI classes
#
# This script is part of FAI (Fully Automatic Installation)
# Copyright (C) 2001-2008 Thomas Lange, lange@informatik.uni-koeln.de
# Universitaet zu Koeln
#
#*********************************************************************
# This program 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.
#
# 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.
# 
# A copy of the GNU General Public License is available as
# '/usr/share/common-licences/GPL' in the Debian GNU/Linux distribution
# or on the World Wide Web at http://www.gnu.org/copyleft/gpl.html.  You
# can also obtain it by writing to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
#*********************************************************************

version="Version 1.6.2, 16-june-2008"

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
die() {
    echo "ftar died with error: $1"
    exit 99
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
extract() {

    local file=$1
    local catname=$2

    echo "ftar: extracting $file to $target/$dir"
    $catname $file | tar -C $target/$dir $vflag -xf -
    tardone=1
    # if option -1 is set, only one class will be used
    [ $single -eq 1 ] && exit 0
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
usage() {

    cat <<EOF
ftar, extract tar files using classes. $version

   Copyright (C) 2001-2008 by Thomas Lange

Usage: ftar [OPTION] ... SOURCE

   -1                   Use only first tar file matching class name.
   -c class[class]      Define classes (space separated).
   -d                   Delete all files in target before extracting.
   -D                   Create debug output.
   -h                   Show summary of options.
   -r                   Recursively remove files in target before extracting.
   -s source_dir        Look for source files relative to source_dir.
   -t target_dir        Extract files relativ to target_dir.
   -v                   Be verbose. Not yet used.

Report bugs to <lange@informatik.uni-koeln.de>.
EOF
    exit 0
}
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

source=$FAI/files
target=$FAI_ROOT
deletefiles=0
removedir=0
tardone=0
single=0

while getopts 1hDdrvs:t:c: opt ; do
        case "$opt" in
        d) deletefiles=1 ;;
        D) debug=1 ;;
        r) removedir=1 ;;
#        v) verbose=1 ;;
        1) single=1 ;;
        s) source=$OPTARG ;;
        t) target=$OPTARG ;;
        c) classes=$OPTARG ;;
        h) usage ;;
	esac
done
shift $(($OPTIND - 1))

[ "$1" ] || usage
[ -f $ENV{LOGDIR}/FAI_CLASSES ] && classes=$(< $ENV{LOGDIR}/FAI_CLASSES)
# last class has highest priority

# reverse order of classes
for class in $classes; do
    revclasses="$class $revclasses"
done

[ "$debug" ] && vflag="-v"
[ "$debug" ] && echo "ftar: classes : $revclasses"
[ -z "$source" ] && die "Source directory undefined."
[ -z "$target" ] && die "Target directory undefined."

# currently only one directory is used
dir=$1
fpath=$source/$dir
[ -d $fpath ] || die "No directory $fpath"

[ $deletefiles -eq 1 ] && cd $target/$dir && rm -f  .* * 2>/dev/null

if [ $removedir -eq 1 ]; then
    cd $target/$dir
    if [ $PWD = "/" ]; then
	echo "ftar WARNING: Will not do recursive removal of directory /"
    else
	rm -rf .* * 2>/dev/null
    fi
fi

for c in $revclasses ; do
    # what if a directory exists which is equal to the hostname or a classname?
    # [ -f $fpath/$c ]          && extract $fpath/$c cat
    [ -f $fpath/$c.tgz ]      && die "Suffix .tgz not supported. Use .tar.gz"
    [ -f $fpath/$c.tar ]      && extract $fpath/$c.tar cat
    [ -f $fpath/$c.tar.gz ]   && extract $fpath/$c.tar.gz zcat
    [ -f $fpath/$c.tar.bz2 ]  && extract $fpath/$c.tar.bz2 bzcat
done

if [ $tardone -eq 0 ]; then
    echo "ftar: No matching class found in $fpath"
    exit 1
else
    exit 0
fi

