#!/bin/bash

if [ x`whoami` != xroot ]; then
  echo Error: must be run as root user
  exit 1
fi

usage() {
  echo "$0 [-help] [-verbose]"
  echo "-help     Usage"
  echo "-verbose  Verbose output"
  echo
  exit
}

verbose=no

for opt in "$@"; do
  case "$opt" in
    -verbose|--verbose|-v)
      verbose=yes
      shift
      ;;
    -help|--help|-h|--h)
      usage
      shift
      ;;
    *)
      echo "Unknown option $opt"
      usage
      shift
      ;;
  esac
done

print() {
  if [ $verbose = "yes" ]; then
    echo $*
  fi
}

SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
MSH_HOME=`realpath $SCRIPT_DIR/..`

source "$MSH_HOME/lib/defaults.cf"
[ -f "$MSH_HOME/etc/config.cf" ] && source "$MSH_HOME/etc/config.cf"

chown -R $MSH_USER:$MSH_USER $MSH_HOME
find $MSH_HOME -type d | xargs chmod 750
find $MSH_HOME -type f | xargs chmod 640

chmod 750 $MSH_HOME/bin/msh-pm-milter
chmod 750 $MSH_HOME/bin/msh-pm-settings
chmod 750 $MSH_HOME/lib/fixperms.sh
