#!/bin/sh

#####################################################################
copy()
{
    USE_COPY=$1
    TARGET=$2
    KEY_PATH=`grep "${TARGET}-KEY-PATH=" ../.DEPLOYMENT_INFO |  sed -e "s/$(echo $TARGET)-KEY-PATH=//" `
    PKG_OUTPUT2="$3/$KEY_PATH"
    PKG_NAME=$4

    SERVER=`grep "$(echo $TARGET)=" ../.DEPLOYMENT_INFO | sed "s/$(echo $TARGET)=//"`

	# 2020-12-28 FreeBSD 12.2 code add (#33367)
	OS=`uname -r | cut -c1-2 | bc`
	if [ $OS -ge 12 ]; then
		OUT_PATH_TEMP=`grep "$(echo $TARGET)-PATH=" ../.DEPLOYMENT_INFO | sed "s/$(echo $TARGET)-PATH=//"`
		OUT_PATH="$OUT_PATH_TEMP/FB$OS"
	else
		OUT_PATH=`grep "$(echo $TARGET)-PATH=" ../.DEPLOYMENT_INFO | sed "s/$(echo $TARGET)-PATH=//"`
	fi

    OUT_OLD="$OUT_PATH/.old"
    PKG_KEY_EXCEPTION=`grep "$(echo $TARGET)-KEY-EXCEPTION=" ../.DEPLOYMENT_INFO |  sed -e "s/$(echo $TARGET)-KEY-EXCEPTION=//" `
    
    $USE_COPY "$PKG_OUTPUT2" "$PKG_NAME" "$SERVER" "$OUT_PATH" "$OUT_OLD" "$PKG_KEY_EXCEPTION"
}
#####################################################################

USE_COPY=$1
PKG_OUTPUT=$2
PKG_NAME=$3
TARGET=$4

echo "#### copy2output $USE_COPY,$PKG_OUTPUT,$PKG_NAME,$TARGET"
if [ -n "$TARGET" ]; then
    copy "$USE_COPY" "$TARGET" "$PKG_OUTPUT" "$PKG_NAME"
else
    KEY_LIST=`grep "ALL-KEY-PATH=" ../.DEPLOYMENT_INFO | sed 's/ALL-KEY-PATH=//'`
    for v in $KEY_LIST; do
        copy "$USE_COPY" "$v" "$PKG_OUTPUT" "$PKG_NAME"
    done
fi

