#!/bin/bash

PATH="/usr/sbin:/usr/bin:/sbin:/bin"
me="$(basename $0)"
PACKAGE="dsc-statistics"

#set -x

logecho() {
  PVALUE="$1"
  shift
  STR="$@"
  logger -p "$PVALUE" "$PACKAGE" -- "me: $STR"
  PRE="$(echo $PVALUE | sed 's/[^\.]*\.\(.*\)/\1/' | tr 'a-z' 'A-Z')"
  if tty -s || [ "$PRE" != "INFO" ] ; then
    echo >&2 "$PRE: $STR"
  fi
}

cmd()
{
  logecho user.info "$@"
  "$@"
}

illegal-command() {
    logecho user.err "ignoring illegal command $SSH_ORIGINAL_COMMAND"
    exit 1
}

dsc-rsync-pull() {
  if ! command -v rsync >/dev/null; then
    logecho user.err "rsync not found"
  else
    if echo "$SSH_ORIGINAL_COMMAND" | grep --quiet "^rsync --server --sender -v\?logDtpre\.iLs\?f\? \(--remove-source-files \)\?\. /var/lib/dsc-statistics/[^[:space:]]\+[[:space:]]*$"; then
      cmd $SSH_ORIGINAL_COMMAND
    else
      illegal-command
    fi
  fi
}

execute-all() {
  KEY="$KEY execute-all"
  case "$SSH_ORIGINAL_COMMAND" in
    *) cmd $SSH_ORIGINAL_COMMAND;;
  esac
}

dsc-rsync-pull

