/*   See backup for volume directory information
 * 
 *   Args
 * 
 *        -b ttt  = name of both rewinding & non-rewinding tape device
 *        -f ttt  = name of rewinding tape device
 *        -i x    = where the archive information files are
 *        -l fff  = name of log file
 *        -m num  = level of logging (0=none...3=verbose)
 *        -n ttt  = name of non-rewinding tape device
 *        -p xx   = name of preferences file (default = .taper in $HOME/.taper)
 *        -v      = prints version
 *        -x num  = maximum # of bytes to transfer to/from device in one hit
 *        -?      = prints command line help
 *
*/

#include "common.h"

int info_fd;


void usage()
{
    fprintf(stderr, "\n\n");
    fprintf(stderr, "Mkinfo command line options\n");
    fprintf(stderr, "----------------------------\n");
    common_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++;
    }
}


inline void write_info(char *buf, long szbuf, char *lmes)
{
    if (write(info_fd, buf, szbuf) != szbuf) {
	write_error_log(lmes);
	do_exit(ERROR_WRITING_INFO);
    }
}

int mkinfo_action(struct file_info *fi, char *fn)
{
    char l[300];
    
    strcpy(l, "Writing filename & file info");
    if (log_level == 3) write_log(l);
    write_info((char *) fi, sizeof(*fi), l);
    write_info(fn, fi->name_len, l);
    ifd.info_file_size += sizeof(*fi)+fi->name_len;
    ifd.no_in_archive++;
    return 0;
}


long file_no;					 /* for cumulative  */
long tr;					 /* info, not volume based */
void mkinfo_print_status(WINDOW *mes_box, long cur_in_vol, 
			 long file_size, char *fn,
			 time_t t_start, time_t t_current)
{
    char num1[50], scr[MAXNAMLEN];
    
    sprintf(scr, "Passing %s", fn);
    status_box(mes_box, scr, 0, FALSE, 1);
    file_no++;
    sprintf(scr, "File %ld", file_no);
    status_box(mes_box, scr, 2, FALSE, 1);
    sprintf(scr, "Byte %s", convert(num1, tr));
    status_box(mes_box, scr, 3, FALSE, 1);
    sprintf(scr, "Time elasped %s", convtime(t_start, t_current));
    status_box(mes_box, scr, 5, FALSE, 1);
    tr += sizeof(struct file_info)+strlen(fn)+1+file_size;
    sprintf(scr, "Average restore/mkinfo rate %ld K/sec",
	    ((t_current-t_start) ? (tr/1024)/(t_current-t_start) : 0));
    status_box(mes_box, scr, 6, FALSE, 1);
}


void recreate() 
{
    WINDOW *mes_box;
    char   l[MAXNAMLEN];

    file_no=0;
    tr = 0;
    mes_box = status_box(mes_box, "Rewinding tape", 2, TRUE, 4);/* create window */
    tape_rewind();
    status_box(mes_box, "Identifying tape", 2, FALSE, 1);
    dv =tape_open(O_RDWR);			 /* open rewinding device */
    if (dv == -1) 				 /* so that after this procedure */
      do_exit(ERROR_OPENING_BACKUP);
    tape_readheader(&tdh, 0);
    if (tdh.magic != TAPER_MAGIC_NUMBER) 
      do_exit(ERROR_MAGIC);
    close_statusbox(mes_box);

    print_title_line();				 /* refresh title line */
    info_fd = open_info_file(FALSE, tdh.archive_id);
    ifd.archive_id = tdh.archive_id;
    strcpy(ifd.archive_title, tdh.archive_title);
    ifd.number_tapes = 1;
    ifd.info_file_size = sizeof(ifd);
    ifd.no_in_archive = 0;
    ifd.number_volumes = 1;
    vt_info = my_realloc(vt_info, sizeof(struct volume_tape_info));
    vt_info->volume = 1;			 /* first volume always on tape 1 */
    vt_info->tape = 1;

    write_info((char *) &ifd, sizeof(ifd), "Writing info file header");

    common_restore_loop(mkinfo_action, -1, mkinfo_print_status);
    tape_close(dv);
    write(info_fd, vt_info, sizeof(struct volume_tape_info)*ifd.number_volumes);

    lseek(info_fd, 0, SEEK_SET);		 /* update info file */
    strcpy(l, "Updating info file header");
    write_info((char *) &ifd, sizeof(ifd), l);
    close(info_fd);
}

    
void init_windows() {
/*  Initialise windows and print screen edges etc..      */

    win_main = initscr();			 /* Initialize the cuwrsor library */    
    raw(); noecho(); cbreak();
    start_color(); setup_colors();

    screen_xlen=win_main->_maxx;		 /* Dimensions of screen */    
    screen_ylen=win_main->_maxy;

    left_width = screen_xlen; 
    right_width = 0;
    
    screen_ylen_files = screen_ylen/2 +4;
    screen_ylen_selection = screen_ylen - screen_ylen_files;

    title = newwin(1, screen_xlen, 0, 0);	 /* title bar */
    files = newwin(screen_ylen, screen_xlen,
		  1, 0);			 

    my_werase(files, COLOR_MAIN); wrefresh(files);
    curs_set(0);
}


int main(int argc, char *argv[]) 
{
    init_memory();
    vt_info = my_malloc(1);
    read_environment();
    read_prefs_file(argc, argv);
    parse_args(argc, argv);
    fixup_device_names();
    init_buffers();
    init_windows();
    open_logfile("Mkinfo");
    recreate();
    my_free_all();
    my_werase(win_main, COLOR_MAIN); wrefresh(win_main);
    final_message("Mkinfo");
    delwin(files); delwin(title);
    werase(win_main); wrefresh(win_main);
    endwin();
    close_logfile("Mkinfo");
    return 0;			                 /* succesful return */
}

    
