]> git.lizzy.rs Git - elidragon_v2.git/blob - scripts/internal.sh
8bfd1fc1038b88143e749ce53f6aabd49342ecb1
[elidragon_v2.git] / scripts / internal.sh
1 # internal functions used by Elidragon v2 scripts
2
3 function world_lock {
4         echo "/tmp/ElidragonV2_$1_lock"
5 }
6
7 function world_screenname {
8         echo "Elidragon v2 - $1"
9 }
10
11 function kill_world {
12         kill `cat \`world_lock $1\``
13 }
14
15 function is_running {
16         return $(test -f `world_lock $1`)
17 }
18
19 function loop_worlds {
20         WORLDS=`ls worlds`
21         for WORLD in $WORLDS; do
22                 $1 $WORLD
23         done
24 }
25
26 function assert_running {
27         if ! is_running $1; then
28                 echo "Error: World $1 is not running"
29                 exit 1
30         fi
31 }
32
33 function assert_not_running {
34         if is_running $1; then
35                 echo "Error: World $1 is already running"
36                 exit 1
37         fi
38 }
39
40 function start_world {
41         echo "Starting $1..."
42         assert_not_running $1
43
44         LOCK=`world_lock $1`
45
46         screen -dmS `world_screenname $1` bash -c "
47                 while is_running $1; do
48                         bash -c \"
49                                 echo \\$\\$ > $LOCK
50                                 exec minetest --server --terminal --world worlds/$1 --config worlds/$1/minetest.conf --logfile worlds/$1/debug.txt 
51                         \"
52                 done
53                 rm $LOCK
54         "
55 }
56
57 function stop_world {
58         echo "Stopping $1..."
59         assert_running $1
60
61         kill_world $1
62         rm `world_lock $1`
63 }
64
65 function restart_world {
66         echo "Restarting $1..."
67         assert_running $1
68
69         kill_world $1
70 }
71
72 function run_one_or_all {
73         if [ -z "$2" ]; then
74                 loop_worlds $1
75         else
76                 $1 $2
77         fi
78 }