summaryrefslogtreecommitdiff
path: root/apps/startup-scripts/src/starter
blob: 4e58e8e7b490e45d6a9e650fe32f894e7dc58ae3 (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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env bash

# AzerothCore Starter Script
# This script handles the execution of AzerothCore binaries with optional GDB support
#
# Usage: starter <binpath> <binfile> [gdb_file] [config] [syslog] [syserr] [gdb_enabled] [crashes_path]
#
# Parameters:
#   $1 - Binary path (required)
#   $2 - Binary file name (required)
#   $3 - GDB configuration file (optional)
#   $4 - Configuration file path (optional)
#   $5 - System log file (optional)
#   $6 - System error file (optional)
#   $7 - GDB enabled flag (0/1, optional)
#   $8 - Crashes directory path (optional)

BINPATH="$1"
BINFILE="$2"
GDB_FILE="$3"
CONFIG="$4"
SYSLOG="$5"
SYSERR="$6"
GDB_ENABLED="${7:-0}"
CRASHES_PATH="$8"

# Default values
CURRENT_PATH="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
DEFAULT_CRASHES_PATH=$(realpath "$BINPATH/crashes")
[ -n "$CONFIG" ] && CONFIG_ABS=$(realpath "$CONFIG")

# Set defaults if not provided
CRASHES_PATH="${CRASHES_PATH:-$DEFAULT_CRASHES_PATH}"

# Validate binary
if [ -z "$BINPATH" ] || [ -z "$BINFILE" ]; then
    echo "Error: Binary path and file are required"
    echo "Usage: $0 <binpath> <binfile> [gdb_file] [config] [syslog] [syserr] [gdb_enabled] [crashes_path]"
    exit 1
fi

BINARY="$BINPATH/$BINFILE"
if [ ! -f "$BINARY" ]; then
    echo "Error: Binary '$BINARY' not found"
    exit 1
fi

# Create crashes directory if it doesn't exist
mkdir -p "$CRASHES_PATH"

cd "$BINPATH" || {
    echo "Error: Could not change to binary path '$BINPATH'"
    exit 1
}

EXECPATH=$(realpath "$BINFILE")

if [ "$GDB_ENABLED" -eq 1 ]; then
    echo "Starting $EXECPATH with GDB enabled"

    # Generate GDB configuration on the fly
    TIMESTAMP=$(date +%Y-%m-%d-%H-%M-%S)
    GDB_TEMP_FILE="$CRASHES_PATH/gdb-$TIMESTAMP.conf"
    GDB_OUTPUT_FILE="$CRASHES_PATH/gdb-$TIMESTAMP.txt"

    # Create GDB configuration file if it is not defined
    if [ -z "$GDB_FILE" ]; then

        # Create GDB configuration
        cat > "$GDB_TEMP_FILE" << EOF
set logging file $GDB_OUTPUT_FILE
set logging enabled on
set debug timestamp
EOF

    # Add run command with config if specified
    if [ -n "$CONFIG_ABS" ]; then
        echo "run -c $CONFIG_ABS" >> "$GDB_TEMP_FILE"
    else
        echo "run" >> "$GDB_TEMP_FILE"
    fi
    
    cat >> "$GDB_TEMP_FILE" << EOF
bt
bt full
info thread
thread apply all backtrace full
EOF


        GDB_FILE="$GDB_TEMP_FILE"
    fi



    # Create log files if specified
    if [ -n "$SYSLOG" ]; then
        [ ! -f "$SYSLOG" ] && touch "$SYSLOG"
    fi

    if [ -n "$SYSERR" ]; then
        [ ! -f "$SYSERR" ] && touch "$SYSERR"
    fi

    # Execute with GDB
    if [ "${WITH_CONSOLE:-0}" -eq 0 ] && [ -n "$SYSLOG" ] && [ -n "$SYSERR" ]; then
        gdb -x "$GDB_FILE" --batch "$EXECPATH" >> "$SYSLOG" 2>> "$SYSERR"
    else
        echo "> Console enabled"
        if [ -n "$SYSLOG" ] && [ -n "$SYSERR" ]; then
            gdb -x "$GDB_FILE" --batch "$EXECPATH" > >(tee "$SYSLOG") 2> >(tee "$SYSERR" >&2)
        else
            gdb -x "$GDB_FILE" --batch "$EXECPATH"
        fi
    fi


    # clean up temporary GDB file if it exists
    if [ -n "$GDB_TEMP_FILE" ]; then
        # Clean up temporary GDB file
        rm -f "$GDB_TEMP_FILE"
    fi
else
    echo "Starting $BINFILE without GDB"
    # Determine if PM2 is active
    is_pm2_active="0"
    [ "$AC_LAUNCHED_BY_PM2" == "1" ] && is_pm2_active="1"
    
    # Determine if interactive mode is enabled
    is_interactive_enabled="1"
    [ "$AC_DISABLE_INTERACTIVE" == "1" ] && is_interactive_enabled="0"
    
    # use normal execution if we are running the binary under PM2 
    # or when interactive mode is enabled
    if [[ "$is_pm2_active" == "1" || "$is_interactive_enabled" == "1" ]]; then
        echo "Running AC"
        "$EXECPATH" ${CONFIG_ABS:+-c "$CONFIG_ABS"}
    else
        # When AC_DISABLE_INTERACTIVE is set to 1 and we are not in PM2 
        # This means we are using systemd without interactive mode and no session managers
        # in this case we need to run AC with unbuffer for line-buffered output 
        # NOTE unbuffer doesn't fully support interactive mode
        if command -v unbuffer >/dev/null 2>&1; then
            echo "Running AC with unbuffer for line-buffered output"
            unbuffer "$EXECPATH" ${CONFIG_ABS:+-c "$CONFIG_ABS"}
        else
            echo "⚠️  unbuffer not found, the output may not be line-buffered. Try installing expect."
            exec "$EXECPATH" ${CONFIG_ABS:+-c "$CONFIG_ABS"}
        fi
    fi
fi