//
// storesys.c
//
//   This module archives a system saved with "savesys" into the
//   current working directory.
//

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

#define MAXSTRING             80
#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 *file );

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

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

char *tape;
char boot[ MAXSTRING ];
char buffer[ MAXSTRING ];

int main( int argc, char **argv )
     {
     time_t start_time;
     time_t end_time;
     int index;
     FILE *fp;

     start_time = time( NULL );

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

     if( argc == 2 )
          tape = argv[ 1 ];
     else
          tape = "/dev/rmt/c1t1d0s0n";

     sprintf( buffer, "mt -f %s rewind", tape );
     sys( buffer );

     sprintf( buffer, "cpio -icvdumB -I %s", tape );
     sys( buffer );
     sys( "echo *.snd >scratch" );
     fp = fopen( "scratch", "r" );
     fscanf( fp, "%s", buffer );
     fclose( fp );

     readinfo( buffer );

     for( index = 0; index < fs_count; ++index )
          {
          if( strcmp( fs[ index ].mount, "/tmp" ))
               {
               sprintf( buffer, "dd if=%s bs=5120 >%s.backup", tape, 
                       fs[ index ].slice );
               sys( buffer );
               }
          }

     end_time = time( NULL );
     index = end_time - start_time;
     printf( "Elapsed time: %d mins %d secs\n", index / 60, index % 60 );
     printf( "storesys completed successfully.\n" );

     return( 0 );
     }

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

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

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

     strcpy( boot, "c0t6d0" );

        // Get file system descriptions
        fp = fopen( file, "r" );
        if( ! fp )
                {
                printf( "storesys: 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:
                        if( ! strcmp( "/", fs[ fs_count ].slice ))
                                {
                                strcpy( boot, fs[ fs_count ].mount );
                                boot[ strlen( boot ) - 2 ] = 0;
                                }

                        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


