#!/bin/sh

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

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

. $curDir/dm_incl
. $curDir/dm_text
. $curDir/dm_remote

# ---------------------------------------------------------------------------- #
#
# Copy a drawing directory from another designer or from the releases area
# to the designer area; owner = login
#
# Parameters:
#           input : - area type ('D' / 'R')
#                   - area ('login' / 'R')
#                   - project (without suffix) or "-1"
#                   - drawing name or partNumberVersion (without suffix)
#                   - file which should exists
#                   - file which should not exists
#           output: - error code ("0": ok; "1"/"2": error)
#                   - path of drawing (to) or error message
#                   - basename of drawing directory
#
# 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=6
if test $# -ne $nbParam
then
    (eval "echo $ParameterNotValid") > $RETURN_FILE
    exit 1
fi

# ---------------------------------------------------------------------------- #
# Set parameters to variables
# ---------------------------------------------------------------------------- #
areaType=$1
area=$2
project=$3
drawName=$4
existFile=$5
notExitFile=$6
fromHost=false

# ---------------------------------------------------------------------------- #
# Copy from release area ?
# ---------------------------------------------------------------------------- #
if ( test "$areaType" = $RELEASE_AREA )
then

    projPath=$RELEASES_PATH/$project$PRO_SUFF

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

    # ------------------------------------------------------------------------ #
    # Exists drawing (from) ?
    # ------------------------------------------------------------------------ #
    drawPath=$projPath"/"$drawName$REL_SUFF
    if ( test ! -d $drawPath )
    then
        (eval "echo $DrawingNotExists" ) > $RETURN_FILE
        exit 1
    fi
    if ( test ! -r $drawPath )
    then
        (eval "echo $DrawingNoAccess" ) > $RETURN_FILE
        exit 1
    fi

    # ------------------------------------------------------------------------ #
    # Exists file (from) ?
    # ------------------------------------------------------------------------ #
    aFile=$drawPath"/"$existFile
    if ( test ! -r $aFile )
    then
        anArcFile=$drawPath"/"$drawName$ARC_SUFF
        if ( test -r $anArcFile )
        then
            (eval "echo $DrawingArchived" ) > $RETURN_FILE
            exit 1
        else
            (eval "echo $DrawingNotExists" ) > $RETURN_FILE
            exit 1
        fi
    fi

else

    designer=$area
    # ------------------------------------------------------------------------ #
    # Designer in user file
    # ------------------------------------------------------------------------ #
    for i in `awk ' { print \$1 } ' $USER_FILE `
    do
        if ( test "$i" = "$designer" ); then found=$YES; break; fi
    done   

    if ( test ! "$found" = $YES )
    then
        (eval "echo $DesignerNotInUser") > $RETURN_FILE
        exit 1
    fi

    nb=`awk "{ if ( \"$designer\" == \\$1 ) print NF }" $USER_FILE`

    # ------------------------------------------------------------------------ #
    # If local
    # ------------------------------------------------------------------------ #
    if ( test $nb -eq 2 ) 
    then
        userHome=`awk "{ if ( \"$designer\" == \\$1 ) print \\$2 } " $USER_FILE`
        desPath=$userHome"$DESIGNS_PATH"

        # -------------------------------------------------------------------- #
        # Exists designer area ?
        # -------------------------------------------------------------------- #
        if ( test ! -d $desPath )
        then
            (eval "echo $DesAreaNotExists") > $RETURN_FILE
            exit 1
        fi

        # -------------------------------------------------------------------- #
        # Exists drawing (from) ?
        # -------------------------------------------------------------------- #
        drawPath=$desPath"/"$drawName$DES_SUFF
        if ( test ! -d $drawPath )
        then
            (eval "echo $DrawingNotExists" ) > $RETURN_FILE
            exit 1
        fi
        if ( test ! -r $drawPath )
        then
            (eval "echo $DrawingNoAccess" ) > $RETURN_FILE
            exit 1
        fi

        # ------------------------------------------------------------------------ #
        # Exists file (from) ?
        # ------------------------------------------------------------------------ #
        aFile=$drawPath"/"$existFile
        if ( test ! -r $aFile )
        then
            (eval "echo $DrawingNotExists" ) > $RETURN_FILE
            exit 1
        fi

        # ------------------------------------------------------------------------ #
        # Exists not file (from) ?
        # ------------------------------------------------------------------------ #
        aFile=$drawPath"/"$notExitFile
        if ( test -r $aFile )
        then
            (eval "echo $DrawingLocked" ) > $RETURN_FILE
            exit 1
        fi

    # ------------------------------------------------------------------------ #
    # else on host
    # ------------------------------------------------------------------------ #
    else

        userNode=`awk "{ if ( \"$designer\" == \\$1 ) print \\$2 }" $USER_FILE`
        userHome=`awk "{ if ( \"$designer\" == \\$1 ) print \\$3 }" $USER_FILE`

        desPath=$userHome"$DESIGNS_PATH"

        # -------------------------------------------------------------------- #
        # Exists designer area ?
        # -------------------------------------------------------------------- #
        drawPath=$desPath"/"$drawName$DES_SUFF
        cmd="if ( test ! -d $desPath ); then echo 1; else echo 0; fi"
        ret=`$RSH $userNode $cmd`
        if ( test $ret -eq 1 )
        then
            (eval "echo $DesAreaNotExists") > $RETURN_FILE
            exit 1
        fi
    fi

fi

fromPath=$drawPath
desPath=$HOME/$DESIGNS_PATH

# --------------------------------------------------------------------------- #
# Exists drawing (to) ?
# --------------------------------------------------------------------------- #
drawPath=$desPath"/"$drawName$DES_SUFF
if ( test -d $drawPath )
then
    (eval "echo $DrawingExists" ) > $RETURN_FILE
    exit 1
fi

# --------------------------------------------------------------------------- #
# Create drawing directory and copy files from source to target
# --------------------------------------------------------------------------- #
toPath=$drawPath
mkdir $toPath
chmod 755 $toPath

ret="notok"
if ( test $userNode )
then

    if ( $RCP -r $userNode":"$fromPath/* $toPath )
    then
        ret="ok"
    fi

else

    if ( cp -r $fromPath/* $toPath )
    then
        ret="ok"
    fi

fi

if ( test "$ret" != "ok" )
then 
    (eval "echo $ErrCopyDrawing") > $RETURN_FILE
    find $toPath -exec chmod 777 {} \;
    rm -rf $toPath
    exit 1
fi

find $toPath -exec chmod 755 {} \;

# --------------------------------------------------------------------------- #
# Return 'ok'
# --------------------------------------------------------------------------- #
echo "0 $fromPath $toPath $drawName$DES_SUFF" > $RETURN_FILE
