#!/bin/sh
# Copyright (C) 2004 International Business Machines Corporation and others.
# All Rights Reserved. This program and the accompanying
# materials are made available under the terms of the
# Common Public License v1.0 which accompanies this distribution.
#
# Author: Brian King <brking@us.ibm.com>
ter_afp_cfg=/usr/tos/bin/ter_afp_cfg

service_reload() {
  $ter_afp_cfg >/dev/null 2>&1
}

service_status() {
  local pid=$(pidof netatalk)
  if [ ! -z $pid ]; then
    echo "running"
  else
    echo "stopped"
  fi
}

service_prepare() {
  service_reload
  rm -fr /var/netatalk/
  mkdir -p /var/netatalk/CNID
  [ ! -d /tmp/TOS_CONFIG/zlog/ ] && mkdir -p /tmp/TOS_CONFIG/zlog/
  cp -a /etc/zlog/zlog_afpd.conf /tmp/TOS_CONFIG/zlog/
  chmod 777 /var/log/zlog/.
  /usr/sbin/terconf -c afp
}

service_start() {
  local pid=$(pidof netatalk)
  if [ ! -z "$pid" ]; then
    echo "netatalk running"
    return
  fi
  service_prepare
  start-stop-daemon --start --make-pidfile -b --pidfile /var/run/netatalk.pid --exec /usr/sbin/netatalk
}

service_stop() {
  start-stop-daemon --stop --quiet --exec /usr/sbin/netatalk
  rm -rf /var/run/netatalk.pid
}

service_restart() {
  service_stop
  service_start
}

case $1 in
start)
  service_start
  exit 0
  ;;
stop)
  service_stop
  exit 0
  ;;
restart)
  service_restart
  exit 0
  ;;
reload)
  service_reload
  exit 0
  ;;
status)
  service_status
  exit 0
  ;;
*)
  echo "Usage: $(basename $0) {start|stop|restart|reload|status}"
  exit 1
  ;;
esac
