
#!/bin/sh
# File	: set_cifs_ro.sh
# By	: Maarten.deBoer@Atos.net, 191017
# Subject	: Script to set a CIFS-share to RO
PGM=`basename $0|cut -d\. -f1`
VER="0.1"
TMP="/tmp/${PGM}.$$"
LOG="${HOME}.log"
ETC="${HOME}/etc/${PGM}.shares"
SSH="/usr/bin/ssh"
DATADIR="${HOME}/data/${PGM}"
DATI=`date +%Y%d%m-%H%M%S`

if [ ! -f ${ETC} ]; then
  echo "  Input-file (${ETC}) NOT found. Exiting..."
  exit 2
fi
if [ ! -d ${DATADIR} ]; then
  echo "  DATA-dir (${DATADIR}) not available. Please create. Exiting..."
  exit 3
fi

echo "  Saving shares data first (into data/${PGM}/<filer>_<vfiler>_${DATI}) ..."
cat ${ETC}|grep -v ^#|awk '{print $1}'|sort -u|while read FILER
do
  cat ${ETC}|grep -v ^#|awk '{print $2}'|sort -u|while read VFILER
  do
    echo "  ${FILER}/${VFILER} ..."
    VSTATUS=`${SSH} ${FILER} "vfiler status ${VFILER}"|awk '{print $2}'`
    if [ "${VSTATUS}" != "running" ]; then
      echo "  ${FILER}/${VFILER} NOT running. Exiting ..."
      exit 4
    else
      ${SSH} ${FILER} "vfiler run -q ${VFILER} cifs shares" > ${DATADIR}/${FILER}_${VFILER}_${DATI}.shares-save
#      ls -l ${DATADIR}/${FILER}_${VFILER}_${DATI}.exports-save
    fi
  done  # VFILER
done  # FILER

# A 3 round (FILER, VFILER, EXPORTS) is needed
cat ${ETC}|grep -v ^#|awk '{print $1}'|sort -u|while read FILER
do
  cat ${ETC}|grep -v ^#|awk '{print $2}'|sort -u|while read VFILER
  do
    echo "  ${FILER}/${VFILER} ..."
    VSTATUS=`${SSH} ${FILER} "vfiler status ${VFILER}"|awk '{print $2}'`
    if [ "${VSTATUS}" != "running" ]; then
      echo "  ${FILER}/${VFILER} NOT running. Exiting ..."
      exit 4
    else
      cat ${ETC}|grep -v ^#|grep ${FILER}|awk '{print $3}'|while read SHARE REST
      do
        echo "|${FILER}|${VFILER}|${SHARE}|"
        ${SSH} ${FILER} "vfiler run -q ${VFILER} cifs access ${SHARE} S-1-5-11 Read"
      done  # LINE
    fi  # VSTATUS

  done  # VFILER

done # FILER

echo "  Checking LIVE at Vfilers ..."
cat ${ETC}|grep -v ^#|while read LINE
do
  FILER=`echo ${LINE}|awk '{print $1}'`
  VFILER=`echo ${LINE}|awk '{print $2}'`
  SHARE=`echo ${LINE}|awk '{print $3}'`
  echo "  ${FILER}/${VFILER}:${SHARE} ... "
  VSTATUS=`${SSH} -n ${FILER} "vfiler status ${VFILER}"|awk '{print $2}'`
  if [ "${VSTATUS}" != "running" ]; then
    echo "  ${FILER}/${VFILER} NOT running. Exiting ..."
    exit 4
  else
#  cat ${DATADIR}/${FILER}_${DATI}.exports-new|grep ${EXPORT}
    ${SSH} -n ${FILER} "vfiler run -q ${VFILER} cifs shares ${SHARE}"
  fi  # VSTATUS
done  # cat ETC


exit 0

