#! /usr/bin/perl -w
use feature ':5.10';
use strict 'vars';

use MARC::Record;
use MARC::Batch;

my @tags=("1..","24.","6..","7..");	#MARC tags to pull.

my $FileID="0_2";

my $batch= MARC::Batch->new('USMARC',"amdbRepaired".$FileID.".txt");
$batch->strict_off();
$batch->warnings_off();

my $record;
my $bibid;
my $field;

open(OUTPUT,">","SomeFields".$FileID.".txt");
print OUTPUT ("BIB_ID\tTag\tInd\tField\n");
RECORDS: while ($record = $batch->next()){
	#next RECORDS if !defined($record->fields("001"));	#Jump to next record if 001 not found.
	TAGS: foreach my $tag(@tags){
		$bibid=$record->field("001")->as_string;
		foreach $field($record->field($tag)){
			print OUTPUT (
				$bibid,
				"\t",
				$field->tag(),
				"\t",
				defined $field->indicator(1) ? $field->indicator(1) : " ",
				defined $field->indicator(2) ? $field->indicator(2) : " ",
				"\t",
				$field->as_string,"\n"
			)
		}
	}
}

close(OUTPUT);

#Version of decode below courtesy of:  Al  ratty@berkeley.edu
package Encode;
use Encode::Alias;

sub decode($$;$)
{
  my ($name,$octets,$check) = @_;
  my $altstring = $octets;
  return undef unless defined $octets;
  $octets .= '' if ref $octets;
  $check ||=0;
  my $enc = find_encoding($name);
  unless(defined $enc){
     require Carp;
     Carp::croak("Unknown encoding '$name'");
  }
  my $string;
  eval { $string = $enc->decode($octets,$check); };
  $_[1] = $octets if $check and !($check & LEAVE_SRC());
  if ($@) {
     return $altstring;
  } else {
     return $string;
  }
}
