Init Script for Calibre Server

I use the very excellent Calibre to make my ebooks accessible to all of my devices anywhere I am. It has a very useful server component that many ebook readers can use to download content. What I was missing was a way to automatically start/stop calibre-server. For that,I put together this rc script for Arch Linux.

#!/bin/bash

. /etc/rc.conf
. /etc/rc.d/functions

CONTENT=/mnt/miscellaneous/Books/Literary
PORT=8080

DAEMON=calibre-server
PIDFILE=/var/run/$DAEMON.pid
ARGS="--auto-reload --with-library=$CONTENT --pidfile=$PIDFILE --port=$PORT --daemonize"

case "$1" in
 start)
  stat_busy "Starting $DAEMON"
  [ -z "$PID" ] && $DAEMON $ARGS &>/dev/null
   if [ $? = 0 ]; then
     add_daemon $DAEMON
     stat_done
   else
     stat_fail
     exit 1
   fi
   ;;
 stop)
   stat_busy "Stopping $DAEMON"
   PID=`cat $PIDFILE`
   [ -n "$PID" ] && kill $PID &>/dev/null
   if [ $? = 0 ]; then
     rm_daemon $DAEMON
     stat_done
   else
     stat_fail
     exit 1
   fi
   ;;
 restart)
   $0 stop
   sleep 1
   $0 start
   ;;
 *)
   echo "usage: $0 {start|stop|restart}"
esac