#!/bin/sh

set -e

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DEFAULTS=/etc/default/ngcp-rtpengine-recording-daemon
DESC="RTP engine recording NFS share"

. /lib/lsb/init-functions

# Load startup options if available
if [ -f "$DEFAULTS" ]; then
  . "$DEFAULTS" || true
fi

[ -z "$NFS_OPTIONS" ] && NFS_OPTIONS="hard,tcp,intr"

###

case "$1" in
  start)
    if [ "$MUST_NFS" = yes ]; then
      if ! grep -E -q "^[^ :]+:[^ :]+ $NFS_LOCAL_MOUNT nfs.? " /proc/mounts; then
        log_action_msg "Mounting $DESC"
        test -d "$NFS_LOCAL_MOUNT" || mkdir -p "$NFS_LOCAL_MOUNT"
        mount -t nfs -o "$NFS_OPTIONS" "$NFS_HOST:$NFS_REMOTE_PATH" "$NFS_LOCAL_MOUNT"
      fi
    fi
    ;;
  stop)
    if grep -E -q "^[^ :]+:[^ :]+ $NFS_LOCAL_MOUNT nfs.? " /proc/mounts; then
      log_action_msg "Unmounting $DESC"
      umount "$NFS_LOCAL_MOUNT"
    fi
    ;;
  *)
    echo "Usage: $0 {start|stop}" >&2
    exit 1
    ;;
esac

exit 0
