#!/bin/csh -f

# Compress file $1 (e.g. as produced by dvi2tty) to remove all blank
# lines (except those preceding a line matching the awk regexp $2 if
# present).  $1 should be the exact name of the file or a base name on
# to which .tty will be appended.

set tempfile = /tmp/ttycompress.$user.$$

set filename = $1
if (! -e $filename) set filename = $1.tty

set noglob

set compressregexp = '/$^/'
if ($#argv > 1) set compressregexp = "/$2/"

awk 'BEGIN {pendbl="no"}\
     /^$/ {pendbl="yes"}\
     '"$compressregexp"' {if (pendbl=="yes") print ""}\
     /./ {pendbl="no"; print}' $filename > $tempfile

if (! $status) mv $tempfile $filename

rm -f $tempfile
