#! /bin/sh
#                                                           -*- sh -*-
# Copyright (c) 2000  Motoyuki Kasahara
#
# 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, 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.
#

MKDIRHIER="/usr/local/libexec/freepwing/mkdirhier"

if [ $# -lt 2 ] ; then
    echo "$0: too few argument" 1>&2
    exit 1
fi

if [ $# -gt 2 ] ; then
    echo "$0: too many arguments" 1>&2
    exit 1
fi

SRCARG="`echo X$1 | sed -e 's/^X//' -e 's,//*,/,g'`"
DESTARG="`echo X$2 | sed -e 's/^X//' -e 's,//*,/,g'`"

if [ -f "${SRCARG}" ] ; then
    DESTDIR="`echo X${DESTARG} | sed -e 's/^X//' -e 's,/[^/][^/]*$,,g'`"
    ${MKDIRHIER} ${DESTDIR} || exit 1
    cp -p ${SRCARG} ${DESTARG} || exit 1
else
    if [ -d "${SRCARG}" ] ; then
	DESTDIR="`echo X${DESTARG} | sed -e 's/^X//' -e 's,/[^/]*$,,g'`"
	${MKDIRHIER} ${DESTDIR} || exit 1
        tar cf - ${SRCARG} | (cd ${DESTDIR} && tar xfp -) || exit 1
    else
	echo "$0: no such file or directory: $SRCARG" 1>&2
	exit 1
    fi
fi

exit 0
