#!/bin/sh
########################################################
# This is a shell archive  --- shark 0.1.1 ---         #
# Please remove any lines before this header and       #
# run     sh this-file-name     to extract all files.  #
# 1994 (C) Fernando J G Pereira - fjp@minerva.inesc.pt #
########################################################
echo unsharking shark
mkdir shark
echo unsharking shark/shark.c
cat > shark/shark.c << '\\__END__OF__shark/shark.c__FILE\\'
/******************************************************************************
* shark - shell archiver.						      *
*									      *
* This is free software - you are free to use it and distribute it as long as *
* this message appears in the source code and archive headers.                *
* There is no explicit or implicit WARRANTY in the usage of this program.     *
* You are free to use it at your own risk (including all kinds of possible    *
* damages). 						                      *
*									      *
*				15/Feb/94				      *
*									      *
*                 Fernando Pereira - fjp@minerva.inesc.pt                     *
******************************************************************************/




#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>


#define LINE_SIZE 1024
#define SCAN_SIZE 2048

FILE *arch = stdout;

void archive( char *file );
void archive_dir( char* file, struct stat *info );
void archive_file( char* file, struct stat *info );
void archive_link( char *file, struct stat *info );
void archive_char_dev( char *file, struct stat *info );
void archive_block_dev( char *file, struct stat *info );
void archive_pipe( char *file, struct stat *info );
int is_bin( char *file );




int main( int argc, char **argv )
{
    int i = 1;

    while( argv[i] && argv[i][0] == '-' ) {
	switch( argv[i][1] ) {
	    case 'o':
	        ++i;
	        if( argv[i] == 0 ) exit( 1 );
	        arch = fopen( argv[i], "w" );
		fchmod( fileno( arch ), 0755 );
		break;
	    case 'h':
	    default:
	        fprintf( stderr, "usage: shark [-o output-file] files\n");
	        exit( 0 );
	}
	++i;
    }

    fprintf(arch,"#!/bin/sh\n" );
    fprintf(arch,"########################################################\n" );
    fprintf(arch,"# This is a shell archive  --- shark 0.1.1 ---         #\n" );
    fprintf(arch,"# Please remove any lines before this header and       #\n" );
    fprintf(arch,"# run     sh this-file-name     to extract all files.  #\n" );
    fprintf(arch,"# 1994 (C) Fernando J G Pereira - fjp@minerva.inesc.pt #\n" );
    fprintf(arch,"########################################################\n" );

    while( i < argc ) archive( argv[i++] );
}




void archive( char *file )
{
    struct stat info;

    if( lstat( file, &info ) < 0 ) {
        perror( file );
        return;
    }
    fprintf( stderr, "sharking %s\n", file );
    fprintf( arch, "echo unsharking %s\n", file );

    if( S_ISDIR( info.st_mode ) ) archive_dir( file, &info );
    else if( S_ISREG( info.st_mode ) ) archive_file( file, &info );
    else if( S_ISFIFO( info.st_mode ) ) archive_pipe( file, &info );
    else if( S_ISBLK( info.st_mode ) ) archive_block_dev( file, &info );
    else if( S_ISCHR( info.st_mode ) ) archive_char_dev( file, &info );
    else if( S_ISLNK( info.st_mode ) ) archive_link( file, &info );
    else fprintf( stderr, "Unable to shark file: %s\n", file );
}


/* We will only be able to store MAX_FILES_OPEN sub-directory levels ... */

void archive_dir( char* file, struct stat *info )
{
    char path[LINE_SIZE+1];
    struct dirent *ent;
    DIR *dir;

    if( strcmp( file, "." ) && strcmp( file, ".." ) )
	fprintf( arch, "mkdir %s\n", file );

    dir = opendir( file );
    if( dir == NULL ) {
        perror( file );
        return;
    }

    while( ent = readdir( dir ) ) {
        ent->d_name[ent->d_reclen] = '\0';
	if( strcmp( ent->d_name, "." ) && strcmp( ent->d_name, ".." ) ) {
	    sprintf( path, "%s/%s", file, ent->d_name );
	    archive( path );
	}
    }

    closedir( dir );

    fprintf( arch, "chmod %o %s\n", 07777&info->st_mode, file );
}



/* Maybe a Bug: If a text file doesn't end with a '\n', it will be appended */

void archive_file( char* file, struct stat *info )
{
    char cmd[LINE_SIZE+1];
    FILE *fptr;
    int d, n, lc = '\n';
    unsigned char c;

    if( is_bin( file ) ) {
	sprintf( cmd, "uuencode %s %s", file, file );
	fptr = popen( cmd, "r" );
	if( fptr == NULL ) {
	    perror( cmd );
	    return;
	}
	fprintf( arch, "uudecode << '\\\\__END__OF__%s__FILE\\\\'\n", file );
    }
    else {
	fptr = fopen( file, "r" );
	if( fptr == NULL ) {
	    perror( file );
	    return;
	}
        fprintf(arch, "cat > %s << '\\\\__END__OF__%s__FILE\\\\'\n",file,file);
    }


    for(;;) {
	c = getc( fptr );
	if( feof( fptr ) ) break;
	putc( c, arch );
	lc = c;
    }

    if( lc != '\n' ) putc( '\n', arch );
    fprintf( arch, "\\\\__END__OF__%s__FILE\\\\\n", file );
    fprintf( arch, "chmod %o %s\n", 07777&info->st_mode, file );

    fclose( fptr );
}



void archive_pipe( char *file, struct stat *info )
{
    fprintf( arch, "mknod -m %o %s p\n", file, 07777&info->st_mode );
}



void archive_block_dev( char *file, struct stat *info )
{
    fprintf( arch, "mknod -m %o %s b %d %d\n", 07777&info->st_mode, file,
    	     (info->st_rdev&0xff00)>>8, info->st_rdev & 0xff );
}



void archive_char_dev( char *file, struct stat *info )
{
    fprintf( arch, "mknod -m %o %s c %d %d\n", 07777 & info->st_mode, file,
    	     (info->st_rdev&0xff00)>>8, info->st_rdev & 0xff );
}



void archive_link( char *file, struct stat *info )
{
    char path[LINE_SIZE+1];
    int n;

    n = readlink( file, path, LINE_SIZE );

    if( n <= 0 ) {
	perror( file );
	return;
    }

    path[n] = '\0';
    fprintf( arch, "ln -s %s %s\n", path, file );
}


/*Test if a file is binary: Fails if the first SCAN_SIZE chars are all ASCII.*/

int is_bin( char *file )
{
    int n, d;
    char buff[SCAN_SIZE+1];

    d = open( file, O_RDONLY );
    if( d == -1 ) {
	perror( file );
	return;
    }

    n = read( d, buff, SCAN_SIZE );
    close( d );

    while( --n >= 0 ) if( buff[n] != '\0' && !isascii( buff[n] ) ) return 1;

    return 0;
}
\\__END__OF__shark/shark.c__FILE\\
chmod 644 shark/shark.c
echo unsharking shark/shark
uudecode << '\\__END__OF__shark/shark__FILE\\'
begin 755 shark/shark
M"P%D```0````$```````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M`````````````````````````````````````````````.A/"P``N"T```"[
M`````,V`HUP+"6"+1"0(HS0+"6`/MP44$```4.A`#```@\0$Z'@,``#H>P$`
M`%#HD0,`8%NX`0```,V`Z_>0D)"0D)"0`)"0D'<`=7-A9V4Z('-H87)K(%LM
M;R!O=71P=70M9FEL95T@9FEL97,*`",A+V)I;B]S:`H`(R,C(R,C(R,C(R,C
M(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,C(R,*
M`",@5&AI<R!I<R!A('-H96QL(&%R8VAI=F4@("TM+2!S:&%R:R`P+C$N,2`M
M+2T@("`@("`@("`C"@`C(%!L96%S92!R96UO=F4@86YY(&QI;F5S(&)E9F]R
M92!T:&ES(&AE861E<B!A;F0@("`@("`@(PH`(R!R=6X@("`@('-H('1H:7,M
M9FEL92UN86UE("`@("!T;R!E>'1R86-T(&%L;"!F:6QE<RX@(",*`",@,3DY
M-"`H0RD@1F5R;F%N9&\@2B!'(%!E<F5I<F$@+2!F:G!`;6EN97)V82YI;F5S
M8RYP="`C"@"0D)"0D)"0D)"0D)!5B>575E.+?0SH4@@``+X!````@W\$``^$
MEP```(M'!(`X+0^%BP```(U?!(L#BD`!/&AT2SQO=4>#PP1&@SL`=0YJ`>C3
M`0!@D)"0D)"0D&A4````BQ-2Z%\"`&"C!!```&CM`0``4.@W`@!@@\0$4.BN
M`0!@@\00ZR60D)"0D&A6````:-0'"6#H10(`8&H`Z(8!`&"0D)"0D)"0D)"0
M@\,$1H,[`'0+BP.`."T/A'C___]H>P```(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7\^___5E/H)`(`8(/$#(7`?PE3Z!<!`&#K')#&
MA"C\^___`%-6:)<(``"+%000``!2Z)G[_U^-I?3[__];7HGL7<.0D)"0D)"0
MD)!5B>6![`0(``!64XM="&H`4^B.``!@B<:#Q`B#_O]U(%/HO@``8.M)D)"0
MD)"0D)"X`0```.LZD)"0D)"0D)"0:``(``"-A?SW__]05NB"`0!@B<-6Z/+X
M_U_K$)"0D)"`O"O\]___`'0"?,1+>?$QP(VE]/?__UM>B>Q=PY"0D%6)Y5.A
M>!```(/X_W49,<"#/7P0````=`Z0D)!`@SR%?!````!U]8G#A=MT#Y"0D(L$
MG7@0``#_T$MU](M=_(GL7<.0D)"0D)"0D)"0D)"058GE4[M,$```@SU,$```
M`'0.D)"+`X/#!/_0@SL`=?1HA`D``.B:]_]?BUW\B>Q=PY"0D)"0D)"0D)"0
MD)"0D%6)Y8,]#!````!U#\<%#!````$```#HI?___XGL7<.04[@!````BUPD
M",V`A<!]$/?8HQP0``"X_____UO#D)!;PY"0D)"0D)"0D)"0D)"04[A6````
MBUPD",V`A<!]$/?8HQP0``"X_____UO#D)!;PY"0D)"0D)"0D)"0D)"04[@$
M````BUPD"(M,)`R+5"00S8"%P'T8]]BC'!```+C_____6\.0D)"0D)"0D)"0
M6\.0D)"0D)"0D)"0D)"0D%.X6P```(M<)`B+3"0,S8"%P'T<]]BC'!```+C_
M____6\.0D)"0D)"0D)"0D)"0D%O#+VQI8B]L9"YS;P`Z(&-A;B=T(&QO860@
M9'EN86UI8R!L:6YK97(@)R]L:6(O;&0N<V\G"@`)<W1A=&EC86QL>2!L:6YK
M960*`)"0D)"0@^PX55=64XML)$R+7"10@SU`$`````^$M````,=$)$0@`/!B
M:`8+``#HXO[__X/$!(7`=%N+`XU\)!B^$0L``/RY"@```/.E9J6%P'0<@#@`
M=`>00(`X`'7Z*P-0BQM3:@+HV?[__X/$#&HJC40D'%!J`NC(_O__@\0,D&B`
M````Z%K^__^#Q`3K\9"0D)"0:%00``!H.!```(M4)%Q2BQM3C40D(%"X`@``
M`(7M?P6X`0```%"+1"1<_]"+5"0L4HM4)"Q2Z+7^__^#Q"#K)HU\)!B^.PL`
M`/RY!0```/.EA>U_*VH4C40D'%!J`NA-_O__@\0,A>U_%I"0:@#HW?W__X/$
M!.OTD)"0D)"0D)!;7E]=@\0XPX/L!&:+5"0(9H72=06Z<A,``-E\)`)FBT0D
M`F8EP/!FB40D`HG09B4_#V:+5"0"9@G09HE$)`+9;"0"@\0$PY!55U93BVPD
M%(M\)!B+="0<NX00``"#/800````=!20D%9758L#_]"#Q`R#PP2#.P!U[EM>
M7UW#D&QI8F,N<V\N-`!$3$P@2G5M<"`T+C5P;#$Y`)``````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M``````````````````````````````````````````````#3'NO^B`8)8!`0
M````````````````D)````````````````#D#```[@P``````&`'`@0``/`(
M8`(```#\#P``)!`````````!````^#\`8``````#`````!```'`0```T$```
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
H````````````````````````````````````````````````!```````
`
end
\\__END__OF__shark/shark__FILE\\
chmod 755 shark/shark
echo unsharking shark/Makefile
cat > shark/Makefile << '\\__END__OF__shark/Makefile__FILE\\'
shark: shark.c
	cc -O2 -o shark shark.c
	strip shark
\\__END__OF__shark/Makefile__FILE\\
chmod 644 shark/Makefile
echo unsharking shark/shark.lsm
cat > shark/shark.lsm << '\\__END__OF__shark/shark.lsm__FILE\\'
Begin2
Title        = shark - shell archiver
Version      = 0.1.1
Desc1        = Simple SHELL ARCHIVER: usefull to pack data in news/mail messages
desc2        = Stores files, recursive directories, char/block devices, pipes,
Desc3        = symbolic links, saves permitions, and auto-uuencodes binary files.
Desc4        = The resulting package is shell script that unpackages itself.
Desc5        = This is an alfa release - so you must be very carefull ...
Author       = Fernando J. G. Pereira
AuthorEmail  = fjp@minerva.inesc.pt
Maintainer   = Fernando J. G. Pereira
MaintEmail   = fjp@minerva.inesc.pt
Site1        = sunsite.unc.edu
Path1        = 
File1        = shark.sh
FileSize1    = 21k
Site2        = nic.funet.fi
Path2        =
File2        = shark.sh
FileSize2    = 21k
Site3        = 
Path3        =
File3        =
FileSize3    =
Site4        =
Path4        =
File4        =
FileSize4    =
Required1    = Linux
Required2    = 
Required3    =
Required4    =
CopyPolicy1  = Free
CopyPolicy2  =
Keywords     = shell-archive - shell-scripts
Comment1     =
Comment2     =
Comment3     =
Comment4     =
RelFiles1    = 
RelFiles2    =
RelFiles3    =
Entered      = 16/2/94
EnteredBy    = fjp
CheckedEmail =
End
\\__END__OF__shark/shark.lsm__FILE\\
chmod 644 shark/shark.lsm
chmod 755 shark
