#!/bin/sh
if [ $# = 0 ] || ( [ $# = 1 ] && [ "x$1" = "x-h" ] )
then
 echo "Usage: mactrashblock [-d/b] directory [directory ...]"
 echo "Options (not recommended; only use if sure of what you're doing):"
 echo "  -d    Delete cruft files only; don't add files to block future cruft."
 echo "  -b    Add cruft blocking files only; don't delete existing cruft."
 echo "Note: since these options are mutually exclusive, -db/-bd and any -b or"
 echo "-d following the first parameter will be read as directory names".
else
 for argument in "$@"
 do
  if [ "$option" = "" ]
  then
   if [ "x$1" = "x-d" ]
   then
    if [ $# = 1 ]
    then
     echo "Need at least one directory parameter."
     exit
    else
     option=1
    fi
   elif [ "x$1" = "x-b" ]
   then
    if [ $# = 1 ]
    then
     echo "Need at least one directory parameter."
     exit
    else
     option=2
    fi
   else
    option=0
   fi
  fi
  
  if [ -d "$argument" ]
  then
   if [ "$option" != "2" ]
   then
    rm -rf "$argument/.fseventsd" "$argument/.Trashes" "$argument/.Spotlight*" "$argument/._*"
   fi 
   if [ "$option" != "1" ]
   then
    mkdir "$argument/.fseventsd"
    touch "$argument/.fseventsd/no_log" "$argument/.Trashes" "$argument/.metadata_never_index"
   fi 
  else
   if [ "$option" = "0" ] || [ "x$argument" != "x$1" ]
   then
    echo $argument is not a directory.
   fi
  fi
 done
fi
