#!/bin/bash

# Setup ME, NOTME, and INITD vars; parse arguments; check for req dirs/files
xsync_setup() {
  ME=`basename $0`
  ME=${ME#xsync-}

  NOTME=${ME/-on/-off}
  test "$NOTME" != "$ME" || NOTME=${ME/-off/-on}
  test "$NOTME" != "$ME" || NOTME=${ME/-install/-remove}
  test "$NOTME" != "$ME" || NOTME=${ME/-remove/-install}
  test "$NOTME" != "$ME" || NOTME=

  INITD=/etc/rc.d/init.d
  test -d $INITD || INITD=/etc/init.d
  test -d $INITD || INITD=
  
  while getopts qvnd: OPT; do
    case $OPT in
      n) ECHO=1 ;;
      q) QUIET=1; VERBOSE= ;;
      v) VERBOSE=1; QUIET= ;;
      d) DIR=$OPTARG ;;
    esac
  done
  test -n "$DIR" || DIR=/etc/xsync

  test -d "$DIR" -a -d "$DIR/$ME.d" || exit 0
  echo $DIR/$ME.d/* | grep -q '*' && exit 0      # skip unless files
}

# Test for conflicts e.g. the same entry in both -off.d and -on.d
xsync_conflicts() {
  if [ -n "$NOTME" -a -f $DIR/$NOTME.d/$1 ]; then
    test -n "$ALL" || echo "WARNING: $TYPE $1 in both $ME.d and $NOTME.d - skipping"
    continue
  fi
}

# Test for a bogus ALL entry - ALL should always be expanded to a service list
xsync_check_all() {
  if [ "$1" = 'ALL' ]; then
    echo "WARNING: bogus $1 entry in $ME.d - skipping"
    continue
  fi
}

# Echo or exec the given arg list, depending on $ECHO
xsync_echo_do() {
  if [ -n "$ECHO" ]; then
    echo $*
  else
    $*
  fi
}

