#!/usr/bin/env fish

set lockfile $argv[1]

if test -e "$lockfile"
    set pid (cat "$lockfile")

    if ps -p $pid >/dev/null
        echo "Another instance of this script is running (PID $pid). Exiting."
        echo "If you think this is a mistake, remove $lockfile."
        exit 1
    else
        rm -f "$lockfile"
    end
end

echo $fish_pid >"$lockfile"

trap "rm -f \"$lockfile\"; exit 0" INT TERM EXIT
