use strict;
package SingleInstance;
use LogHandles;

(my $script_name = $0) =~ s!.*/!!;
my $Pid_File = "$ENV{HOME}/tmp/$script_name.pid";
my $OK_To_Delete;

if (open PID, $Pid_File) {
    chomp (my $pid = <PID>);
    close PID;
    if (kill 0, $pid) {
        $ERROR_LOG->print("Other $script_name instance running.  Exiting.");
        print STDERR "Error: another instance of $script_name is running.\n";
        exit;
    }
}
open PID, ">$Pid_File" or die "cannot open $Pid_File";
print PID "$$\n";
close PID;
$OK_To_Delete = 1;

END {
    unlink $Pid_File if $OK_To_Delete;
}

1;
