#!/bin/bash

# Deletes PID files for Indexer and OpenOffice, if the processes did not clear them properly on reboot.
indexer_pid="../var/indexer.pid"
indexer_tick="../var/indexer.tick"
indexer_cmd="indexer"
indexer_rm=0
openoffice_pid="../var/openoffice.pid"
openoffice_cmd="soffice"

cd `dirname $0`
if [[ -f $indexer_pid && ! -d /proc/`cat $indexer_pid` ]]; then
    indexer_rm=1
elif [[ -f $indexer_pid && -d /proc/`cat $indexer_pid` && "`cat /proc/\`cat $indexer_pid\`/cmdline | tr -d '\0'`" != "$indexer_cmd" ]]; then
    indexer_rm=1
fi
if [[ $indexer_rm -eq 1 ]]; then
    rm $indexer_pid
    if [[ -f $indexer_tick ]]; then
        rm $indexer_tick
    fi
fi
if [[ -f $openoffice_pid && ! -d /proc/`cat $openoffice_pid` ]]; then
    rm $openoffice_pid
elif [[ -f $openoffice_pid && -d /proc/`cat $openoffice_pid` && "`cat /proc/\`cat $openoffice_pid\`/cmdline | tr -d '\0'`" != *"$openoffice_cmd"* ]]; then
    rm $openoffice_pid
fi
