1#! /bin/sh
2# wrapper to prevent running multiple instances of tin
3# 2000-01-19 <urs@tin.org>
4
5prog=/usr/local/bin/tin
6
7args=${1+"$@"}
8
9name=`basename $prog`
10host=`hostname`
11pid=$$
12lock=$host"	"$pid
13
14if test -d ${TIN_HOMEDIR-"$HOME"} ; then
15	lockdir=${TIN_HOMEDIR-"$HOME"}/.tin
16	file=tinlock
17else
18	lockdir=${TMPDIR-/tmp}
19	if test -z $USER; then
20		if test -z $UID ; then
21			UID=`id|sed 's,uid=\([0-9]\+\).*,\1,'`
22		fi
23	fi
24	file=tinlock.${USER-"$UID"}
25fi
26
27lockfile=$lockdir/$file
28
29if test -f $lockfile ; then
30	echo "Found \"$lockfile\"! Another $name running on:"
31	awk '{print $1" with pid: "$2"?"}' < $lockfile
32	echo "(q)uit - start (r)ead-only - (d)elete lock and start normal."
33	IFS="       " read key
34	case $key in
35		[dD]*)
36			echo "Removing \"$lockfile\" and starting $name normal..."
37			rm -f $lockfile || (echo "Can't delete \"$lockfile\"" ; exit 1)
38			trap "rm -f $lockfile; exit" 0 1 2 3 15
39			(echo $lock > $lockfile && $prog $args && rm -f $lockfile) || echo "Oops!"
40			;;
41		[rR]*)
42			echo "Starting $name in no-write (-X) mode..."
43			$prog -X $args
44			echo "There might still be copy(s) of $name running on:"
45			awk '{print $1" with pid: "$2}' < $lockfile
46			echo "If this is not the case remember to remove \"$lockfile\"."
47			;;
48		*)
49			echo "Aborting..."
50			exit 1
51			;;
52	esac
53else
54	trap "rm -f $lockfile; exit" 0 1 2 3 15
55	(echo $lock > $lockfile && $prog $args && rm -f $lockfile) || echo "Oops!"
56fi
57