aboutsummaryrefslogtreecommitdiff
path: root/multimedia/minidlna/rc.minidlna
blob: 2651f3b63698e29977982e13920e87ba6a6037a6 (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
#!/bin/sh
#
# /etc/rc.d/rc.minidlna
#
# start/stop/restart/status of the MiniDLNA server.
#
# To make MiniDLNA start automatically at boot, make this
# file executable:  chmod 755 /etc/rc.d/rc.minidlna
#
# Written by Georgi D. Sotirov <gsotirov@gmail.com>
# Based on examples from Internet

# Bash colors
C_GREEN=$'\e[32;01m'
C_YELLOW=$'\e[33;01m'
C_RED=$'\e[31;01m'
C_NORMAL=$'\e[0m'

SNAME='MiniDLNA server'
NAME=minidlna
CMD="/usr/sbin/${NAME}d"
CONFILE="/etc/${NAME}.conf"
PIDFILE="/var/run/${NAME}.pid"
CMD_OPTS="-f $CONFILE -P $PIDFILE"

ok() {
  echo "${C_GREEN}Done${C_NORMAL}"
}

fail() {
  echo "${C_RED}Failure${C_NORMAL}"
}

print_status() {
  if [ $? != 0 ]; then
    fail
  else
    ok
  fi
}

start() {
  if [ -x $CMD ]; then
    if [ ! -e $PIDFILE ]; then
      echo -n "Starting $SNAME in $CMD... "
      $CMD $CMD_OPTS
      print_status
    else
      echo "Starting $SNAME: Already running with PID `cat $PIDFILE`!"
    fi
  fi
}

stop() {
  if [ -e $PIDFILE ]; then
    echo -n "Stopping ${SNAME}... "
    kill -15 `cat $PIDFILE`
    print_status
  else
    echo "Stopping ${SNAME}: Not running!"
  fi
}

restart() {
  stop
  sleep 3
  start
}

status() {
  if [ -e $PIDFILE ]; then
    echo "$SNAME running with PID `cat $PIDFILE`."
  else
    STAT=`ps -C $NAME -o pid= | wc -l`
    if [ ${STAT} -ge 1 ]; then
      echo "$SNAME is running"
    else
      echo "$SNAME is not running"
    fi
  fi
}

db_clean() {
  # If DB dir defined explicitly in log (i.e. not commented), then use it
  DB_DIR_CONF=$(grep -E '^db_dir=' $CONFILE | awk -F'=' '{ print $2 }')
  # else fallback to default
  DB_DIR=${DB_DIR_CONF:-/var/cache/$NAME}
  WAS_RUNNING=0

  if [ -e $DB_DIR -a -d $DB_DIR ]; then
    # Stop daemon if running
    if [ -e $PIDFILE ]; then
      WAS_RUNNING=1
      stop
      sleep 3
    fi

    echo -n "Cleaning cache in ${DB_DIR}... "
    rm -f ${DB_DIR}/files.db
    rm -rf ${DB_DIR}art_cache
    print_status

    # And restart daemon afterwards
    if [ $WAS_RUNNING -ne 0 ]; then
      start
    fi
  else
    echo "Error: cache dir $DB_DIR does not exist or not a direcory!"
  fi
}

help() {
  echo "$SNAME control"
  echo "Usage: $0 start|stop|restart|status|clean"
  echo
}

case "$1" in
  'start')
    start ;;
  'stop')
    stop ;;
  'restart')
    restart ;;
  'status')
    status ;;
  'clean')
    db_clean ;;
  *)
    help ;;
esac