
#!/bin/sh
# File	: omi_proc.sh
# By	: Maarten.deBoer@Atos.net, 240826
# Subject	: Script to process OMI-change CHG002744997
#
PGM=`basename $0|cut -d\. -f1`
VER="0.1"
LOG="${HOME}/log/${PGM}.log"
SSH="/usr/bin/ssh -n"

CLUSTER="nlnaf102"
SVM_SUFFIX="mss12"

IN_FILE="omi_CHG002744997"
#CLUSTER="nlnaf102"
#SVM_SUFFIX="omi54"
#CLUSTER="nlnaf102"
#SVM_SUFFIX="omi56"
#CLUSTER="nlnaf103"
#SVM_SUFFIX="omi53"

echo "`date` ${PGM} v${VER} started."|tee -a ${LOG}
echo "  IN_FILE=${IN_FILE}"|tee -a ${LOG}
echo "  SVM_SUFFIX=${SVM_SUFFIX}"|tee -a ${LOG}

cat ${IN_FILE}|grep -v ^#|grep "${SVM_SUFFIX}"|cut -d\: -f2|sort -u|while read LINE
do
  echo "LINE=${LINE}"
  VSERVER="nlnafs${SVM_SUFFIX}"
  VOLUME=`echo "${LINE}"|cut -d\/ -f2`
  QTREE=`echo "${LINE}"|cut -d\/ -f3`
  EXPORT=`${SSH} ${CLUSTER} "set -showseparator \";\" ; qtree show -vserver ${VSERVER} -volume ${VOLUME} -qtree ${QTREE} -field export"|grep "${QTREE}"|awk -F\; '{print $4}'|head -1`

  if [ "${EXPORT}" != "" ]; then
    echo "VSERVER=${VSERVER} VOLUME=${VOLUME} QTREE=${QTREE} EXPORT=${EXPORT}"


    ${SSH} ${CLUSTER} "export-policy rule show -vserver ${VSERVER} -policyname \"${EXPORT}\" -ruleindex 14 -protocol nfs -clientmatch 10.71.115.74 -ro sys -rw never -super sys -fields policyname,ruleindex,protocol,clientmatch,rorule,rwrule,superuser"
    EC=${?}
    echo "EC=${EC}"
    if [ ${EC} -gt 0 ]; then
      ${SSH} ${CLUSTER} "export-policy rule create -vserver ${VSERVER} -policyname \"${EXPORT}\" -ruleindex 14 -protocol nfs -clientmatch 10.71.115.74 -rorule sys -rwrule never -superuser sys"
    fi

    ${SSH} ${CLUSTER} "export-policy rule show -vserver ${VSERVER} -policyname \"${EXPORT}\" -ruleindex 15 -protocol nfs -clientmatch 10.71.115.75 -ro sys -rw never -super sys -fields policyname,ruleindex,protocol,clientmatch,rorule,rwrule,superuser"
    EC=${?}
    echo "EC=${EC}"
    if [ ${EC} -gt 0 ]; then
      ${SSH} ${CLUSTER} "export-policy rule create -vserver ${VSERVER} -policyname \"${EXPORT}\" -ruleindex 15 -protocol nfs -clientmatch 10.71.115.75 -rorule sys -rwrule never -superuser sys"
    fi

    ${SSH} ${CLUSTER} "export-policy rule show -vserver ${VSERVER} -policyname \"${EXPORT}\" -ruleindex 16 -protocol nfs -clientmatch 10.71.115.76 -ro sys -rw never -super sys -fields policyname,ruleindex,protocol,clientmatch,rorule,rwrule,superuser"
    EC=${?}
    echo "EC=${EC}"
    if [ ${EC} -gt 0 ]; then
      ${SSH} ${CLUSTER} "export-policy rule create -vserver ${VSERVER} -policyname \"${EXPORT}\" -ruleindex 16 -protocol nfs -clientmatch 10.71.115.76 -rorule sys -rwrule never -superuser sys"
    fi

    ${SSH} ${CLUSTER} "export-policy rule show -vserver ${VSERVER} -policyname \"${EXPORT}\" -ruleindex 17 -protocol nfs -clientmatch 10.71.115.77 -ro sys -rw never -super sys -fields policyname,ruleindex,protocol,clientmatch,rorule,rwrule,superuser"
    EC=${?}
    echo "EC=${EC}"
    if [ ${EC} -gt 0 ]; then
      ${SSH} ${CLUSTER} "export-policy rule create -vserver ${VSERVER} -policyname \"${EXPORT}\" -ruleindex 17 -protocol nfs -clientmatch 10.71.115.77 -rorule sys -rwrule never -superuser sys"
    fi

#    echo "VSERVER=${VSERVER} VOLUME=${VOLUME} QTREE=${QTREE} EXPORT=${EXPORT}"|tee -a ${LOG}
    ${SSH} ${CLUSTER} "export-policy rule show -vserver ${VSERVER} -policyname \"${EXPORT}\" -protocol nfs -clientmatch 10.71.115.* -ro sys -rw never -super sys -fields policyname,ruleindex,protocol,clientmatch,rorule,rwrule,superuser"|tee -a ${LOG}

  fi  # EXPORT<>""

done  # LINE


echo "`date` ${PGM} v${VER} finished."|tee -a ${LOG}
exit 0

