my $signatura = 
	lookup(
		sub { rec('C') },
		'casopisi','sig',
		sub { rec('A') . rec('B') },
		sub { config ('input collection') . rec('000') }
	);

if ( $signatura ) {


### LEADER

## LDR 05 - n - new
marc_leader('05','n');

## LDR 06 - a - language material 
marc_leader('06','a');

## LDR 07 - Bibliographic level 
#  izvuci podatke iz tablie (za po i sf)

my $ldr7 =
	lookup(
		sub { rec('D') },
		'casopisi','kod',
		sub { rec('B') . rec('A') },
		sub { config ('input collection') . rec('000') }
	);

#if ( rec('225','a') ) {
#	marc_leader('07','d');	
#} else {
#	marc_leader('07','s');	
#}

if ( $ldr7 ) {
	marc_leader('07', $ldr7 );
} else {
	marc_leader('07','s');
}


## LDR 17 - Encoding level ; 7 - minimal level, u - unknown
marc_leader('17','u');

## LDR 18 - i - isbd 
marc_leader('18','i');


## LDR 19 - Multipart resource record level ; # - Not specified or not applicable, a - Set, b - Part with independent title, c - Part with dependent title 
# tematski brojevi?

### 008 - All materials

marc_fixed('008','00', '090820');	# 00-05 - Date entered on file

## za 008 06 treba i lookup u tablicu gdje postoji podatak o tome jel tekuci casopis ili nije?
## c - Continuing resource currently published
## d - Continuing resource ceased publication
## u - Continuing resource status unknown

marc_fixed('008','06','|');

my $d1 = '    ';
my $d2 = '    ';

if ( rec('210','c') ) {
	if ( rec ('210','c') =~ m#(\d{4}).*?-.*?(\d{4})# ) {
		$d1 = $1;
		$d2 = $2;
		marc_fixed('008','06','d');
	}
	if ( rec ('210','c') =~ m{(\d{4}).*?-\s*$} ) {
		$d1 = $1;
		$d2 = '9999';
		marc_fixed('008','06','c');
	}
	
	marc_fixed('008','07', $d1);
	marc_fixed('008','11', $d2);
		
}

marc_fixed('008','15',  		# 15-17 - Place of publication, production, or execution
	rec('102')
);

marc_fixed('008','35',
	first( lc(rec('101')) )		# 35-37 - Language
);

marc_fixed('008','38','|');		# 38 - Modified record
marc_fixed('008','39','d');		# 39 - Cataloging source

### 008 - Continuing resources

my $fre;

$fre = 	( rec('326') eq '2g' )    ? 'g' :
	( rec('326') eq 'g1' )    ? 'a' :
	( rec('326') eq 'g2' )    ? 'f' :
	( rec('326') eq 'g3' )    ? 't' :
	( rec('326') eq 'g4' )    ? 'q' :
	( rec('326') eq 'g5' )    ? 'z' :
	( rec('326') eq 'g6' )    ? 'b' :
	( rec('326') eq 'g8' )    ? 'b' :
	( rec('326') eq 'g9' )    ? 'm' :
	( rec('326') eq 'm1' )    ? 'm' :
 	( rec('326') eq '10' )    ? 'm' :
 	( rec('326') eq '11' )    ? 'm' :
	( rec('326') eq 'm2' )    ? 's' :
	( rec('326') eq '19' )    ? 's' :
 	( rec('326') eq '20' )    ? 's' :
 	( rec('326') eq '26' )    ? 's' :
	( rec('326') eq 'm3' )    ? 'j' :
	( rec('326') eq 't1' )    ? 'w' :
	( rec('326') eq 't2' )    ? 'c' :
	( rec('326') eq 't2us' )  ? 'c' :
	( rec('326') eq 'nr' )    ? ' ' : 	
				    '|'
;

marc_fixed('008','18', $fre);	# 18 - Frequency - ovo treba popuniti iz polja 326
marc_fixed('008','19', '|');	# 19 - Regularity
				# 20 - Undefined, contains a blank (#) or fill character (|)
# za polje 008 21 treba lookup u tablicu s kodovima za sf i po, za ostale nadopuniti naknadno

my $stype = 
	lookup(
		sub { rec('E') },
		'casopisi','kod',
		sub { rec('B') . rec('A') },
		sub { config ('input collection') . rec('000') }
	);

if ( $stype ) {
	marc_fixed('008','21', $stype);	# 21 - Type of continuing resource
} else {
	marc_fixed('008','21', 'p');
}
				# 22 - Form of original item - blank
				# 23 - Form of item
marc_fixed('008','24','|');	# 24 - Nature of entire work
marc_fixed('008','25','|||');	# 25-27 - Nature of contents
				# 28 - Government publication
marc_fixed('008','29','|');	# 29 - Conference publication
				# 30-32 - Undefined
marc_fixed('008','33','|');	# 33 - Original alphabet or script of title
marc_fixed('008','34','0');	# 34 - Entry convention

### 022

#if ( ! rec('225','a') ) {
	marc('022','a',
		rec('11')
	);
#}

### 035

my $id;

$id = ( 
	join_with('',
		'HR-ZaFF ',
		config('input name'),
		'P',
		rec('000')
	)
);

marc('035','a', $id);

my $csigid = lookup(
		sub { prefix('HR-ZaFF csig', rec('I') ) },
		'casopisi','sig',
		sub { rec('A') . rec('B') },
		sub { config ('input collection') . rec('000') }
	);
	 
marc('035','a', $csigid);


### 040
# za sve je isti
# treba utvrditi tocnu oznaku za ustanovu

marc('040','a',
	'HR-ZaFF'
);

marc('040','b',
	'hrv'
);

marc('040','c',
	'HR-ZaFF'
);

marc('040','e',
	'ppiak'
);

#### 041 - indikatori
#
#
#marc_indicators('041', 0, ' ');
#
#### 041
#
#marc_repeatable_subfield('041','a',
#	rec('101')
#);


#_debug(3);

### 080

marc('080','a',
	rec('675','a')
);

marc('080','a',
	rec('675','b')
);

marc('080','a',
	rec('675','c')
);

marc('080','a',
	rec('675','d')
);


### 210 indikatori
# marc_indicators('210', '1',' ');

### 210 - ABBREVIATED TITLE 
# pronaći prikladan izvor podataka (nsk baza, issn baza)

### 222 indikatori
# drugi indikator prema clanu

marc_indicators('222',' ','0');

### 222 - Key title
# pronaci prikadan izvor podataka - nsk baza, issn baze

marc('222','a',
	rec('530')
);

# iz nsk:
#if ( rec('11') ) {
#marc('222','a',
#	lookup(
#	 	sub { rec('530','a') },
# 		'nsk','bbaza',
# 		sub { rec('011','a') },
# 		sub { rec('11') },
# 	) 
#);
#}

### 245 indikatori
# generiraju se prema clanu, trebalo bi możda i prema jeziku (jezika nema uvijek). 
# potrebna naknadna kontrola

my $naslov = 
	lookup(
		sub { rec('E') },
		'casopisi','sig',
		sub { rec('A') . rec('B') },
                sub { config ('input collection') . rec('000') }
	);

marc_indicators('245', 0, 0);

if ( 	( $naslov =~ m/^Die /) || 
	( $naslov =~ m/^Das /) || 
	( $naslov =~ m/^Der /) || 
	( $naslov =~ m/^Les /) || 
	( $naslov =~ m/^The /) ) {
		marc_indicators('245', 0, 4);

} 

if (	( $naslov =~ m/^Un /) || 
	( $naslov =~ m/^An /) ||
	( $naslov =~ m/^La /) ||
	( $naslov =~ m/^Le /) ||
	( $naslov =~ m/^Il /) ) {
		marc_indicators('245', 0, 3);
} 

if (	( $naslov =~ m/^A /) || 
	( $naslov =~ m/^L'/) ) {
		marc_indicators('245', 0, 2);
} 

### 245


#warn $naslov;

my ($a,$b,$c);
$c = $2 if $naslov =~ s{(\s+/)\s+(.+)$}{$1};
if ( $naslov =~ s{^(.+?[:=])\s+}{} ) {
	$a = $1;
	$b = $naslov ;
} else {
	$a = $naslov ;
}

#warn "a: $a\nb: $b\nc: $c\n";

$a = $a . '.' if ( ! ( $b || $c || rec('200','f') ) );
$a = $a . ' /' if ( ! ($b || $c) && rec('200','f') );
$b = $b . '.' if ( $b && ! ( $c || rec('200','f') ) ) ;
$b = $b . ' /' if ( $b && ! $c && rec('200','f') ) ;

marc('245','a', $a);
marc('245','b', $b) if $b;
if ( $c ) {
	marc('245','c', 
		suffix('.', $c)
	);
} elsif ( rec('200','f') ) {
	marc('245','c',
		suffix('.', rec('200','f') )
	);
}

### 246 inikatori
# prema jeziku - provjera
#
#if ( 	( rec('200','a') =~ m/^Die /) || 
#	( rec('200','a') =~ m/^Das /) || 
#	( rec('200','a') =~ m/^The /) ) {
#		marc_indicators('246', 0, 4);
#} else {
#	marc_indicators('246', 0, 0);
#}

### 246
# povuci podatke iz prikladnog izvora (nsk, issn)
# 532 iz ISIS-a nije konvertirano
#marc('246','a',
#	rec('532')
#);

## iz nsk:
#	marc('246','a',
#		lookup(
#			sub { rec('532','a') },
#			'nsk','bbaza',
#			sub { rec('011','a') },
#			sub { rec('11') },
#		)
#	);

#_debug(0);


## 260 - izdavanje
# provjeriti interpunkcije !!, u NSK nesto ne stima

marc_original_order('260','210');

## lookup u NSK:
# if ( rec('11') ) {
# 	marc_repeatable_subfield('260','a',
# 		lookup(
# 			sub { rec('210','a') },
# 			'nsk','bbaza',
# 			sub { rec('011','a') },
# 			sub { rec('11') },
# 		) 
# 	);
# 	marc_repeatable_subfield('260','b',
# 		lookup(
# 			sub { rec('210','c') },
# 			'nsk','bbaza',
# 			sub { rec('011','a') },
# 			sub { rec('11') },
# 		) 
# 	);
# 	marc('260','c',
# 		lookup(
# 			sub { rec('210','d') },
# 			'nsk','bbaza',
# 			sub { rec('011','a') },
# 			sub { rec('11') },
# 		) 
# 	);
# } else {
# 	marc_original_order('260', '210');
# }


### 300

marc('300','a',
	suffix(
		( rec('215','a') && rec('215','c') )	?  ' : ' :
		( rec('215','a') && rec('215','d') )	?  ' ; ' :
		( rec('215','a') && rec('215','e') )    ?  ' + ' :
				   ' ',
			rec('215','a')
	)
);

marc('300','b',
	suffix(
		( rec('215','c') && rec('215','d') )	?  ' ; ' :
		( rec('215','c') && rec('215','e') )   	?  ' + ' :
				 		       	   ' ',
			rec('215','c')
	)
);

marc('300','c',
	suffix(
		rec('215','e') ? ' + ' :
				 ' ',
			rec('215','d')
	)
);

marc('300','e',
	rec('215','e')
);

### 310 - current stated publication frequency
# potrebno ujednaciti, koristi se i u polju 008

#my $fredesc;
#
#$fredesc = 	( rec('326') eq '2g' )    ? 'Dvogodišnje' :
#		( rec('326') eq 'g1' )    ? 'Godišnje' :
#		( rec('326') eq 'g2' )    ? 'Polugodišnje' :
#		( rec('326') eq 'g3' )    ? 'Tri broja godišnje' :
#		( rec('326') eq 'g4' )    ? 'Četvrtgodišnje' :
#		( rec('326') eq 'g5' )    ? 'Pet brojeva godišnje' :
#		( rec('326') eq 'g6' )    ? 'Dvomjesečno' :
#		( rec('326') eq 'g8' )    ? 'Osam brojeva godišnje' :
#		( rec('326') eq 'g9' )    ? 'Devet brojeva godišnje' :
#		( rec('326') eq 'm1' )    ? 'Mjesečno' :
#		( rec('326') eq '10' )    ? 'Deset brojeva godišnje' :
#		( rec('326') eq '11' )    ? 'Jedanaest brojeve godišnje' :
#		( rec('326') eq 'm2' )    ? 'Polumjesečno' :
#		( rec('326') eq '19' )    ? 'Devetnaest brojeva godišnje' :
#		( rec('326') eq '20' )    ? 'Dvadeset brojeva godišnje' :
#		( rec('326') eq '26' )    ? 'Dvadeset i šest brojeva godišnje' :
#		( rec('326') eq 'm3' )    ? 'Tri broja mjesečno' :
#		( rec('326') eq 't1' )    ? 'Tjedno' :
#		( rec('326') eq 't2' )    ? 'Polutjedno' :
#		( rec('326') eq 't2us' )  ? 'Polutjedno utorkom u subotom' :
#		( rec('326') eq 'nr' )    ? 'Neredovito' :
#					    ' '
#;

#warn $fredesc;

my $fredesc = 
	lookup(
		sub { rec('B') },
		'casopisi','frek',
		sub { rec('A') },
		sub { rec('326') },
	);

marc('310','a',
	$fredesc
);

### 321 - former frequency of either an item or an update to an item when a current frequency is contained in field 310

# marc('321','a',
# 	ovo bi trebalo procitati iz NSK konvertiranih podataka
#);

marc_indicators('362', 0, ' ');

### 362
marc('362','a',
	rec('207','a'),
);

## lookup iz nsk
#else {
#	if ( rec('11') ) {
#		marc('362','a',
#			lookup(
#				sub { rec('207','a') },
#				'nsk','bbaza',
#				sub { first( rec('011','a') ) },
#				sub { rec('11') },
#			) 
#		);
#	}
#}

### 490 ind.

marc_indicators('490', '0', ' ');

### 490

marc('490','a',
	join_with('. ',
		join_with(' / ',
			join_with(' : ',
				join_with(' = ',
					rec('225','a'),
					rec('225','d')
				),
				rec('225','e')
			),
			join_with(' ; ',
				rec('225','f'),
				rec('225','g')
			),
		),
		rec('225','h')
	)
);

marc('490','v',
	rec('225','v')
);

marc('490','x',
	rec('225','x')
);

#if ( rec('11') ) {
#	marc('999','a',
#		lookup(
#			sub { rec('110','a'), },
#			'nsk','bbaza',
#			sub { rec('011','a') }, 
#			sub { rec('11') } 
#		)
#	);
#}


#marc('998','a',
#	lookup(
#		sub { rec('H') },
#		'ffsf','xls',
#		sub { rec('A') },
#		sub { $id },
#	),
#);


### 5xx
# koje podatke povuci iz drugih baza?

marc('500','a',
	rec('300','a'),
);
 

### 534 - napomena o pretisku

marc('534','a',
	rec('324')
);

## potencijalno za izvuci iz NSK
#if ( rec('11') ) {
#marc('546','a',
#	lookup(
# 		sub { rec('302','a') },
# 		'nsk','bbaza',
# 		sub { rec('011','a') },
# 		sub { rec('11') },
# 	) 
#);
#
#marc('550','a',
#	lookup(
#	 	sub { rec('314','a') },
# 		'nsk','bbaza',
# 		sub { rec('011','a') },
# 		sub { rec('11') },
# 	) 
#);
#
#marc('555','a',
#	lookup(
#	 	sub { rec('320','a') },
# 		'nsk','bbaza',
# 		sub { rec('011','a') },
# 		sub { rec('11') },
# 	) 
#);
#
#
## marc('562','a',
##	lookup(
##	 	sub { rec('316','a') },
##		'nsk','bbaza',
## 		sub { rec('011','a') },
## 		sub { rec('11') },
## 	) 
## );
#}

### 653 indikatori
marc_indicators('653', ' ', ' ');

### 653

marc('653','a',
	rec('610')
);

# marc_indicators('655', ' ', 4);
# marc('655','a',
#	lookup(
#	 	sub { rec('608','a') },
#		'nsk','bbaza',
# 		sub { rec('011','a') },
# 		sub { rec('11') },
# 	) 
# );


#_debug(2);

### 700 indikatori

marc_indicators('700', 1, ' ');

### 700

marc('700','a',
	rec('702','a'),
);

marc('700','a',
	rec('702','c'),
);

marc('700','a',
	rec('702','e'),
);

#_debug(0);

### 710 indikatori

marc_indicators('110', '2', ' ');

### 710
marc('110','a',
	rec('710','a')
);
marc('110','b',
	rec('710','b')
);
if ( rec('710','c') ) {
	marc('110','c',
		surround('(', ')', rec('710','c'))
	);
}

### 770 indikatori

marc_indicators('770', 0, ' ');

### 770
marc('770','t',
	rec('421','a'),
);

marc('770','x',
	rec('421','b')
);

### 772 indikatori
marc_indicators('772', 0, ' ');

### 772
marc('772','t',
	rec('422','a'),
);
marc('772','x',
	rec('422','b'),
);

### 773 indikatori

#if ( rec('225','a') ) {
#
#marc_indicators('773', 0, ' ');
#
### 773
## da li je bitan redoslijed potpolja?

#marc('773','w',
#	lookup(
#		sub { rec('000') },
#		'ffsf','peri',
#		sub { rec('200','a') },
#		sub { 
#			regex('s/ ; //g',
#				rec('225','a')
#			)
#		},
#	)
#);
#
#}


### 777 indikatori

marc_indicators('777', 0, ' ');

### 777

marc('777','t',
	rec('423','a'),
);

marc('777','x',
	rec('423','b'),
);

### 780 indikatori

marc_indicators('780', 0, 0);

### 780
marc('780','t',
	rec('430','a'),
);

marc('780','x',
	rec('430','b'),
);

### 785 indikatori

marc_indicators('785', 0, 0);

### 785
marc('785','t',
	rec('440','a'),
);
marc('785','x',
	rec('440','b'),
);


### 856
marc('856','u',
	rec('856','u')
);

### 866

marc_indicators('866',' ', 0);

marc('866','a',
	rec('992')
);


### 942

marc('942','b',
	'PER'
);

marc('942','c',
	'PER'
);

marc('942','d',
	$signatura
);

marc('942','i',
	$signatura
);



## polja za probu


#marc('998','a',
#	lookup(
#		sub { rec('200','a') },
#		'ffiz','peri',
#		sub { rec('200','a'), },
#		sub { rec('225','a'), }
#	)
#);

 
# marc('997','a',
# 	sort(
#  		lookup(
#  			sub { rec('001') },
#  			'nsk','bbaza',
#  			sub { rec('011','a') },
#  			sub { rec('11') },
#  		)
#  	)
# );
# 
# marc('998','a',
# 	sort(
#  		lookup(
#  			sub { rec('001') },
#  			'nsk','bbaza',
#  			sub { first(rec('200','a')) . first(rec('200','e')) },
#  			sub { rec('200','a') . rec('200','e') },
#  		)
#  	)
# );
# 
# marc('998','a',
# 	sort(
#  		lookup(
#  			sub { rec('001') },
#  			'nsk','bbaza',
#  			sub { first(rec('200','a')) },
#  			sub { rec('200','a') },
#  		)
#  	)
# );

} # signatura
