
#!/bin/sh
# File	: cdot_create_shares_onto_dp-volumes.sh
# By	: Maarten.deBoer@Atos.net, 160108
# Subject	: Script to create shares onto DP-volumes
#(0.2)	: Add check if share exist
PGM="`basename $0|cut -d\. -f1`"
VER="0.2"
TMP="/tmp/${TMP}.$$"
LOG="${HOME}/log/${PGM}.log"
SSH="/usr/bin/ssh -n"
ASC="/tmp/${PGM}.asc"

MAILTO="maarten.deboer@atos.net"

CLUSTER="nlnaf103"
#CLUSTER="nlnaf103-02"
SVM="nlnafsmnp41"

echo "`date`: ${PGM} v${VER} started"|tee -a ${LOG}
touch ${TMP}

${SSH} ${CLUSTER} "volume show -vserver ${SVM} -type DP"|grep ${SVM}| while read VSERVER VOL TYPE REST
do
  JPATH="`${SSH} ${CLUSTER} "volume show -vserver ${SVM} -volume ${VOL} -field junction-path "|grep ' - '`"
# If JPATH <> " - ", no junctoin=path; then create junction-path
  echo ${JPATH}
  if [ "${JPATH}" != "" ]; then
    echo "  Create junction-path for ${VOL}"|tee -a ${LOG}
    ${SSH} ${CLUSTER} "volume mount -vserver ${SVM} -volume ${VOL} -junction-path /${VOL}"
  fi  # JPATH

  
  ${SSH} ${CLUSTER} "qtree show -vserver ${SVM} -volume ${VOL} -fields qtree,security-style"|grep ntfs|grep -v \"| while read VSERVER2 VOLUME QTREE STYLE REST
  do
    echo "${VSERVER2} ${VOLUME} ${QTREE} ${STYLE} ${REST}"
    ${SSH} ${CLUSTER} "share show -vserver ${SVM} -share-name bck-${QTREE}$ -field share-name"
    EC="$?"
    # If not OK (EC<>0) then share not there & need to be made
    if [ ${EC} -ne 0 ]; then

      echo "  Create share bck-${QTREE}$ onto /${VOL}/${QTREE} "|tee -a ${LOG}
      ${SSH} ${CLUSTER} "share create -vserver ${SVM} -share-name bck-${QTREE}$ -path /${VOL}/${QTREE} -share-properties oplocks,browsable,changenotify"

      echo "  Mod share bck-${QTREE}$ "|tee -a ${LOG}
      ${SSH} ${CLUSTER} "cifs share access-control create -vserver ${SVM} -share bck-${QTREE}$ -user-or-group \"Authenticated Users\" -permission Full_Control "
      ${SSH} ${CLUSTER} "cifs share access-control delete -vserver ${SVM} -share bck-${QTREE}$ -user-or-group Everyone "
    fi   # EC
  done  # qtree show -vserver
  echo ""
done  # volume show -vserver

${SSH} ${CLUSTER} "share show -vserver ${SVM} -share-name  bck* -field share-name,share-properties,path,acl" > ${ASC}
date|mailx -a ${ASC} -s "[${PGM} v${VER}]" ${MAILTO}




rm ${TMP} ${ASC}
echo "`date`: ${PGM} v${VER} finished"|tee -a ${LOG}
exit 0

