/*

/usr/src/ext2ed/group_com.c

A part of the extended file system 2 disk editor.

General user commands

First written on: April 17 1995

Copyright (C) 1995 Gadi Oxman

*/

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

#include "ext2ed.h"

void type_ext2_group_desc___next (char *command_line)

{
	long entry_offset=1;
	char *ptr,buffer [80];

	ptr=parse_word (command_line,buffer);
	if (*ptr!=0) {
		ptr=parse_word (ptr,buffer);
		entry_offset=atol (buffer);
	}

	sprintf (buffer,"entry %ld",group_info.group_num+entry_offset);
	dispatch (buffer);
}

void type_ext2_group_desc___prev (char *command_line)

{
	long entry_offset=1;
	char *ptr,buffer [80];

	ptr=parse_word (command_line,buffer);
	if (*ptr!=0) {
		ptr=parse_word (ptr,buffer);
		entry_offset=atol (buffer);
	}

	sprintf (buffer,"entry %ld",group_info.group_num-entry_offset);
	dispatch (buffer);
}

void type_ext2_group_desc___entry (char *command_line)

{
	long group_num;
	char *ptr,buffer [80];

	ptr=parse_word (command_line,buffer);
	if (*ptr==0) {
		wprintw (command_win,"Error - No argument specified\n");refresh_command_win ();return;
	}
	ptr=parse_word (ptr,buffer);

	group_num=atol (buffer);

	if (group_num < 0 || group_num >= file_system_info.groups_count) {
		wprintw (command_win,"Error - Entry number out of bounds\n");refresh_command_win ();return;
	}

	device_offset=file_system_info.first_group_desc_offset+group_num*sizeof (struct ext2_group_desc);

	sprintf (buffer,"setoffset %ld",device_offset);dispatch (buffer);
	strcpy (buffer,"show");dispatch (buffer);
	group_info.group_num=group_num;
}


void type_ext2_group_desc___gocopy (char *command_line)

{
	unsigned long copy_num,offset;
	char *ptr,buffer [80];

	ptr=parse_word (command_line,buffer);
	if (*ptr==0) {
		wprintw (command_win,"Error - No argument specified\n");refresh_command_win ();return;
	}
	ptr=parse_word (ptr,buffer);

	copy_num=atol (buffer);

	offset=file_system_info.first_group_desc_offset+copy_num*file_system_info.super_block.s_blocks_per_group*file_system_info.block_size;

	if (offset > file_system_info.file_system_size) {
		wprintw (command_win,"Error - Copy number out of bounds\n");refresh_command_win ();return;
	}

	group_info.copy_num=copy_num;
	device_offset=offset+group_info.group_num*sizeof (struct ext2_group_desc);

	sprintf (buffer,"setoffset %ld",device_offset);dispatch (buffer);
	strcpy (buffer,"show");dispatch (buffer);
}


void type_ext2_group_desc___show (char *command_line)

{
	long group_num,temp;

	temp=(device_offset-file_system_info.first_group_desc_offset) % (file_system_info.super_block.s_blocks_per_group*file_system_info.block_size);
	group_num=temp/sizeof (struct ext2_group_desc);

	show (command_line);

	wmove (show_win,1,0);wprintw (show_win,"\n");wmove (show_win,2,0);
	wprintw (show_win,"Group %ld of %ld ",group_num,file_system_info.groups_count-1);
	wprintw (show_win,"in copy %ld ",group_info.copy_num);
	if (group_info.copy_num==0) wprintw (show_win,"(Main copy)");
	wprintw (show_win,"\n");refresh_show_win ();

	if (group_num==0) {
		wprintw (command_win,"Reached first group descriptor\n");
		wrefresh (command_win);
	}

	if (group_num==file_system_info.groups_count-1) {
		wprintw (command_win,"Reached last group descriptor\n");
		wrefresh (command_win);
	}
}

void type_ext2_group_desc___inode (char *command_line)

{
	long inode_offset;
	char buffer [80];

	inode_offset=type_data.u.t_ext2_group_desc.bg_inode_table;
	sprintf (buffer,"setoffset block %ld",inode_offset);dispatch (buffer);
	sprintf (buffer,"settype ext2_inode");dispatch (buffer);
}

void type_ext2_group_desc___blockbitmap (char *command_line)

{
	long block_bitmap_offset;
	char buffer [80];

	block_bitmap_info.entry_num=0;
	block_bitmap_info.group_num=group_info.group_num;

	block_bitmap_offset=type_data.u.t_ext2_group_desc.bg_block_bitmap;
	sprintf (buffer,"setoffset block %ld",block_bitmap_offset);dispatch (buffer);
	sprintf (buffer,"settype block_bitmap");dispatch (buffer);
}

void type_ext2_group_desc___inodebitmap (char *command_line)

{
	long inode_bitmap_offset;
	char buffer [80];

	inode_bitmap_info.entry_num=0;
	inode_bitmap_info.group_num=group_info.group_num;

	inode_bitmap_offset=type_data.u.t_ext2_group_desc.bg_inode_bitmap;
	sprintf (buffer,"setoffset block %ld",inode_bitmap_offset);dispatch (buffer);
	sprintf (buffer,"settype inode_bitmap");dispatch (buffer);
}

void type_ext2_group_desc___setactivecopy (char *command_line)

{
	struct ext2_group_desc gd;

	gd=type_data.u.t_ext2_group_desc;
	dispatch ("gocopy 0");
	type_data.u.t_ext2_group_desc=gd;
	dispatch ("show");
}