/*   MKTAPE
 * 
 *   See backup for volume directory information
 * 
 *   This program erases a tape and then writes a taper header to
 *   the tape. To signify that the tape is empty, tdh.archive_id = 0
 * 
 *   Args
 * 
 *        -b ttt  = name of both rewinding & non-rewinding tape device
 *        -f ttt  = name of rewinding tape device
 *        -n ttt  = name of non-rewinding tape device
 *        -v      = prints version
 *        -?      = prints command line help
 *
*/

#include "common.h"



void usage()
{
    fprintf(stderr, "\n\n");
    fprintf(stderr, "Mktape command line options\n");
    fprintf(stderr, "----------------------------\n");
    mktape_usage();
    do_exit(ERROR_USAGE);
}

						 
void parse_args(int argc, char *argv[])
{
    int c=1;
    char z;
    
    while (c<argc) {
	z = common_parse_args(argc,argv, &c);	 /* look for common args */
	if (z==-1) usage();
	if (z==0) 
	    usage();
	c++;
    }
}






int main(int argc, char *argv[]) 
{
    char s[100];
    
    read_environment();
    read_prefs_file(argc, argv);
    parse_args(argc, argv);
    fixup_device_names();
    printf("\n\nMKTAPE:  This will erase ALL existing data on tape.\nProceed ?");
    while (1) {
	scanf("%s", s);
	if (!strcasecmp(s, "yes") || (!strcasecmp(s, "no")))
	  break;
	printf("Please type yes or no\n\nProceed ?");
    }
    printf("\nRewinding tape..\n"); tape_rewind(); 
    printf("Opening tape..\n"); dv=tape_open(O_RDWR); 
    printf("Erasing tape..\n"); tape_erase(dv); 
    printf("Closing tape..\n"); close(dv); 
    printf("\nRewinding tape..\n"); tape_rewind(); 
    tdh.magic = TAPER_MAGIC_NUMBER;
    tdh.archive_id = 0;
    tdh.tape_number = 0;
    memset(tdh.archive_title, 0, MAX_ARCHIVETITLE);
    printf("Opening tape..\n"); dv=tape_open(O_RDWR); 
    if (dv == -1) do_exit(ERROR_OPENING_BACKUP);
    printf("Writing tape header..\n"); write(dv, &tdh, sizeof(struct tape_header));
    
    printf("Closing tape..\n\n\n"); close(dv);
    return 0;			                 /* succesful return */
}

    
