]> git.lizzy.rs Git - elidragon_v2.git/blob - scripts/common.sh
Add missing -e arguments to common script
[elidragon_v2.git] / scripts / common.sh
1 # common 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 -e "\e[31mWorld $1 is not running\e[0m"
29                 if [ -z "$2" ]; then
30                         exit 1
31                 else
32                         return 1
33                 fi
34         fi
35 }
36
37 function assert_not_running {
38         if is_running $1; then
39                 echo -e "\e[31mWorld $1 is already running\e[0m"
40                 if [ -z "$2" ]; then
41                         exit 1
42                 else
43                         return 1
44                 fi
45         fi
46 }
47
48 function start_world {
49         echo -n "Starting $1... "
50         if assert_not_running $1 "true"; then
51                 LOCK=`world_lock $1`
52
53                 screen -dmS `world_screenname $1` bash -c "
54                         while is_running $1; do
55                                 bash -c \"
56                                         echo \\$\\$ > $LOCK
57                                         exec minetest --server --terminal --world worlds/$1 --config worlds/$1/minetest.conf --logfile worlds/$1/debug.txt 
58                                 \"
59                         done
60                         rm $LOCK
61                 "
62
63                 echo -e "\e[32mDone\e[0m"
64         fi
65 }
66
67 function stop_world {
68         echo -n "Stopping $1..."
69         if assert_running $1 "true"; then
70                 kill_world $1
71                 rm `world_lock $1`
72
73                 echo -e "\e[32mDone\e[0m"
74         fi
75 }
76
77 function restart_world {
78         echo -n "Restarting $1..."
79         if assert_running $1 "true"; then
80                 kill_world $1
81
82                 echo -e "\e[32mDone\e[0m"
83         fi
84 }
85
86 function run_one_or_all {
87         if [ -z "$2" ]; then
88                 loop_worlds $1
89         else
90                 $1 $2
91         fi
92 }