#!/bin/bash
#
# mark/unmark volume
#  
# usage:
#        sm_mark mark   <mount_point>  <ver> <path/to/conf>
#        sm_mark unmark <mount_point>
# env:   APL_USR & APL_HTTPD_GRP are used

cmd=$1
ver=$2
cfg=$3
own=$APL_USR:$APL_HTTPD_GRP
mnt=/vasm/probe
#---------------------------- check arguments
case "$cmd" in
 unmark) ;;
 restore|mark)
         [ -z "$ver" ] && echo 'FAIL: version is expected' && exit 1
         [ -z "$cfg" ] && echo 'FAIL: config  is expected' && exit 1
         [ ! -f $cfg ] && echo 'FAIL: config  is absent'   && exit 1
         ;;
      *) echo 'FAIL: Wrong usage'; exit 1
esac
[ -z "$mnt" ] && echo 'FAIL: mount-point is expected' && exit 1
df $mnt 2>/dev/null | egrep -sq "$mnt$"
[ $? != 0 ] && echo 'FAIL: mount-point is incorrect'  && exit 1

#---------------------------- ACTION
if [ $cmd = 'restore' ]; then # restore mark after restoring backup
   cat $cfg >$mnt/.vasm-info || {
       echo "FAIL: cannot create $mnt/.vasm-info"
       exit 1
 }
 echo 'SUCCESS'
 exit 0
fi;

if [ $cmd = 'mark' ]; then  # create actual mark and ver  
 # TBD probably should be a check if mark alredy exists
 cat $cfg >$mnt/.vasm-info || {
       echo "FAIL: cannot create $mnt/.vasm-info"
       exit 1
 }
 mkdir  $mnt/$ver
 [ ! -d "$mnt/$ver" ] && echo "FAIL: cannot create dir $mnt/$ver" && exit 1
 chown $own $mnt/$ver
 chmod 750  $mnt/$ver
 if [ -d $mnt/.Spotlight-V100/Store-V1/ ]; then # disable spotlight
    cat $APL/sm/etc/slight.conf > $mnt/.Spotlight-V100/Store-V1/Exclusions.plist
    chmod a+r  $mnt/.Spotlight-V100/Store-V1/Exclusions.plist
 fi
else                        # rename .vasm-info .vasm-info.deleted
 mv -f $mnt/.vasm-info $mnt/.vasm-info.deleted 2>/dev/null
 [ -f $mnt/.vasm-info ] && "FAIL: cannot remove $mnt/.vasm-info" && exit 1
fi
echo 'SUCCESS'
exit 0
