#!/bin/sh

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

if [ -n "$TARGET" ]; then
    KEY_PATH=`grep "${TARGET}-KEY-PATH=" ../.DEPLOYMENT_INFO |  sed -e "s/$(echo $TARGET)-KEY-PATH=//" `
    PKG_OUTPUT="$PKG_OUTPUT/$KEY_PATH"
else
    T=$PKG_OUTPUT;
    PKG_OUTPUT=""
    KEY_LIST=`grep "ALL-KEY-PATH=" ../.DEPLOYMENT_INFO | sed 's/ALL-KEY-PATH=//'`
    for v in $KEY_LIST; do
        P="$T/$v "
        PKG_OUTPUT="$PKG_OUTPUT$P" 
    done
fi

echo "clear : [$PKG_OUTPUT]"

for v in $PKG_OUTPUT; do

    if [ -n "$EXCEPTION" ]; then
        SRC=`find $v -name "$PKG_NAME*" | grep -v $EXCEPTION`
    else
        SRC=`find $v -name "$PKG_NAME*"`
    fi
    
    CNT=`echo "$SRC" | wc -l`
    if [ $CNT -ne 1 ]; then
        echo "Two or more package exist.[$PKG_NAME]"
        exit 1
    fi
    
    rm $SRC > /dev/null 2>&1
done
