#! /bin/sh
#
# This is a shell archive. Save this into a file, edit it
# and delete all lines above this comment.  Then give this
# file to sh by executing the command 'sh file'. The files
# will be extracted into the current directory owned by
# you with default permissions.

echo 'x - shar.cc'
sed 's/^X//' << '________This_Is_The_END________' > shar.cc
X// This may look like C code, but it is really -*- C++ -*-
X/*
X ************************************************************************
X *			  Build a .shar archive
X *
X * Call the routine
X *      shar arc_name file1 file2 ... fileN
X *
X * where arc_name specifies the name of the archive to create, and
X * file1 ... fileN are names of the files to put.
X *
X * .shar archive is a shell script to contain the contents of several text
X * files, which can be treated, say, moved by e-mail as a single unit. 
X * To unpack .shar archive it has to be fed to shell by simply executing 
X * 'sh arc_name'. 
X * .shar acrhive usually contains calls of the sed text editor to enter the
X * part of the script into the file and to modify it (say, to strip out 
X * leading chars X, which have been placed to prevent file lines (especially
X * those beginning with the dot) from misinterpretation by the mailer).
X *
X * The general structure of the shar archive is as follows
X *	Header: contains shell comment strings (starting with # ) identifying
X * 	the archive
X *	For every file being archived the following script is  
X *	generated
X *		echo 'x - FILE_NAME'
X *		sed 's/^X//' << '________This_Is_The_END________' > FILE_NAME
X *		Xlines of the file
X *		________This_Is_The_END________
X *		if test `wc -l < FILE_NAME` -ne FILE_LINES; then
X *		echo 'shar: FILE_NAME was damaged during transit 
X *		(should have been FILE_LINES lines)'
X *		fi
X *		
X * In the pattern above the FILE_NAME stands for the name of the file in
X * the archive, and FILE_LINES does for its size.
X *
X ************************************************************************
X */
X
X#include <assert.h>
X#include <fstream.h>
X#include <std.h>
X
X/*
X *-----------------------------------------------------------------------
X *		    Creating parts of the archive script
X */
X
X				// SHAR archive stream
Xclass SharStream : public ofstream
X{
Xpublic:
X  SharStream(const char * arc_name);	// Create a stream with the given name
X  void put_file(const char * fname);	// Put 'fname' into the stream
X};
X
X				// Constructor of a SHAR stream with the
X				// given name
XSharStream::SharStream(const char * arc_name)
X     : ofstream(arc_name)
X{
X  if( !good() )
X    cerr << "error opening the archive file named '" << arc_name << "'"
X         << endl,
X    exit(4);
X
X  *this << "#--------------------------------CUT HERE"
X              "-------------------------------------\n";
X  *this << "#! /bin/sh\n";
X  *this << "#\n";
X  *this << "# This is a shell archive. Save this into a file, edit it\n";
X  *this << "# and delete all lines above this comment.  Then give this\n";
X  *this << "# file to sh by executing the command 'sh file'. The files\n";
X  *this << "# will be extracted into the current directory owned by\n";
X  *this << "# you with default permissions.\n";
X}
X
X		
X			// Read the file with the given name and
X			// and put it to the archive
Xvoid SharStream::put_file(const char * fname)
X{
X  ifstream ifile(fname);
X  if( !ifile )
X    cerr << "error opening the input file '" << fname << "'" << endl,
X    exit(4);
X
X  *this << "\necho 'x - " << fname <<"'\n";
X  *this << "sed 's/^X//' << '________This_Is_The_END________' > " << fname
X           << "\n";
X
X  int lines_counter;
X  char * str;
X  for(lines_counter=0; ; lines_counter++)
X    if( !ifile.gets(&str) )
X      if( !ifile.eof() )
X	perror("reading the imput file"), exit(4);
X      else
X	break;
X    else
X    {
X      *this << "X" << str << "\n";
X      delete str;
X    }
X
X  *this << "________This_Is_The_END________\n";
X  *this << "if test `wc -l < " << fname << "` -ne " << lines_counter <<
X	    "; then\n";
X  *this << "echo 'shar: " << fname << " was damaged during transit " <<
X           "(should have had " << lines_counter << " lines)'\n";
X  *this << "fi\n\n";
X}
X
X
X/*
X *========================================================================
X *			Initialization routines
X */
X
Xstatic void help()
X{
X  cout << "\n\n\t\t\tBuild a .shar archive";
X  cout << "\n\n\n\tshar arc_name file1 file2 ... fileN";
X  cout << "\n\n";
X  exit(2);
X}
X
X
X/*
X *-----------------------------------------------------------------------
X *				Root module
X *  Parse the command string
X *  At least two arguments are expected (excluding the pgm name as the 0. arg)
X *  one     "arc_name"
X *  others names of the files to process
X */
X
Xmain(const int argc, const char *argv[])
X{
X if( argc < 3 )                        // Too few or too many arguments
X   help();
X
X SharStream shio(argv[1]);
X
X register int i;
X for(i=2; i<argc; i++)			// For every file specified
X   shio.put_file(argv[i]);
X
X cout << "\nSHAR archive " << argv[1] << " has been built successfully\n";
X}
X
X
X
X
________This_Is_The_END________
if test `wc -l < shar.cc` -ne 155; then
echo 'shar: shar.cc was damaged during transit (should have had 155 lines)'
fi
