#!/usr/bin/perl
#
# Transform a MAME DAT file to SQL
#
my $tosec_type;
my $media_type;

$numArgs = $#ARGV + 1;

die "Input DAT file required" unless $numArgs > 0;

foreach $num (0 .. $#ARGV ) {
	process_dat_file( $ARGV[$num] );
}

sub csv_title
{
    my ($title) = @_;
    $title =~ s/"/""/g;
    return "\"$title\"";
}

sub toml_string
{
    my ($string) = @_;
    $string =~ s/\\/\\\\/g;
    $string =~ s/"/\\"/g;
    return $string;
}

sub clean_title
{
    my ($title) = @_;
    $title =~ s/&amp;/&/g;
    return $title;
}

sub process_dat_file
{

	open ( INFILE, "$ARGV[$num]" ) or die "Cound not open file: $ARGV[$num]: $!\n";

    mkdir ("profiles.5200");
    mkdir ("profiles.8bit");

	open ( LIST5200, '>>', "profiles.5200/list.csv" ) or die "Cound not open file:list5200\n";
	open ( LIST800, '>>', "profiles.8bit/list.csv" ) or die "Cound not open file:list800\n";

	$string1 = '';
	while ( <INFILE> ) {

		$string1 .= $_;

       #if ( ($string1 =~ /clrmamepro.*\(.*name\W+"?([^"\n]*)"?\W+description.*version\W+([\w]*)\W+author.*\)/si) ||
		if ( ($string1 =~ /clrmamepro.*\(.*name\W+"?([^"\n]*)"?\W+description.*version\W+([^\n]*)\W+author.*\)/si ) ||
             ($string1 =~ /<header>.*<name>(.+?)<\/name>.*<version>(.+?)<\/version>.*<\/header>/si) ) {

			# So after matching we get name and version
			$tosec_name = $1;
			$tosec_version = $2;

            # Determine collection type
			if ( $string1 =~ /Atari 8bit - Applications/ ) {
				$tosec_type = 1;
                $tosec_string = "TOSEC-Applications"
			} 
            elsif ( $string1 =~ /Atari 8bit - Compilations/ ) {
				$tosec_type = 1;
                $tosec_string = "TOSEC-Compilations"
			} 
            elsif ( $string1 =~ /Atari 8bit - Demos/ ) {
				$tosec_type = 1;
                $tosec_string = "TOSEC-Demos"
			} 
            elsif ( $string1 =~ /Atari 8bit - Docs/ ) {
				$tosec_type = 1;
                $tosec_string = "TOSEC-Docs"
			} 
            elsif ( $string1 =~ /Atari 8bit - Educational/ ) {
				$tosec_type = 1;
                $tosec_string = "TOSEC-Educational"
			} 
            elsif ( $string1 =~ /Atari 8bit - Games/ ) {
				$tosec_type = 1;
                $tosec_string = "TOSEC-Games"
			} 
            elsif ( $string1 =~ /Atari 8bit - Magazines/ ) {
				$tosec_type = 1;
                $tosec_string = "TOSEC-Magazines"
			} 
            elsif ( $string1 =~ /Atari 8bit - Operating Systems/ ) {
				$tosec_type = 1;
                $tosec_string = "TOSEC-Operating-Systems"
			} 
            elsif ( $string1 =~ /Atari 8bit/ ) {
				$tosec_type = 1;
            }
            elsif ( $string1 =~ /No-Intro/ ) {
				$tosec_type = 0;
                $tosec_string = "No-Intro-5200"
            }
            elsif ( $string1 =~ /Hacks/ ) {
				$tosec_type = 0;
                $tosec_string = "Hacks-5200"
            }
            elsif ( $string1 =~ /Homebrews/ ) {
				$tosec_type = 0;
                $tosec_string = "Homebrews-5200"
            }
            elsif ( $string1 =~ /Conversions/ ) {
				$tosec_type = 0;
                $tosec_string = "Conversions-5200"
            }
            else {
				$tosec_type = 0;
                $tosec_string = "Other"
            }

            # Detemine media type
			if ( $string1 =~ /\[ATR\]/ ) {
				$media_type = "floppy";
			} 
			elsif ( $string1 =~ /\[ATX\]/ ) {
				$media_type = "floppy";
			} 
			elsif ( $string1 =~ /\[BAS\]/ ) {
				$media_type = "basic";
			} 
			elsif ( $string1 =~ /\[BIN\]/ ) {
				$media_type = "cartridge";
			} 
			elsif ( $string1 =~ /\[CAS\]/ ) {
				$media_type = "cassette";
			} 
			elsif ( $string1 =~ /\[COM\]/ ) {
				$media_type = "exe";
			} 
			elsif ( $string1 =~ /\[XEX\]/ ) {
				$media_type = "exe";
			} 
			elsif ( $string1 =~ /\[XFD\]/ ) {
				$media_type = "floppy";
			} 
            else {
				$media_type = "cartridge";
            }

            if ( $tosec_type > 0 ) {
                $dir = "profiles.8bit";
            }
            else {
                $dir = "profiles.5200";
            }

	        open ( TYPEFILE, ">>", "$dir/$tosec_string.csv" )  or die "Cound not create file: $dir/$tosec_string.conf: $!\n";
			$string1 = '';
		}

		if ( $string1 =~ /game\s*\(.*?name\s+"([^"\n]*)"\s+description.*?rom.*?\(.*?name.*?size\W+([\w]*)\W+crc\W+([\w]*)\W+.*\)\W+\)/si ||
             $string1 =~ /<game name="(.+?)".*<description>.*<rom name.*size="(.+?)".*crc="(.+?)".*<\/game>/si ) {
            $specific_machine = 0;
            $rev_b = 0;
			$crc = hex ( $3 );
			$name = clean_title($1);
			$size = $2;
            $mapping = "";
            $md5 = "";
            $sha1 = "";
            if ( $string1 =~ /md5\W+([\w]*)\W+/ ) {
			    $md5 = $1;
            }
            if ( $string1 =~ /sha1\W+([\w]*)\W+/ ) {
			    $sha1 = $1;
            }
			$ram = "0";
			if ( ($tosec_type > 0 && $tosec_type < 24) || $tosec_name =~ 800  ) {
				$machine_type = "800";
			}
			else {
				$machine_type = "5200";
			}
			if ( $name =~ /\[BASIC\]/i ) {
				$basic = 1;
			}
			else {
				$basic = 0;
			}
			if ( $name =~ /\[Req OSb\]/i ) {
				$machine_type = "800";
                $specific_machine = 1;
                $rev_b = 1;
			}
			if ( $name =~ /\[Req 128k\]/i ) {
				$machine_type = "xl";
				$ram = "128";
                $specific_machine = 1;
			}
			if ( $name =~ /\[Req 64k\]/i ) {
				$machine_type = "xl";
				$ram = "64";
                $specific_machine = 1;
			}
			if ( $name =~ /\[Req 16k\]/i ) {
				$ram = "16";
			}
            if ( $tosec_type > 0 && $media_type eq "cartridge" && $name =~ /\[XEGS cart\]/i ) {
                $mapping = "8bit.xegs";
            }

            $file = sprintf("%08x", $crc);
            if ( $machine_type =~ "5200" ) {
                $dir = "profiles.5200";
                print LIST5200 csv_title($name) . ", 0x$file\n";
            }
            else {
                $dir = "profiles.8bit";
                print LIST800 csv_title($name) . ", 0x$file\n";
            }
            print TYPEFILE csv_title($name) . ", 0x$file\n";
	        open ( OUTFILE, ">", "$dir/$file.conf" )  or die "Cound not create file: $dir/$file.conf: $!\n";
            print OUTFILE "\n";
            print OUTFILE "title = \"" . toml_string($name) . "\"\n";
            print OUTFILE "file = \"\"\n";
            print OUTFILE "crc = \"0x$file\"\n";
            print OUTFILE "md5 = \"$md5\"\n";
            print OUTFILE "sha1 = \"$sha1\"\n";
            print OUTFILE "machine = \"$machine_type\"\n";
            print OUTFILE "mapping = \"$mapping\"\n";
            print OUTFILE "media_type = \"$media_type\"\n";
            print OUTFILE "image_size = $size\n";
            print OUTFILE "write_protect = true\n";
            print OUTFILE "collections = [ $tosec_string\n";
            print OUTFILE "               ]\n";
            print OUTFILE "\n";
            print OUTFILE "sound_tags = [ ]\n";
            print OUTFILE "video_tags = [ ]\n";
            print OUTFILE "input_tags = [ ]\n";
            print OUTFILE "\n";
            print OUTFILE "[general]\n";
            if ( $specific_machine == 1 ) {
                print OUTFILE "machine = \"$machine_type\"\n";
            }
            if ( $basic == 1 ) {
                print OUTFILE "basic_enable = true\n";
            }
            print OUTFILE "\n";
            print OUTFILE "[machine.5200]\n\n";
            print OUTFILE "[machine.800]\n";
            if ( $ram != 0 && $ram <= 16 ) {
                print OUTFILE "ram = $ram\n";
            }
            if ( $rev_b == 1 ) {
                print OUTFILE "bios = \"rev_b\"\n";
            }
            print OUTFILE "\n";
            print OUTFILE "[machine.xl]\n";
            if ( $ram != 0 ) {
                print OUTFILE "ram = $ram\n";
            }
            print OUTFILE "\n";
            print OUTFILE "[video]\n";
            print OUTFILE "tag = \"this\"\n";
            print OUTFILE "\n";
            print OUTFILE "[sound]\n";
            print OUTFILE "tag = \"this\"\n";
            print OUTFILE "\n";
            print OUTFILE "[input.misc]\n";
            print OUTFILE "\n";
            print OUTFILE "[input]\n";
            print OUTFILE "tag = \"this\"\n";
            print OUTFILE "\n";
	        close OUTFILE;

			$string1 = '';
		}

	} # end while INFILE */

	close INFILE;
	close TYPEFILE;
	close LIST800;
	close LIST5200;

} # end process_dat_file
