#!/bin/sh
#	$Id: cpsencode.sh,v 1.4 2003/06/30 13:09:17 pwessel Exp $
#
#      Copyright (c) 2003 by P. Wessel
#
#      This program is free software; you can redistribute it and/or modify
#      it under the terms of the GNU General Public License as published by
#      the Free Software Foundation; version 2 of the License.
#
#      This program is distributed in the hope that it will be useful,
#      but WITHOUT ANY WARRANTY; without even the implied warranty of
#      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#      GNU General Public License for more details.
#
#--------------------------------------------------------------------*/
#	Bare-bones Bourne shell version of cpsencode.c.  Performs the
#	same job as cpsencode.c but lacks its options [-s -v].
#
#  cpsencode - Generating Complete PostScript (cps) files that
#		contain all the commands and the data required
#		to recreate the plot.
#
# Author:	Paul Wessel, SOEST, U. of Hawaii
# Version:	2.0 [for latest release, see www.soest.hawaii.edu/pwessel/cps]
# Date:		01-JUL-2003
#
# Usage:	cpsencode file(s) [-s] [-v] >> PSfile
 
if [ $# -eq 0 ]; then
	echo "usage: cpsencode.sh files >> plot.ps" >&2
	exit
fi

cat << EOF
%%EOF
%CPS_PrivateDataBegin
%
%CPS_Info: ------------------------------------------------------------
%CPS_Info: This section contains bzip2|uuencoded data files enclosed
%CPS_Info: between pairs of CPS_FileBegin and CPS_FileEnd markers.
%CPS_Info: Extract original data files using cpsdecode this_PS_file.
%CPS_Info: Obtain cpsdecode from http://www.soest.hawaii.edu/pwessel/cps
%CPS_Info: or use the following Bourne script cpsdecode.sh instead:
%CPS_Info: Cut out, remove leading percentage comments, and chmod +x
%CPS_Info: or extract from this file with sed -n -e 's/^%CPS_Code: //p'
%CPS_Code: #!/bin/sh
%CPS_Code: if [ \$# -ne 1 ]; then
%CPS_Code: 	echo "usage: cpsdecode.sh PSfile" >&2
%CPS_Code: 	exit
%CPS_Code: fi
%CPS_Code: trap "rm -f \$\$; exit" 0 1 2 15
%CPS_Code: sed -n '/^%CPS_PrivateDataBegin/,\$p' \$1 > \$\$
%CPS_Code: grep -n '%CPS_File' \$\$ | tr ':' ' ' | awk '{if (NF == 5) {f = \$3; s = \$1} else { printf "sed -n %d,%ds/^%%//gp %s | uudecode\nbzip2 -d %s.bz2\n", s+1, \$1-1, D, f}}' D=\$\$ | sh -s
%CPS_Info: ------------------------------------------------------------
%
EOF
for file in $*; do
	source_len=`ls -l $file | awk '{print $5}'`
	bzip2 -c9 $file > $file.bz2 
	dest_len=`ls -l $file.bz2 | awk '{print $5}'`
	echo "%CPS_FileBegin: $file $source_len $dest_len"
	uuencode $file.bz2 $file.bz2 | sed -e 's/^/%/g'
	echo "%CPS_FileEnd"
	rm -f $file.bz2 
done
echo "%CPS_PrivateDataEnd"

