#!/bin/sh

# Copyright 1988-1991 Network Computing Devices, Inc.  All rights reserved.
# An unpublished work.

#ident "@(#)xlog	14.1	91/02/26"

# Simple script to control xterm logging.  There are three useful invocations:
#
#	xlog on file	starts logging to the specified file
#	xlog on		starts logging to the spooler
#	xlog off	terminates the logging
#
# The only oddity is that if a relative file name is specified it is relative
# to the working directory at the time xterm was started.
#
# When logging to the spooler, CRs are removed
# before printing.

EXIT_OK=0
EXIT_USAGE=1

MYNAME=$0
DIR=`dirname $0`

CHR=$DIR/chr
PRINT=$DIR/print

# Must have at least one argument

if [ $# -lt 1 ]
then
    echo Usage: xlog on [logfile] or xlog off >&2
    exit $EXIT_USAGE
fi

# This nonsense is necessary to assure that we have an absolute pathname
# for xlog.sed so that when xterm starts the pipeline to do the logging
# we aren't dependent on how the user referenced this script.  The issue
# is that xterm pipes the log data to a command of the form:
#
#	$SHELL -c $LPR
#
# which is then run with the working directory of the xterm (not the
# current directory).

WD=`pwd`
cd $DIR

LPR="sed -e '$d'|lpr"

case "$1" in
    on| ON)
	if [ $# -ne 2 ]
	then
	    logfile="|$LPR"
	else
	    logfile="$2"
	fi
	# Define log file.
	$CHR ESC ] 4 6 \;; $PRINT "$logfile"; $CHR BEL
	# Turn on logging
	$CHR ESC [ ? 4 6 h
	;;
    off| OFF)
	# Turn off logging
	$CHR ESC [ ? 4 6 l
	# Reset log file.
	$CHR ESC ] 4 6 \; BEL
	;;
    *)
	echo Usage: xlog on [logfile] or xlog off >&2
	exit $EXIT_USAGE
	;;
esac

exit $EXIT_OK
