#!/bin/sh
configFile=$1
shareName=$2
pathName=$3
comment=$4
maxConnections=$5

tmp_configFile=/tmp/smb.conf.$$

umask 077

# (1) If a share specified by $shareName exists, then delete it (in a case of change/delete).

cat $configFile | awk "
  BEGIN {
    omit = 0
}

omit == 1 && /( \t)*\[.+\]/ {
    omit = 0
}

/( \t)*\[$shareName\]/ {
    omit = 1
}

{
  if (omit != 1) {
    print;
  }
}" > $tmp_configFile

cat $tmp_configFile > $configFile
rm $tmp_configFile

# (2) run by parameters except "delete share command"
#     with 4 or 5 parameters, (re)create a share specified by $shareName.

if [ $# -eq 4 ] || [ $# -eq 5 ]; then

# (2-1) If $pathName does not exist then create it
#       NOT assuming $pathName is a file!
  if [ ! -d $pathName ]; then
      mkdir -p $pathName
      chmod 775 $pathName
      chgrp domusers $pathName
  fi

  cat >> $configFile << EOF

[$shareName]
    path = $pathName
    comment = $comment
    writeable = yes
    max connections = $maxConnections
EOF
fi
