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

Pull info from Pithos for Conky

I switched from pianobar to Pithos as pianobar didn’t work as well with conky as I would have liked. The script below lets me get information from Pithos that I can use in pianobar.

#!/usr/bin/php
< ?php

$song=`dbus-send --print-reply --dest=net.kevinmehall.Pithos /net/kevinmehall/Pithos net.kevinmehall.Pithos.GetCurrentSong`;

$matches = array();

switch($argv[1]) {
	case 'album':
		$pattern = '~"album".*?"(.*?)".*~s';
		break;
	case 'title':
		$pattern = '~"title".*?"(.*?)".*~s';
		break;
	case 'artist':
		$pattern = '~"artist".*?"(.*?)".*~s';
		break;
	default:
		exit;
}

preg_match($pattern, $song, $matches);

if($matches) {
	echo $matches[1];
}

PHP Script for Deleting Orphaned Images

The title for this sucks. But that is because it isn’t easy to concisely describe (in a title fashion) what the script does. Imagine you have a lot of pictures from a photo shoot in both tif and jpg formats. You pick out the jpgs you want to keep and delete the rest. You still have all the tifs and want to delete the ones without a corresponding jpg. This script will do that. It takes three arguments: directory to delete from, directory to base that on, and an optional backup directory to move instead of deleting.

If you want it to handle different extensions, edit the code where it says jpg and tif

#!/usr/bin/php

< ?php $deleteFromDir = rtrim($argv[1], '/'); $basedOnDir = rtrim($argv[2], '/'); $backupDir = rtrim($argv[3], '/'); if(!empty($backupDir) && !file_exists($backupDir)){ if(!mkdir($backupDir)){ dir("Unable to create backup directory. Quitting\n"); } } $dh = opendir($deleteFromDir); if($dh){ while(false !== ($file = readdir($dh))){ if($file[0] != '.' && !empty($file)){ $info = pathinfo($deleteFromDir . '/' . $file); if($info['extension'] == 'tif' && !file_exists($basedOnDir . '/'. $info['filename'] . '.jpg')){ echo $file . "\n"; if(!empty($backupDir)){ rename($deleteFromDir . '/' . $file, $backupDir . '/' . $file); }else{ unlink($deleteFromDir . '/' . $file); } } } } }else{ echo "Unable to open $dir\n"; } [/sourcecode]