aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/start-sim
blob: b280a1d34193dc90d673e13989566a9a244855a9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/bin/bash

# Number of days to keep backup OAR files.
DAYS="15"

USER=$(whoami)
console_name="OpenSim_console"
screen_command="tmux -S /var/run/opensim/opensim-tmux.socket"
screen_session=${console_name}
screen_window="${screen_command} select-window -t ${screen_session}"
osversion="current"
bindir=/opt/opensim/${osversion}/bin
quiet=""

if [ $USER = "opensim" ]
then
    SUDO=""
else
    SUDO="sudo -Hu opensim"
fi

if [ "$1" = "-q" ]
then
    quiet="true"
    shift 1
fi
   
if [ "x$1" = "x" ]; then
    pathname=$(pwd)
    tgt=$(basename $pathname)
elif [ -d "./$1" ]; then
    tgt=$1
elif [ -d "./sim$1" ]; then
    tgt=sim$1
fi

if [ "x$tgt" = "x" ]; then
    echo "usage:"
    echo "    $ $(basename $0) <sim>"
    echo "where <sim> is one of: " robust sim[0-9][0-9]
    exit 1
fi

# Would be nice if this worked.
export MONO_PATH=${bindir}
# Then we would not have to do this, and subsequently write all over the damn bin directory.
cd ${bindir}

if ( ${screen_command} -q list-sessions 2>/dev/null | grep -q ${console_name}: ); then
    true
else
    # The sudo is only so that the session is owned by opensim, otherwise it's owned by whoever ran this script, which is a likely security hole.
    # After the session is created, we rely on the /var/run/opensim directory to be group sticky, so that anyone in the opensim group can attach to the tmux socket.
    $SUDO ${screen_command} new-session -d -s ${console_name}
fi

inidir=/opt/opensim/config/${tgt}

if [ "x$tgt" = "xrobust" ]; then
    exe="Robust"
    title="ROBUST"
    roboini="-inifile=${inidir}/Robust.ini"
else
    exe="OpenSim"
    # Grab the first line of the sim.ini file, it should be the sim name in square brackets.
    title=$(head -n 1 ${inidir}/Regions/sim.ini )
    # Strip off spaces at either end.
    shopt -s extglob
    title=${title##*( )}
    title=${title%%*( )}
    shopt -u extglob
    # Strip off the square brackets at either end.
    title=${title:1:$(( ${#title} - 2 ))}
fi

case $(basename $0) in 
    "start-sim")
	cmd="/usr/bin/mono ${bindir}/${exe}.exe -inidirectory=${inidir} -logconfig=${inidir}/${exe}.exe.config ${roboini}"

	# Check if it's already running.
	if [ -e /var/run/opensim/${tgt}.pid ]
	then
	    # Double check if it's REALLY running.
	    if [ "x$(ps -p $(cat /var/run/opensim/${tgt}.pid) --no-headers -o comm)" = "x" ]; then
		$SUDO rm -f /var/run/opensim/${tgt}.pid
	    fi
	fi
	# Now see if it's running.  lol
	if [ ! -e /var/run/opensim/${tgt}.pid ]
	then
		${screen_command} new-window -dn "[${title}]" -t "${screen_session}" "${cmd}"
	fi
	;&

    "sim-console")
	if [ "x$quiet" = "x" ]
	then
	    ${screen_window}:"[${title}]" \; attach-session -t "${screen_session}"
	fi
	;;

    "backup-sim")
	# First delete old backups.  Using mtime instead of ctime, so that people can touch files the want to keep, and coz ctime seems to have a bug.
	find "${inidir}/../../backups/" -name "${title// /_}-*.oar" -mtime +${DAYS} -type f -print0 | xargs -0 /bin/rm -f
	# Substitute underscores for spaces in the title, then add the full date and time to create the OAR file name.
	cmd="save oar ${inidir}/../../backups/${title// /_}-$(date '+%F_%T').oar"
	${screen_window}:"[${title}]" \; send-keys "${cmd}" Enter
	;;

    "stop-sim")
	${screen_window}:"[${title}]" \; send-keys "shutdown" Enter
	;;
esac