#!/bin/tcsh
#
# oldjobs	This csh/tcsh script starts a set of jobs for a login shell
#		shell from a file listing previously executing jobs.  It
#		should be sourced, rather than executed.
#
#		As written now, the only jobs that are restarted are vi, nvi,
#		and vim jobs.  It is a simple matter to extend the command
#		list iff any other commands should also be restarted.  Search
#		for the string "JOBS TO START" and modify the if statement
#		that follows.  Following the existing if, it should be a
#		trivial matter to add additional commands. 
#
#		This script is intended for use in a windowing environment.
#
#	Requirements:
#		- There must be a directory in the user's home directory
#		  called ".sessions".  This will hold the jobs files.
#
#		- There will be a shell variable "ttyname", which will be
#		  the name of an individual terminal.  (xterm, in my case.)
#
#		  My .cshrc uses this line to set this variable:
#
#			set ttyname = `xprop -id $WINDOWID WM_NAME | awk -F\" '{print $2}' | awk '{print $1}'`
#
#		  The first word from the window's title field will be used
#		  as the tty's name.
#
#		  This variable must be adjusted if a different windowing
#		  system is used, or if xprop isn't available.
#
#		- The jobs files must be updated manually.  I use these
#		  commands (in my .cshrc) to accomplish this:
#
#			set jobsfile = "~/.sessions/j-$ttyname-$$"
#
#			alias  jobber  "jobs > $jobsfile"
#
#		  I run jobber periodically whenever I feel the jobs file
#		  must be updated.
#
#		- The jobs files are named "j-${ttyname}-<pid>".
#		  So, for a terminal with a name of "devel", and an xterm
#		  with a pid of 38911, the jobs file's absolute path will
#		  be ~/.sessions/j-devel-38911.
#
#	Usage:
#		$ source oldjobs
#
# Revision History
#	1.0	Initial revision.					140616
#	1.1	Changed sessions directory from .ttys to .sessions.	150920
#	1.2	Added licensing info.					180531
#
#	Written by Wayne Morrison, 140616.
#
# Copyright 2014 Wayne Morrison
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#
# If we have a ttyname, we'll dig out the tty's directory file and 
# add its directories to the shell's stack.
#
if("$ttyname" == "") then
	echo "no ttyname, cannot determine jobsfile"
	exit
endif

#
# Get the sessions directory path.
#
set sessdir = ~/.sessions

#
# Get the number of jobs files this tty has.
# Old ones may still be hanging around...
#
set jfcnt = `ls -1 $sessdir/j-${ttyname}-*[0-9] | wc -l`
if($jfcnt == 0) then
	echo "no jobsfiles for $ttyname, so no jobs list to build"
	unset sessdir
	exit
endif

#
# Use the most recent jobs file, if this tty has more than one.
# Otherwise, we'll use the solitary one we have.
#
if($jfcnt > 1) then
	set jobf = `ls -tr1 $sessdir/j-${ttyname}-*[0-9] | tail -1`
	echo "too many jobsfiles ($jfcnt), using most recent - $jobf"
	echo ''
else
	set jobf = `ls -1 $sessdir/j-${ttyname}-*[0-9]`
endif

#
# Don't continue if we didn't find a jobs file.
#
if("$jobf" == "") then
	echo
	echo "no jobs file for tty ${ttyname}"
	exit
endif

#
# Back up the jobs file.
#
set backer = "$jobf.save"
cp $jobf $backer

##############################################################################

#
# Initialize a line count, just so it's ready for use in the loop.
#
set linecnt = 42

#
# Go through the jobs file and start all the jobs in the list of
# commands we'll restart.
#
while($linecnt > 1)

	#
	# Get the number of lines in the jobs output.
	#
	set linecnt = "`wc -l $jobf | sed 's/^ *//' | sed 's/ .*//'`"

	#
	# Get rid of everything from the jobs lines except the
	# actual commands that were executed.
	#
	perl -pi -e 's/^\s*\[\d+\][ +-]+(Running|Suspended)\s+//gm' $jobf

	#
	# Get the head job from the jobs listing.
	#
	set firstjob = "`head -1 $jobf`"

	#
	# Get the command from the head job listed.
	#
	set cmd = `echo $firstjob | sed 's/ .*$//' | sed 's/^.*\///'`

	#
	# Get the rest of the lines from the jobs output and remove
	# the first line from the file.
	#
	@ keeplines = $linecnt - 1
	if($keeplines > 0) then
		tail -$keeplines $jobf > $jobf.tmp
	else
		cp /dev/null $jobf.tmp
	endif
	mv $jobf.tmp $jobf

	#
	# Only deal with editors.
	#
	#	JOBS TO START
	#
	if($cmd == vi || $cmd == nvi || $cmd == vim) then
#		echo "cmd - $cmd		firstjob - $firstjob"
		$firstjob &
	endif

end

echo

##############################################################################

#
# Maybe delete the job file.
#
set linecnt = `wc -l $jobf | awk '{print $1}'`
if($linecnt == 0) then
	echo
	echo "          deleting $jobf -- old jobs still in $backer"
	echo
	rm -f $jobf
else
	echo
	echo "          NOT deleting $jobf"
	echo
endif

#
# And let's see what we ended up with.
#
jobs

echo 

#
# Unset the shell variables we used here.
#
unset backer cmd firstjob jfcnt jobf linecnt sessdir

exit 0