summaryrefslogtreecommitdiffstats
path: root/multimedia/sickchill/rc.sickchill
blob: bb89629a9c0796cfced4e40a499e3cb555c02ec7 (about) (plain) (blame)
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
#!/bin/bash
# Start/stop/restart sickchill.

# rc.sickrage created by Jeremy Hansen for Slackware in 2016
# rc.sickchill was updated by Jeremy Hansen for Slackware in 2023

# Set program name in case you want to run sick{beard|rage|gear|etc}
PROG=${PROG:-sickchill}

# Source SickRage configuration
if [ -f /etc/"$PROG".conf ]; then
        . /etc/"$PROG".conf
fi

# Ensure all required variables are set in conf file
# Edit conf file in /etc/sickchill.conf for any changes
for var in USERNAME HOMEDIR DATADIR PIDFILE PORT; do
  if [ -z "${!var}" ]; then
    echo "/etc/$PROG.conf is missing some or all required variables ($var)."
    echo "Please check the file and try again."
    exit 1
  fi
done

# Check if the program is running and pid file exists
check() {
  if pgrep "$PROG" > /dev/null; then
    if [ -e "$PIDFILE" ] && ! pgrep -F "$PIDFILE" > /dev/null; then
        STATUS=broken
        echo "WARNING: $PROG is running, but its PID does not match the one in $PIDFILE."
    elif [ ! -e "$PIDFILE" ]; then
        STATUS=broken
        echo "WARNING: $PROG is running, but $PIDFILE does not exist."
        echo "Maybe you ran $PROG from the commandline rather than rc.sickchill?"
    else
      STATUS=running
    fi
  else
    if [ -e "$PIDFILE" ]; then
      STATUS=broken
      echo "WARNING: $PROG is not running but $PIDFILE exists."
    else
      STATUS=stopped
    fi
  fi
}

status() {
  if [ $STATUS == "running" ]; then
    echo "$PROG currently running."
  elif [ $STATUS == "stopped" ]; then
    echo "$PROG not running."
  elif [ $STATUS == "broken" ]; then
    echo "Please fix the issue before attempting to run $(basename "$0") again."
  else
    echo "Status unknown."
  fi
}

start() {
  if [ $STATUS == "running" ]; then
    echo "$PROG already running or not shut down properly."
  else
    echo -n "Starting $PROG: "
    if su "$USERNAME" -s /bin/sh -c "/usr/bin/sickchill --daemon --pidfile=${PIDFILE} --datadir=${DATADIR} --port=${PORT} &> /dev/null"; then
      echo "Startup Successful"
    else
      echo "Startup Failed. Please try running the following to see the errors."
      echo "su $USERNAME -s /bin/sh -c \"/usr/bin/sickchill --daemon --pidfile=${PIDFILE} --datadir=${DATADIR} --port=${PORT}\""
    fi
  fi
}

stop() {
  if [ $STATUS == "stopped" ]; then
    echo "$PROG doesn't seem to be running. Please try running"
    echo "$0 start"
  elif [ $STATUS == "broken" ]; then
    echo "Cannot stop. Please correct issue and try again."
  else
    if [ "$EUID" -ne 0 ];then
      echo "Please run as root"
      exit 1
    fi
    PID=$(cat "$PIDFILE")
    echo -n $"Shutting down $PROG: "
    if ! curl -s http://localhost:"$PORT"/home/shutdown/?pid="$PID" | grep -q "shutting down"; then
      echo "Normal Shutdown Failed - Attempting to kill the process."
      sleep 7
      kill -9 "$PID"
    else
      echo "Shutdown Successful"
    fi
  fi
}

case "$1" in
  start)
        check
        start
        ;;
  stop)
        check
        stop
        ;;
  restart)
        check
        stop
        start
        ;;
  status)
        check
        status
        ;;
  *)
        echo "Usage: $0 {start|stop|restart|status}"
        exit 1
esac