#!/usr/bin/perl

# GMR Equipment management sub routine
#   trigger.pl
#
#   ver 0.705 (09/10/12)




sub def_trigger_list {
	if($language ne "English"){	
		@tuliatrigger = (
			'Seiryu,Suzaku,Byakko,Genbu,Kirin',
			'東玉,春石',
			'南玉,夏石',
			'西玉,秋石',
			'北玉,冬石',
			'青龍印章,朱雀印章,白虎印章,玄武印章'
		);
		@limbustrigger = (
			'テメナス中央塔1階,テメナス中央塔2階,テメナス中央塔3階,テメナス中央塔4階,アポリオンCN',
			'エメラルドチップ',
			'スカーレットチップ',
			'アイボリーチップ',
			'シルバーチップ,セルリアンチップ,オーキッドチップ',
			'チャコールチップ,マゼンタチップ,スマルトチップ,スモーキーチップ'
		);
		@cosmictrigger = (
			'マムージャ蕃国系Step2,マムージャ蕃国系Step3,マムージャ蕃国系Step4,ハルブーン傭兵団領系Step2,ハルブーン傭兵団領系Step3,ハルブーン傭兵団領系Step4,アラパゴ諸島系Step2,アラパゴ諸島系Step3,アラパゴ諸島系Step4,パンデモニウムウォーデン',
			'蒸栗の割符',
			'萌葱の割符',
			'薄墨の割符,烏羽の割符,小豆の割符',
			'臙脂の割符',
			'東雲の割符',
			'滅紫の割符,黄金の割符,柿渋の割符',
			'木賊の割符',
			'山吹の割符',
			'朽葉の割符,黒橡の割符,胡桃の割符',
			'竜胆の割符,瑠璃の割符,紫苑の割符'
		);
		@generaltrigger = (
			'不敗の大帝,不撓の酋帥,不滅の邪神',
			'皇帝の系譜・第1章,皇帝の系譜・第2章,皇帝の系譜・第3章,皇帝の系譜・第4章,皇帝の系譜・第5章,皇帝の系譜・第6章,皇帝の系譜・第7章,皇帝の系譜・第8章',
			'ド・ヌの御言・第1節,ド・ヌの御言・第2節,ド・ヌの御言・第3節,ド・ヌの御言・第4節,ド・ヌの御言・第5節,ド・ヌの御言・第6節,ド・ヌの御言・第7節,ド・ヌの御言・第8節',
			'ハバロの詩・第1の徳,ハバロの詩・第2の徳,ハバロの詩・第3の徳,ハバロの詩・第4の徳,ハバロの詩・第5の徳,ハバロの詩・第6の徳,ハバロの詩・第7の徳,ハバロの詩・第8の徳'
		);
		@lumoriatrigger = (
			'Jailer of Hope,Jailer of Justice,Jailer of Prudence,Jailer of Love',
			'第一の聖徳,無瞋の善根',
			'第二の聖徳,無貪の善根',
			'第三の聖徳,無痴の善根',
			'第四の聖徳,第五の聖徳,第六の聖徳'
		);
		@einherjartorigger = (
			'第Iウイング,第IIウイング,第IIIウイング',
			'ロスヴァイセの羽,グリムゲルデの羽,ジークルーネの羽',
			'ヘルムヴィーゲの羽,シュヴェルトライテの羽,ヴァルトラウテの羽',
			'オルトリンデの羽,ゲルヒルデの羽,ブリュンヒルデの羽'
		);
	}
	else{
		@tuliatrigger = (
			'Seiryu,Suzaku,Byakko,Genbu,Kirin',
			'Gem of the East,Springstone',
			'Gem of the South,Summerstone',
			'Gem of the West,Autumnstone',
			'Gem of the North,Winterstone',
			'Seal of Seiryu,Seal of Suzaku,Seal of Byakko,Seal of Genbu'
		);
		@limbustrigger = (
			'Central Temenos - 1st Floor,Central Temenos - 2nd Floor,Central Temenos - 3rd Floor,Central Temenos - 4th Floor,Central Apollyon',
			'Emerald Chip',
			'Scarlet Chip',
			'Ivory Chip',
			'Silver Chip,Cerulean Chip,Orchid Chip',
			'Charcoal Chip,Magenta Chip,Smalt Chip,Smoky Chip'
		);
		@cosmictrigger = (
			'Mamool Ja Step2,Mamool Ja Step3,Mamool Ja Step4,Halvung Step2,Halvung Step3,Halvung Step4,Arrapago Step2,Arrapago Step3,Arrapago Step4,Pandemonium Warden',
			'Maroon Seal',
			'Apple Green Seal',
			'Charcoal Grey Seal,Deep Purple Seal,Chestnut-colored Seal',
			'Cerise Seal',
			'Salmon Seal',
			'Purplish Grey Seal,Gold-colored Seal,Copper-colored Seal',
			'Pine Green Seal',
			'Amber Seal',
			'Fallow-colored Seal,Taupe-colored Seal,Sienna-colored Seal',
			'Lilac-colored Seal,Bright Blue Seal,Lavender-colored Seal'
		);
		@generaltrigger = (
			'The Blood-Bathed Crown,The Buried God,A Malicious Manifest',
			'Imperial Lineage Chapter I,Imperial Lineage Chapter II,Imperial Lineage Chapter III,Imperial Lineage Chapter IV,Imperial Lineage Chapter V,Imperial Lineage Chapter VI,Imperial Lineage Chapter VII,Imperial Lineage Chapter VIII',
			"The Words of Do'Nhu I,The Words of Do'Nhu II,The Words of Do'Nhu III,The Words of Do'Nhu IV,The Words of Do'Nhu V,The Words of Do'Nhu VI,The Words of Do'Nhu VII,The Words of Do'Nhu VIII",
			"Habalo's Eclogue - Verse I,Habalo's Eclogue - Verse II,Habalo's Eclogue - Verse III,Habalo's Eclogue - Verse IV,Habalo's Eclogue - Verse V,Habalo's Eclogue - Verse VI,Habalo's Eclogue - Verse VII,Habalo's Eclogue - Verse VIII"
		);
		@lumoriatrigger = (
			'Jailer of Hope,Jailer of Justice,Jailer of Prudence,Jailer of Love',
			'First Virtue,Deed of Placidity',
			'Second Virtue,Deed of Moder',
			'Third Virtue,Deed of Sensib',
			'Fourth Virtue,Fifth Virtue,Sixth Virtue'
		);
		@einherjartorigger = (
			'Wing I,Wing II,Wing III',
			"Rossweisse's feather,Grimgerde's feather,Siegrune's feather",
			"Helmwige's feather,Schwertleite's feather,Waltraute's feather",
			"Ortlinde's feather,Gerhilde's feather,Brunhilde's feather"
		);
	}
	
	@triggerdeflist = (
		[ @tuliatrigger ],		# 0
		[ @limbustrigger ],		# 1
		[ @lumoriatrigger ],	# 2
		[ @cosmictrigger ],		# 3
		[ @einherjartorigger ],	# 4
		[ @generaltrigger ]		# 5
	);
	
	@triggerdefname = (
		'四神',				# 0
		'リンバス',			# 1
		'七元徳',			# 2
		'花鳥風月',			# 3
		'エインヘリヤル',	# 4
		'獣人軍将領'		# 5
	);
	
	@tirggerlistname = (
		'tulia',		# 0
		'limbus',		# 1
		'lumoria',		# 2
		'cosmic',		# 3
		'einherjar',	# 4
		'general'		# 5
	);
}

sub login_modify_trigger {
	&def_trigger_list;
	if( !open( IN, "$triggerfile" ) ){
		&header;
		&error_message( 'datafie error/system', 'trigger file access failed');
	}
	my ( $tt_no, $tt_type, $tt_adm, $tt_ent, @tt_tris );
	@alltri = <IN>;
	close IN;
	
	&dirlock;
	
	$max_no = 0;
	foreach( @alltri ){
		next if /^#/;
		($tt_no, $tt_type, $tt_adm, $tt_ent, @tt_tris) = split( /\t/ );
		if( $tt_no > $max_no ){
			$max_no = $tt_no;
		}
		if( $trino == $tt_no ){
			$tt_tri = $_;
		}
	}
	if( $trino == -1 ){
		$tt_no = $max_no+1;
		$trino = $tt_no;
		$o_no = $tt_no;
		$o_type = $type;
		$newr = "$tt_no\t$type\t";
		if( $#admlist < 0 ){
			rmdir($lockdir);
			&header;
			&error_message( 'input error/data', 'need at least one admin');
		}
		foreach my $i ( @admlist ){
			$newr .= "$i,";
		}
		chop( $newr );
		$newr .= "\t";
		foreach my $i ( @entlist ){
			$newr .= "$i,";
		}
		chop( $newr );
		$newr .= "\t";
		foreach my $i ( @tt_tris ){
			$newr .= "$i\t";
		}
		$newr .= "\n";
		@newtri = @alltri;
		push( @newtri, $newr );
	}
	elsif( $all eq 'triggeredit' ){
		($tt_no, $tt_type, $tt_adm, $tt_ent, @tt_tris) = split( /\t/, $tt_tri );
		$newr = "$tt_no\t$tt_type\t$tt_adm\t$tt_ent\t$t_tri\n";
		$type = $tt_type;
		$o_no = $tt_no;
		foreach my $r ( @alltri ){
			my $tmpr = $r;
			$tmpr =~ s/\s//g;
			if( $tmpr eq "" ){
				next;
			}
			($tt_no, $tt_type, @others ) = split( /\t/, $r );
			if( $tt_no == $trino ){
				push( @newtri, $newr );
			}
			else{
				push( @newtri, $r );
			}
		}
	}
	elsif( $all eq 'triggernew' ){
		($tt_no, $tt_type, $tt_adm, $tt_ent, @tt_tris) = split( /\t/, $tt_tri );
		$newr = "$tt_no\t$tt_type\t";
		$o_type = $tt_type;
		$o_no = $tt_no;
		if( $#admlist < 0 ){
			rmdir($lockdir);
			&header;
			&error_message( 'input error/data', 'need at least one admin');
		}
		foreach my $i ( @admlist ){
			$newr .= "$i,";
		}
		chop( $newr );
		$newr .= "\t";
		foreach my $i ( @entlist ){
			$newr .= "$i,";
		}
		chop( $newr );
		$newr .= "\t";
		foreach my $i ( @tt_tris ){
			$newr .= "$i\t";
		}
		$newr .= "\n";
		foreach my $r ( @alltri ){
			my $tmpr = $r;
			$tmpr =~ s/\s//g;
			if( $tmpr eq "" ){
				next;
			}
			($tt_no, $tt_type, @others ) = split( /\t/, $r );
			if( $tt_no == $trino ){
				push( @newtri, $newr );
			}
			else{
				push( @newtri, $r );
			}
		}
	}
	if(!open(OUT,">$triggerfile")){
		&header; &error_message('datafile error1560','trigger file access failed.');
	}
#	print OUT "#No\t#Type\t#Admin menber list\t#Member list\t#Triggers info\n";
	print OUT @newtri;
	close OUT;
	rmdir($lockdir);
	
	$rsstime="20$year-$mon-$mday"."T$hour:$min:00+09:00";
	if( $language ne "English" ){
		$message = "トリガー情報No. $o_no : $triggerdefname[$o_type] が $name さんに編修されました。";
	}
	else{
		$message = "Trigger info, No. $o_no : $triggerdefname[$o_type] is modified by $name ";
	}
	$historybase="EDTRIG\t$name\t$o_no: $triggerdefname[$o_type]\t$rsstime\t$no\t$message\t$rh\t$addr\t\n";
	
	&add_history;
	
	&set_cookie;
#	&reload_header;
	&reload_header;
	
	if( $language ne "English" ){
		print "<table width=100%><TR bgcolor=$indexcolor><TD>$t_no: $triggerdefname[$t_type]を修正しました。</TD></TR></table>";
		print "<a href=$script>MemberList Top</a><BR>自動的に戻ります。";
	}
	else{
		print "<table width=100%><TR bgcolor=$indexcolor><TD>Information modified</TD></TR></table>";
		print "Automatically move top page.<BR>";
	}
}

sub login_modify_trigger_del {
	&def_trigger_list;
	if( !open( IN, "$triggerfile" ) ){
		&header;
		&error_message( 'datafie error/system', 'trigger file access failed');
	}
	my ( $tt_no, $tt_type, $tt_adm, $tt_ent, @tt_tris );
	@alltri = <IN>;
	close IN;
	
	&dirlock;
	
	foreach my $r ( @alltri ){
		my $tmpr = $r;
		$tmpr =~ s/\s//g;
		if( $tmpr eq "" ){
			next;
		}
		($tt_no, $tt_type, @others ) = split( /\t/, $r );
		if( $tt_no == $trino ){
			$o_type = $tt_type;
			next;
		}
		else{
			push( @newtri, $r );
		}
	}
	if(!open(OUT,">$triggerfile")){
		&header; &error_message('datafile error1560','trigger file access failed.');
	}
	print OUT @newtri;
	close OUT;
	rmdir($lockdir);
	
	
#	$rsstime="20$year-$mon-$mday"."T$hour:$min:00+09:00";
#	if( $language ne "English" ){
#		$message = "トリガー情報No. $trino : $triggerdefname[$o_type] が $name さんに削除されました。";
#	}
#	else{
#		$message = "Trigger info, No. $trino : $triggerdefname[$o_type] is deleted by $name ";
#	}
#	$historybase="DELTRIG\t$name\t$trino : $triggerdefname[$o_type]\t$rsstime\t$no\t$message\t$rh\t$addr\t\n";
#	
#	&add_history;
	
	&set_cookie;
#	&reload_header;
	&reload_header;
	
	if( $language ne "English" ){
		print "<table width=100%><TR bgcolor=$indexcolor><TD>$t_no: $triggerdefname[$t_type]を削除しました。</TD></TR></table>";
		print "<a href=$script>MemberList Top</a><BR>自動的に戻ります。";
	}
	else{
		print "<table width=100%><TR bgcolor=$indexcolor><TD>Information modified</TD></TR></table>";
		print "Automatically move top page.<BR>";
	}
}

return 1;