view scripts/mapsearch.sysvinit @ 2490:4f2cda3224f8

Sync mapsearch scripts.
author Matti Hamalainen <ccr@tnsp.org>
date Tue, 18 Apr 2023 12:49:10 +0300
parents 9c3324feef68
children 72a3b687e6ce
line wrap: on
line source

#! /bin/sh

### BEGIN INIT INFO
# Provides:		mapsearch
# Required-Start:	$remote_fs
# Required-Stop:	$remote_fs
# Default-Start:	2 3 4 5
# Default-Stop:		
# Short-Description:	PupuMaps map search server
### END INIT INFO

# SysV init script for MapSearch

set -e

MAPSEARCH_CFG="/etc/default/mapsearch"
MAPSEARCH_BIN="/usr/sbin/mapsearch"
MAPSEARCH_PIDFILE="/var/run/mapsearch.pid"


if test ! -x "$MAPSEARCH_BIN"; then
  echo "$MAPSEARCH_BIN does not exist or is not executable."
  exit 0
fi

umask 022

if test -f "$MAPSEARCH_CFG"; then
  . "$MAPSEARCH_CFG"
else
  echo "$MAPSEARCH_CFG does not exist."
  exit 0
fi

if test "x$MAPSEARCH_DIR" = "x"; then
  echo "MAPSEARCH_DIR not configured."
  exit 0
fi

if test ! -d "$MAPSEARCH_DIR"; then
  echo "$MAPSEARCH_DIR does not exist."
  exit 0
fi

. /lib/lsb/init-functions

THLIBS_DIR="${MAPSEARCH_DIR}/th-libs/"

update_map_data()
{
  if test ! -d "$MAPSEARCH_DIR/.hg"; then
    hg clone "$MAPSEARCH_REPO" "$MAPSEARCH_DIR"
  else
    cd "$MAPSEARCH_DIR" && hg pull && hg update
  fi
}


update_daemon()
{
  if test ! -d "$THLIBS_DIR/.hg"; then
    hg clone "$THLIBS_REPO" "$THLIBS_DIR"
  fi
  cd "$THLIBS_DIR" && hg pull && hg update
  cd "$MAPSEARCH_DIR" && make clean && make "bin/mapsearch" -j4 && cp "bin/mapsearch" "$MAPSEARCH_BIN"
}


MAPSEARCH_ARGS=""
for SPEC in ${MAPSEARCH_MAPLIST}; do
  MAP=$(echo "$SPEC"|cut -d : -f 1)
  WXC=$(echo "$SPEC"|cut -d : -f 2)
  WYC=$(echo "$SPEC"|cut -d : -f 3)
  MAPSEARCH_ARGS="$MAPSEARCH_ARGS ${MAPSEARCH_DIR}/world/${MAP}.map:${MAPSEARCH_DIR}/world/${MAP}.loc:${MAP}:$WXC:$WYC"
done


#export PATH="${PATH:+$PATH:}/usr/sbin:/sbin"

case "$1" in
  start)
  	update_map_data
	log_daemon_msg "Starting PupuMaps map search server" "mapsearch" || true
	if start-stop-daemon --start --quiet --oknodo -m --background --pidfile "$MAPSEARCH_PIDFILE" --exec "$MAPSEARCH_BIN" -- $MAPSEARCH_ARGS $MAPSEARCH_OPTS; then
	    log_end_msg 0 || true
	else
	    log_end_msg 1 || true
	fi
	;;
  stop)
	log_daemon_msg "Stopping PupuMaps map search server" "mapsearch" || true
	if start-stop-daemon --stop --quiet --oknodo --remove-pidfile --pidfile "$MAPSEARCH_PIDFILE"; then
	    log_end_msg 0 || true
	else
	    log_end_msg 1 || true
	fi
	;;

  reload|force-reload)
  	update_map_data
	log_daemon_msg "Reloading PupuMaps map search server's configuration" "mapsearch" || true
	if start-stop-daemon --stop --signal 1 --quiet --oknodo --remove-pidfile --pidfile "$MAPSEARCH_PIDFILE" --exec "$MAPSEARCH_BIN"; then
	    log_end_msg 0 || true
	else
	    log_end_msg 1 || true
	fi
	;;

  restart)
  	update_map_data
	log_daemon_msg "Restarting PupuMaps map search server" "mapsearch" || true
	start-stop-daemon --stop --quiet --oknodo --retry 30 --remove-pidfile --pidfile "$MAPSEARCH_PIDFILE"
  	update_daemon
	if start-stop-daemon --start --quiet --oknodo -m --background --pidfile "$MAPSEARCH_PIDFILE" --exec "$MAPSEARCH_BIN" -- $MAPSEARCH_ARGS $MAPSEARCH_OPTS; then
	    log_end_msg 0 || true
	else
	    log_end_msg 1 || true
	fi
	;;

  try-restart)
  	update_map_data
	log_daemon_msg "Restarting PupuMaps map search server" "mapsearch" || true
	RET=0
	start-stop-daemon --stop --quiet --retry 30 --remove-pidfile --pidfile "$MAPSEARCH_PIDFILE" || RET="$?"
  	update_daemon
	case $RET in
	    0)
		# old daemon stopped
		if start-stop-daemon --start --quiet --oknodo -m --background --pidfile "$MAPSEARCH_PIDFILE" --exec "$MAPSEARCH_BIN" -- $MAPSEARCH_ARGS $MAPSEARCH_OPTS; then
		    log_end_msg 0 || true
		else
		    log_end_msg 1 || true
		fi
		;;
	    1)
		# daemon not running
		log_progress_msg "(not running)" || true
		log_end_msg 0 || true
		;;
	    *)
		# failed to stop
		log_progress_msg "(failed to stop)" || true
		log_end_msg 1 || true
		;;
	esac
	;;

  status)
	status_of_proc -p "$MAPSEARCH_PIDFILE" "$MAPSEARCH_BIN" mapsearch && exit 0 || exit $?
	;;

  *)
	log_action_msg "Usage: /etc/init.d/mapsearch {start|stop|reload|force-reload|restart|try-restart|status}" || true
	exit 1
esac

exit 0