#!/bin/sh

PATH=/bin:/usr/bin:$PATH; export PATH

curDir=`dirname $0`
procName=`basename $0`

. $curDir/dm_incl
. $curDir/dm_text

# ---------------------------------------------------------------------------- #
#
# Export an drawing from path.
#
# Parameter:
#           input : - path
#                   - project (without Suffix)
#                   - partNb.Version (without Suffix) 
#                   - delFlag ( delete path after exporting ('y'/'n')
#           output: - Error code ("0": ok; "1": error)
#                   - Error message
#
# Author : J. Schmidt, AP221, 11.91
#
# Achtung: Die Prozeduren koennen vom Anwender auf firmenspezifische
#          Gegebenheiten angepasst werden.
#          Bei solchen Aenderungen uebernimmt SNI jedoch keine Gewaehr-
#          leistung fuer den ordnungsgemaessen Ablauf der Prozeduren.
#          Fehler werden nur beruecksichtigt, wenn sie in den ausge-
#          lieferten Prozeduren nachvollzogen werden koennen.
#
# ---------------------------------------------------------------------------- #

set -e
# ---------------------------------------------------------------------------- #
# Set error if a command fails
# ---------------------------------------------------------------------------- #
if [ ! -w $RETURN_FILE ]
then
    touch $RETURN_FILE
    chmod 777 $RETURN_FILE
fi
(eval "echo $ErrCommand") > $RETURN_FILE

# ---------------------------------------------------------------------------- #
# Check parameters
# ---------------------------------------------------------------------------- #
nbParam=4
if test $# -lt $nbParam
then
    (eval "echo $ParameterNotValid") > $RETURN_FILE
    exit 1
fi

# ---------------------------------------------------------------------------- #
# Set parameters to variables
# ---------------------------------------------------------------------------- #
apath=$1
project=$2
partNbVer=$3
delFlag=$4


projPath=$RELEASES_PATH/$project$PRO_SUFF
# ------------------------------------------------------------------------ #
# Exists project ?
# ------------------------------------------------------------------------ #
if ( test ! -d $projPath )
then
    (eval "echo $ProjectNotExists") > $RETURN_FILE
    exit 1
fi

# ------------------------------------------------------------------------ #
# Exists partnumber and version
# ------------------------------------------------------------------------ #
partNbPath=$projPath"/"$partNbVer$REL_SUFF
if ( test ! -d $partNbPath )
then
    (eval "echo $PartNbNotExists") > $RETURN_FILE
    exit 1
fi 

cd $apath

# ------------------------------------------------------------------------ #
# Remove old content
# ------------------------------------------------------------------------ #
fromPath=$project$PRO_SUFF/$partNbVer$REL_SUFF
if ( test -d $fromPath )
then
    (rm -rf $fromPath)
fi

# ------------------------------------------------------------------------ #
# Read from tape
# ------------------------------------------------------------------------ #
#tar -xvf /dev/rct8 $fromPath
(eval "echo $FunctionNotCustomized") > $RETURN_FILE
exit 1 

# ------------------------------------------------------------------------ #
# an example with 'crp' on ws30; see also /usr/sigraph/cad/design/dm_import
# notice: if you modify this or the called procedure, you must always
#         guarantee, that the called procedure returns a value 'ret'
#         which indicates if the export was faultless
# ------------------------------------------------------------------------ #
#ret=`$curDir/dm_import $fromPath`

# ------------------------------------------------------------------------ #
# Read from tape ok ?
# ------------------------------------------------------------------------ #
if ( test $ret -ne 0 )
then
    (eval "echo $ErrReadingTape") > $RETURN_FILE
    exit 1
fi

if ( test ! -d $apath/$project$PRO_SUFF/$partNbVer$REL_SUFF )
then
    (eval "echo $ErrReadingTape") > $RETURN_FILE
    exit 1
fi

# ------------------------------------------------------------------------ #
# Copy data to the release area
# ------------------------------------------------------------------------ #
find $partNbPath -exec chmod 755 {} \;
cp -r $fromPath/* $partNbPath
find $partNbPath -exec chmod 555 {} \;
chmod 755 $partNbPath

# ------------------------------------------------------------------------ #
# if delFlag = 'y', remove archiv-file
# ------------------------------------------------------------------------ #
if ( test "$delFlag" = $YES )
then
    cd $apath
    rm -r $apath/$project$PRO_SUFF/$partNbVer$REL_SUFF
fi

echo "0 " > $RETURN_FILE
