//
// cut_tape.c
//
// This little program builds media for a shrinkwrap tape installation.
//

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>

#define MAXSTRING                       256
#define MAXDISKS                        10
#define MAXFILESYSTEMS  50

/*
Template looks like this:

slice           file-system             mount-point
*/

// Structure definitions
struct file_system
        {
        char slice[ MAXSTRING ];
        char type[ MAXSTRING ];
        char mount[ MAXSTRING ];
        };

// Prototypes
void sys( char *command );
void readinfo( char * );

// Global declarations
struct file_system fs[ MAXFILESYSTEMS ];
int fs_count = 0;

char disk[ MAXDISKS ][ MAXSTRING ];
int disk_count = 0;

char buffer[ MAXSTRING ];
char response[ MAXSTRING ];
char bin[ MAXSTRING ];
char *ltape;

void main( int argc, char **argv  )
     {
     int index;
     FILE *fp;

     if(( argc != 2 ) && ( argc != 1 ))
          {
          printf( "Usage: cut_tape [local-tape-device]\n" );
          exit( -1 );
          }

     if( argc == 2 )
          ltape = argv[ 1 ];
     else
          ltape = "/dev/rmt/c1t1d0s0n";
     if( ltape[ strlen( ltape ) - 1 ] != 'n' )
          {
          printf( "Tape device must end with an 'n'\n" );
          exit( -2 );
          }

     printf( "Shrinkwrap - cut_tape\n" );
     printf( "Rick Moore BSE\n\n" );

     sys( "ls *.snd >scratch" );
     fp = fopen( "scratch", "r" );
     fscanf( fp, "%s", buffer );
     fclose( fp );
     readinfo( buffer );

     sys( "/usr/ucb/which cut_tape >scratch" );
     fp = fopen( "scratch", "r" );
     fscanf( fp, "%s", bin );
     fclose( fp );
     for( index = strlen( bin ); index && 
         ( bin[ index ] != '/' ); --index );
     bin[ index ] = 0;

     printf( "\nInsert tape media into device %s.  Press [Enter] 
            when ready. ", ltape );
     gets( response );
     sprintf( buffer, "mt -f %s rewind", ltape );
     sys( buffer );

     sprintf( buffer, "cpio -ocvdumB <stage.flist -O %s", ltape );
     sys( buffer );
     
     for( index = 0; index < fs_count; ++index )
          if( strcmp( "/tmp", fs[ index ].mount ))
               {
               sprintf( buffer, "dd bs=5120 of=%s <%s.backup", 
                       ltape, fs[ index ].slice );
               sys( buffer );
               }
     }

void sys( char *command )
        {
        int result = 0;

        printf( "%s\n", command );
        result = system( command );
        if( result )
                {
                printf( "savesys: '%s' command failed error number
                       %d\n", command, errno );
                exit( -3 );
                }
        }

void readinfo( char *file )
        {
        FILE *fp;
        int index;
        int i;

        // Get file system descriptions
        fp = fopen( file, "r" );
        if( ! fp )
                {
                printf( "savesys: Could not open file system 
                       description file '%s'\n", file );
                exit( -2 );
                }

        while( fgets( buffer, MAXSTRING, fp ))
                {
                if( buffer[ 0 ] == '#' )
                        continue;

                i = sscanf( buffer, "%s %s %s\n", fs[ fs_count ].slice,
                          fs[ fs_count ].type, fs[ fs_count ].mount );
                switch( i )
                        {
                case 1:
                        strcpy( disk[ disk_count ], 
                               fs[ fs_count ].slice );
                        disk_count++;
                        break;
                case 2:
                        fs[ fs_count ].mount[ 0 ] = 0;
                case 3:
                        fs_count++;
                        break;
                        }
                }
        fclose( fp );

        // Display results
        printf( "Disks:\n" );
        for( index = 0; index < disk_count; ++index )
                printf( "\t%s\n", disk[ index ] );

        printf( "\nSlices:\n" );
        for( index = 0; index < fs_count; ++index )
                printf( "\t%s\t%s\t%s\n", fs[ index ].slice,
                       fs[ index ].type, fs[ index ].mount );
        printf( "\n" );
        }

// End of File


