updated on Thu Jan 26 16:09:46 UTC 2012
[aur-mirror.git] / nwn / rc.nwserver.sh
blob188ff3bd36feea69f41fd235a345c39cb4c8eee0
1 #!/bin/bash
3 . /etc/rc.conf
4 . /etc/rc.d/functions
6 NWSERVER_SAVE_SLOT=2
7 NWSERVER_SAVE_NAME=shutdown
8 NWSERVER_SAVE_TIMEOUT=10
9 NWSERVER_ARGS=""
11 . /etc/conf.d/nwserver
13 _daemonName="Neverwinter Nights server"
14 _logDir="/var/log/nwserver"
15 _runDir="/var/run/nwserver"
16 _srvDir="/srv/nwn"
17 _expectScript=$(cat << EOS
18 set timeout "$NWSERVER_SAVE_TIMEOUT"
20 proc exitServer {} {
21 send "exit\r"
22 expect "Server: Exiting..." {return 0}
23 return 1
26 spawn attachtty "$_runDir/socket"
27 send "forcesave $NWSERVER_SAVE_SLOT $NWSERVER_SAVE_NAME\r"
28 expect {
29 "Server: Save complete" {
30 exit [exitServer]
32 default {
33 exit [exitServer]
36 EOS
39 case "$1" in
40 start)
41 stat_busy "Starting $_daemonName"
42 if [[ ! -f "$_runDir/server.pid" ]]; then
43 if [[ ! -d "$_runDir" ]]; then
44 mkdir -p "$_runDir"
45 chown nwserver:nwserver "$_runDir"
47 su nwserver -c "RW_BRANCH=$_srvDir detachtty --dribble-file $_logDir/server_stdout.log --log-file $_logDir/detachtty.log --pid-file $_runDir/server.pid $_runDir/socket /usr/bin/nwserver $NWSERVER_ARGS -interactive" &&
48 add_daemon nwserver &&
49 stat_done || stat_fail
50 else
51 stat_fail
54 stop)
55 stat_busy "Stopping $_daemonName"
56 if [[ ! -f "$_runDir/server.pid" ]]; then
57 # nwserver died
58 rm_daemon nwserver
59 stat_fail
60 else
61 if [[ -S "$_runDir/socket" ]]; then
62 echo "$_expectScript" | expect - &> /dev/null &&
63 # nwserver stopped
64 rm_daemon nwserver &&
65 stat_done ||
66 # nwserver didn't stop
67 stat_fail
68 else
69 # nwserver died
70 rm_daemon nwserver
71 stat_fail
75 restart)
76 $0 stop
77 sleep 1
78 $0 start
80 status)
81 stat_busy "Checking $_daemonName status"
82 ck_status nwserver
85 echo "usage: $0 {start|stop|restart|status}"
86 esac
87 exit 0