#!/bin/bash
#create oem script
ADD_PARM="-a"
EDIT_PARM="-e"
DEL_PARM="-d"
HOST_PARM="-h"
HOST_NAME=""
INFO=""
PORT=8443
ShowExample()
{
  echo -e "Add  oem example:\033[31;49;5;1m bash oat_oem -a -h HOSTNAME_OF_OAT-APPRAISER '{"Name":"OEM_NAME","Description":"DESCRIPTION"}'\033[0m"
  echo -e  "Edit oem example:\033[31;49;5;1m bash oat_oem -e -h HOSTNAME_OF_OAT-APPRAISER '{"Name":"OEM_NAME","Description":"DESCRIPTION"}'\033[0m"
  echo -e "Del  oem example:\033[31;49;5;1m bash oat_oem -d -h HOSTNAME_OF_OAT-APPRAISER '{"Name":"OEM_NAME"}'\033[0m"
}

if [ $# -lt 4 ];then
  echo "parm num error!"
  ShowExample
  exit 0
fi

if [ $2 != $HOST_PARM ];then
  echo "-h parm error!"
  ShowExample
  exit 1
else
  HOST_NAME="$3"
fi

if [ $1 == $DEL_PARM ];then
  INFO="`echo "$4" | awk -F{ '{print $2}'| awk -F} '{print $1}'\
        | sed 's/\"//g' | sed 's/:/=/g' | sed 's/ /%20/g'`"
else
  INFO="$4"
fi

addOEM() 
{
  curl --cacert certfile.cer -H "Content-Type: application/json" \
       -X POST -d "$INFO" "https://$HOST_NAME:$PORT/WLMService/resources/oem" -ssl3
}

editOEM()
{
  curl --cacert certfile.cer -H "Content-Type: application/json" \
        -X PUT -d "$INFO" "https://$HOST_NAME:$PORT/WLMService/resources/oem" -ssl3
}

delOEM()
{
  curl --cacert certfile.cer \
        -X DELETE  "https://$HOST_NAME:$PORT/WLMService/resources/oem?$INFO"  -ssl3
}

case $1 in
      $ADD_PARM)
           echo "add OEM:"
           addOEM
      ;;
      $EDIT_PARM)
           echo "edit OEM:"
           editOEM
      ;;
      $DEL_PARM)
           echo "del OEM:"
           delOEM
      ;;
      *)
      echo "Usage oat_oem {-a|-e|-d}"
      ShowExample
      exit 3
esac
echo -e "\n"
