#!/bin/bash

SESSIONDIR="/var/lib/rtorrent/session"
LOCKFILE="$SESSIONDIR/rtorrent.lock"

if [[ -f "$LOCKFILE" ]]; then
        # extract PID from lockfile
        PIDRT="$(awk -F: '{print($2)}' "${LOCKFILE}" | sed "s/[^0-9]//g")"

        # delete lockfile if PIDRT empty or if rtorrent is not said PID
        if [[ -z "$PIDRT" ]] || [[ "$(pgrep rtorrent)" -ne "$PIDRT" ]]; then
                rm -f "$LOCKFILE"
        fi
fi

exit 0