summarylogtreecommitdiffstats
path: root/umodunpack.pl
blob: 648752afd155221199f29baadba182a87a430ecd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
#!/usr/bin/perl

use Getopt::Long;

# ----------------------------------------------------------------
# $Header: /home/misaka/code/umodunpack/RCS/umodunpack.pl,v 1.11 2000/01/02 22:01:24 misaka Exp $
# ----------------------------------------------------------------

=pod

=head1 NAME

umodunpack.pl - Unpack an Unreal [Tournament] Umod file.

=head1 SYNOPSIS

  umodunpack.pl -h|--help|-v|--version
  umodunpack.pl -l|--list|-i|--info <umod_file>
  umodunpack.pl -u|--unpack <umod_file>
  umodunpack.pl -e|--extract <filename> <umod_file>
  umodunpack.pl -r|--readme <umod_file>

=head1 DESCRIPTION

Extract the packed Unreal [Tournament] mod files from a given umod.
Please read the BUGS section.

This script actually contains a 'umod' Perl package in it.  Currently,
it can only read and extract files from a umod, but concievably I
could add code to pack a umod file.

=head1 BUGS

The code to modify the Manifest.ini is currently disabled.  It appears
that there is no Manifest.ini file under Linux, and not having the
actual specs, I'm not certain if there are any other files that need
to be updated.  As it stands, mods seem to work fine without any
further changes.

The umod's requirements are not checked.  Maybe sometime soon.

Some of the code that parses the umod file could be more intelligent,
and may react in annoying ways if run on a file that isn't a umod, or
is a corrupt umod, or is just a umod that doesn't follow the rules I
made up from the umods I looked at.

=head1 AUTHOR

Initial code written by Mishka Gorodnitzky <misaka@pobox.com>.

umodINIGroup parseing patch added by Avatar <avatar@deva.net>.


=cut

# ----------------------------------------------------------------

my( $umodFileName, $UTBase, $umodFile );
my( $defaultUTBase ) = '.';
my( $rcfile ) = '.umodunpackrc';
my( $versionText ) = <<EOT;
umodunpack.pl v0.4 beta-quality by Mishka <misaka\@pobox.com>
  with enhancement by Avatar <avatar\@deva.net>.
  No warranty, use at your own risk.
EOT

my( $helpText ) = <<EOT;
Usage: $0 [--base <base_dir>] --list|--info|--unpack <umod_file>

  Unpack a Umod file for Unreal our Unreal Tournament.

    -h|--help             : this help
    -v|--version          : display version info
    -i|--info             : info on umod file
    -l|--list             : list files in Umod
    -b|--base <base_dir>  : base directory for Unreal install, default
                       	    is $defaultUTBase
    -u|--unpack           : extract the files to the UT basedir (default)
    -e|--extract <file>   : extract specific file
    -r|--readme           : view ReadMe file, if any, in umod

  Use 'perldoc $0' for more info.

EOT

# --------------------------------
# Process cmdline args and options.

my( $flagInfo, $flagList, $flagHelp, $flagVersion, $flagUnpack, $argExtract, 
    $flagReadme );

GetOptions( 'info'      => \$flagInfo,
	    'list!'     => \$flagList,
	    'help!'     => \$flagHelp,
	    'base=s'    => \$UTBase,
	    'unpack!'   => \$flagUnpack,
	    'extract=s' => \$argExtract,
	    'version!'  => \$flagVersion,
	    'readme!'   => \$flagReadme );

# Check if help was requested.
if( $flagHelp ) {

    print( $helpText );
    exit( 0 );
}

# Check if version info was requested.
if( $flagVersion ) {
    print( $versionText );
    exit( 0 );
}

# See if the UT basedir was given on the cmdline.
if( defined( $UTBase ) ) {

    # UT basedir given.
    #
    # Assuming the given dir exists, we'll be saveing it into
    # ~/.umodunpackrc
    if( !-d $UTBase ) {
	die( "Unreal base directory '$UTBase' not found\n" );
    }

    if( open( RCFILE, ">$ENV{ HOME }/$rcfile" ) ) {
	print( RCFILE "$UTBase\n" );
	close( RCFILE );
    }

} else {

    # UT basedir not given.
    #
    # Read it from our rcfile, if it exists.
    if( -e "$ENV{ HOME }/$rcfile" ) {
	if( open( RCFILE, "<$ENV{ HOME }/$rcfile" ) ) {
	    $UTBase = <RCFILE>;
	    chomp( $UTBase );
	    close( RCFILE );
	}
    } else {

	# rcfile doesn't exist, set basedir to the default dir.
	$UTBase = $defaultUTBase;
    }
}


# Grab the cmdline arg.
$umodFileName = shift( @ARGV );
if( !defined( $umodFileName ) ) {
    warn( "no umod file specified\n" );
    print( $helpText );
    exit( 1 );
}


# And make sure at least we're flagged for extraction, if nothing else.
$flagUnpack = 1
  if( !$flagInfo 
      and !$flagList 
      and !$flagHelp 
      and !$flagUnpack 
      and !$argExtract );

# --------------------------------
# Rev 'em up and get started.

# Initialize the umod file object.
#
# Most of the behind-the-scenes work is done here.
$umodFile = new umodFile ( -file => "$umodFileName" );

die( "$0: could not open or parse '$umodFileName': $!\n" )
  if( !defined( $umodFile ) );

# Set the base directory for extracted files.
$umodFile->utbase( $UTBase );

# Main branches.  Check what operation we are doing.

if( $flagInfo ) {

    # Print out umod info.
    #
    # You know, the one really annoying thing about object refs is that
    # they can't be used inside of a quoted region of text.  Makes for
    # ugly code, IMO.
    #
    # What really makes this code ugly is the need for optional data on
    # each line ... in each case the URL is optional.  Icki.

    print( "\n",
	   ' Mod Name: ', $umodFile->productname );
    print( ' (', $umodFile->producturl, ')' )
      if( defined( $umodFile->producturl ) );
    print( "\n",
	   '  Version: ', $umodFile->version );
    print( ' (', $umodFile->versionurl, ')' )
      if( defined( $umodFile->versionurl ) );
    print( "\n",
	   'Developer: ', $umodFile->developer );
    print( ' (', $umodFile->developerurl, ')' )
      if( defined( $umodFile->developerurl ) );
    print( "\n",
	   "\n" );

} elsif( $flagReadme ) {

    # Display the umod's readme file.
    #
    # The umod's readme file is listed in Setup group.  The umod object
    # grabs that and return that, if it's found.
    my( $readme ) = $umodFile->readme;

    if( defined( $readme ) ) {
	print( $umodFile->readme, "\n\n" );
    } else {
	print( "No umod readme found.\n" );
    }

} elsif( $flagList ) {

    # List out the packing list.

    my( @packingList ) = $umodFile->packingList;
    my( @iniChanges ) = $umodFile->iniChanges;

    print( "Requires:\n",
	   map( "  $_\n", keys( %{ $umodFile->requires } ) ), "\n" )
      if( $umodFile->requires );
    print( "Groups:\n",
	   map( "  $_\n", keys( %{ $umodFile->groups } ) ), "\n" )
      if( $umodFile->groups );
    print( "Packing List:\n",
	   map( "  $_->{ 'src' } ($_->{ 'size' }b)\n",
		@packingList ), "\n" );
    print( "INI File Changes:\n",
	   map( "  $_->{ 'file' }\n    [$_->{ 'section' }]\n    $_->{ 'key' }=$_->{ 'value' }\n",
		@iniChanges ), "\n" );


} elsif( $argExtract ) {

    # Extract a single file from the umod package.

    my( @packingList ) = $umodFile->packingList;
    my( $packedFile ) = grep( $_->{ 'src' } =~ $argExtract, @packingList );

    die( "$0: file '$argExtract' not found for extraction: $!\n" )
      if( !defined( $packedFile ) );

    print( "extracting file: $packedFile->{ src }\n" );
    $umodFile->extract( $packedFile )
      or die( "$0: error extracting file '$argExtract': $!\n" );

} elsif( $flagUnpack ) {

    # Simple extraction of packed files.
    # Added updates ini file functionality by Avatar.

    my( @packingList ) = $umodFile->packingList;
    my( @iniChanges ) = $umodFile->iniChanges;
    my( $packedFile, $file );

    foreach $packedFile ( @packingList ) {

	print( "extracting file: $packedFile->{ src }\n" );
	$umodFile->extract( $packedFile )
	  or die( "$0: error extracting files: $!\n" );

    }

    foreach my $change ( @iniChanges ) {
	my ( $iniName ) = "${UTBase}/$change->{ 'file' }";
	# Silly DOS backslashes ... 
	$iniName =~s-\\-/-g;

	print( "modifying ini file: $change->{ 'file' }\n" );

	open( INI, "<$iniName" )
	  or die( "$0: could not open $iniName for reading: $!" );

	my( $iniContents ) = <INI>;

	close( INI );

	# make a back up just in case
	open( INI, ">$iniName.bak" )
	  or die( "$0: could not open $iniName.bak for writing: $!" );

	print( INI $iniContents );

	close( INI );

	if( grep( /\[$change->{ 'section' }\]/, $iniContents ) ) {
	    $iniContents =~ s#(\[$change->{ 'section' }\])#$1\r\n$change->{ 'key' }=$change->{ 'value' }#;
        } else {
	    $iniContents .= "\r\n[$change->{ 'section' }]\r\n$change->{ 'key' }=$change->{ 'value' }\r\n\r\n";
	}

	open( INI, ">$iniName" )
	  or die( "$0: could not open $iniName for writing: $!" );

	print( INI $iniContents );

	close( INI );
    }

}

##################################################################
##################################################################

# This is the umodFile object package.
#
# This code is an implementation of a umod object in Perl.  It's not
# complete, it can't piece together a umod yet, etc, but it works fine
# for the extraction/unpacking.  Have any suggestions, or do you think
# you would use this more if it could pack together a umod file?  Let
# me know, I'll consider completeing it.

package umodFile;

# ----------------------------------------------------------------

# If you don't know what this sub is for you're going to have a rough
# time following. :)

sub new {

    my( $type ) = shift;
    my( %params ) = @_;
    my( $file ) = shift;
    my( $self ) = {};

    bless( $self );

    if( exists( $params{ -file } ) ) {
	$self->{ 'file' } = $params{ -file };
    } elsif( $file !~ m/^-/ ) {
	$self->{ 'file' } = $file;
    }

    if( exists( $self->{ 'file' } ) ) {
	$self->open or return( undef );
    }

    return( $self );

}

# ----------------------------------------------------------------

# Set/return file variable.

sub file {

    my( $self ) = shift;
    my( $file ) = shift;

    $self->{ 'file' } = $file
      if( $file );

    return( $self->{ 'file' } );

}

# ----------------------------------------------------------------

# Set/return UT base dir variable.

sub utbase {

    my( $self ) = shift;
    my( $utbase ) = shift;

    $self->{ 'utbase' } = $utbase
      if( $utbase );

    return( $self->{ 'utbase' } );

}

# ----------------------------------------------------------------

# Set/return product variable.

sub product {

    my( $self ) = shift;
    my( $product ) = shift;

    $self->{ 'product' } = $product
      if( $product );

    return( $self->{ 'product' } );

}

# ----------------------------------------------------------------

# Set/return version variable.

sub version {

    my( $self ) = shift;
    my( $version ) = shift;

    $self->{ 'version' } = $version
      if( $version );

    return( $self->{ 'version' } );

}

# ----------------------------------------------------------------

# Set/return archive name variable.

sub archive {

    my( $self ) = shift;
    my( $archive ) = shift;

    $self->{ 'archive' } = $archive
      if( $archive );

    return( $self->{ 'archive' } );

}

# ----------------------------------------------------------------

# Set/return requires data.

sub requires {

    my( $self ) = shift;
    my( %requires ) = @_;

    $self->{ 'requires' } = { %requires }
      if( %requires );

    return( $self->{ 'requires' } );

}

# ----------------------------------------------------------------

# Set/return groups data.

sub groups {

    my( $self ) = shift;
    my( %groups ) = @_;

    $self->{ 'groups' } = { %groups }
      if( %groups );

    return( $self->{ 'groups' } );

}

# ----------------------------------------------------------------

# Set/return product name variable.

sub productname {

    my( $self ) = shift;
    my( $productname ) = shift;

    $self->{ 'productname' } = $productname
      if( defined( $productname ) );

    return( $self->{ 'productname' } );

}

# ----------------------------------------------------------------

# Set/return product url variable.

sub producturl {

    my( $self ) = shift;
    my( $producturl ) = shift;

    $self->{ 'producturl' } = $producturl
      if( defined( $producturl ) );

    return( $self->{ 'producturl' } );

}

# ----------------------------------------------------------------

# Set/return version url variable.

sub versionurl {

    my( $self ) = shift;
    my( $versionurl ) = shift;

    $self->{ 'versionurl' } = $versionurl
      if( defined( $versionurl ) );

    return( $self->{ 'versionurl' } );

}

# ----------------------------------------------------------------

# Set/return developer variable.

sub developer {

    my( $self ) = shift;
    my( $developer ) = shift;

    $self->{ 'developer' } = $developer
      if( defined( $developer ) );

    return( $self->{ 'developer' } );

}

# ----------------------------------------------------------------

# Set/return developer url variable.

sub developerurl {

    my( $self ) = shift;
    my( $developerurl ) = shift;

    $self->{ 'developerurl' } = $developerurl
      if( defined( $developerurl ) );

    return( $self->{ 'developerurl' } );

}

# ----------------------------------------------------------------

# Set/return the filename of the ReadMe file.

sub readmefile {

    my( $self ) = shift;
    my( $readme ) = shift;

    $self->{ 'readmefile' } = $readme
      if( defined( $readme ) );

    return( $self->{ 'readmefile' } );

}

# ----------------------------------------------------------------

# Return the packing list.  
#
# At this point, this read-only.  This could change if I add the
# ability to pack a umod file.
#
# Each element in the returned list is a ref to a hash derived from
# the Src lines in the packed ini files.

sub packingList {

    my( $self ) = shift;

    return( @{ $self->{ 'packingList' } } );

}
# ----------------------------------------------------------------

# Return the ini change list.  
#
# Each element in the returned list is a ref to a hash derived from
# the Ini lines in the packed ini files.

sub iniChanges {

    my( $self ) = shift;

    return( @{ $self->{ 'iniChanges' } } );

}

# ----------------------------------------------------------------

# Open the umod file and parse it.
#
# Yup, here's the big one, forx.

sub open {

    my( $self ) = shift;
    my( $file ) = shift;
    my( $fileContents, $line, %sections, $section );
    my( @requires, @groups, @packingList, @iniChanges, $setupLen );
    my( $offset ) = 0;

    # Juggle $file passed to us, if any, with what we've already got.
    if( $file ) {
	$self->file( $file );
    } else {
	$file = $self->file;
    }

    # Open the file or be unhappy.
    if( !open( UMOD, "<$file" ) ) {
	warn( "$0: could not open file '$file' for reading: $!\n" );
	return( 0 );
    }

    undef( $/ );

    # Read in the whole file.
    $fileContents = <UMOD>;

    # Skip any non-section header lines at the beginning of the file.
    #
    # This is a paranoia check, it should, in theory, never be an
    # issue.
    while ( $fileContents !~ m/^\[.+\]\r\n/ ) {
	$line = nextLine( \$fileContents );
    }
    
    # Grab the first 'section' from the file to parse it.
    #
    # Note the 'do ... while', we do this because $line is
    # already populated from above, and it should be a section
    # header.
    do {
	$line = nextLine( \$fileContents );
	$line =~ s/\r\n$//;
	push( @sectionLines, $line );
    } while ( $fileContents !~ m/^\[.+\]\r\n/ );
    
    # The actual parsing is done here.
    #
    # The results are returned as a two-item list, perfect to go straight
    # into our %sections hash.
    %sections = parseSection( @sectionLines );
    
    # Make sure we got a setup section.
    if( !exists( $sections{ 'setup' } ) ) {
	warn( "$0: did not find setup section in file '$file'\n" );
	return( 0 );
    }

    # Set values in $self.
    $self->product( @{ $sections{ 'setup' }->{ 'product' } } );
    $self->version( @{ $sections{ 'setup' }->{ 'version' } } );
    $self->archive( @{ $sections{ 'setup' }->{ 'archive' } } );

    # Populate @requires and @groups.
    @requires = @{ $sections{ 'setup' }->{ 'requires' } };
    @groups = @{ $sections{ 'setup' }->{ 'group' } };

    my( @sectionsToGet ) = ( @requires, @groups );

    # Parse all the sections specified in the 'setup'.
    while( @sectionsToGet ) {
	
	my( @sectionLines, $sectionName, $section, $line );

	# Grab the next section, put into @sectionLines.
	#
	# You know what's also a pisser?  Now that I wrote the
	# Manifest.ini part, I can't help but wonder if that's a
	# better way to parse the .ini-type stuff here.  Of course,
	# I'm just sick of it and want to get this out the door ...
	# maybe I'll sort that out later.
	do {
	    $line = nextLine( \$fileContents );
	    $line =~ s/\r\n$//;
	    push( @sectionLines, $line );
	} while ( $fileContents !~ m/^\[.+\]\r\n/ );

	# Parse the gathered section.
	( $sectionName, $section ) = parseSection( @sectionLines );
	$sections{ $sectionName } = $section;

	# Remove this section from our @sectionsToGet list.
	@sectionsToGet = grep( lc( $_ ) ne $sectionName, @sectionsToGet );

	# Special processing needed if this is the 'setupgroup'.
	#
	# Note the lc() here ... we've assuming these silly .ini files
	# are case insensitive, but I think that's a bad assumption.
	# Will fix later.
	if( lc( $sectionName ) eq 'setupgroup' ) {

	    # Basically the setupgroup holds information on how long 
	    # the first couple of sections in the umod file are.  We
	    # use this info to extract the second part of the setup
	    # info, and to keep track of where the real files start
	    # in the umod file.

	    my( $line );
	    foreach $line ( @{ $section->{ 'copy' } } ) {
		my( $copy ) = parseSourceLine( $line );
		if( $copy->{ 'src' } eq 'System\Manifest.int' ) {
		    $setupLen = $copy->{ 'size' };
		}
	    }

	} elsif( exists( $section->{ 'file' } ) ) {

	    # If this section has some files listed in it, add them
	    # to the packing list, keeping track of it's offset in
	    # the file and length, etc.

	    my( $line );
	    foreach $line ( @{ $section->{ 'file' } } ) {
		my( $hash ) = parseSourceLine( $line );
		$hash->{ 'start' } = $offset;
		$offset += $hash->{ 'size' };
		push( @packingList, $hash );
	    }
	} elsif( lc( $sectionName ) eq 'umodinigroup' ) {

	    # If this section has some ini lines listed in it, add them
	    # to the ini changes list.

	    my( $line );
	    foreach $line ( @{ $section->{ 'ini' } } ) {
		my( $hash ) = parseIniLine( $line );
		push( @iniChanges, $hash );
	    }
	}
    }

    # Set variables in $self.
    $self->requires( map { $_ => $sections{ $_ } } @requires );
    $self->groups( map { $_ => $sections{ $_ } } @groups );
    $self->{ 'packingList' } = \@packingList;
    $self->{ 'iniChanges' } = \@iniChanges;

    # Store away the contents of the umod that has files in them.
    $self->{ 'contents' } = substr( $fileContents, $setupLen );

    # Snip off the second section of the setup part and parse it up.
    my( %setupini )
      = parseSection( split( /\r\n/, substr( $fileContents, 0, $setupLen-1 ) ) );

    # Set variables based on the setup section we just snarfed.
    $self->productname(  @{ $setupini{ 'setup' }->{ 'localproduct' } } );
    $self->producturl(   @{ $setupini{ 'setup' }->{ 'producturl'   } } );
    $self->versionurl(   @{ $setupini{ 'setup' }->{ 'versionurl'   } } );
    $self->developer(    @{ $setupini{ 'setup' }->{ 'developer'    } } );
    $self->developerurl( @{ $setupini{ 'setup' }->{ 'developerurl' } } );
    $self->readmefile  ( @{ $setupini{ 'setup' }->{ 'readme'       } } );

    return( 1 );

}
  
# ----------------------------------------------------------------

# Parse the next line off of the big scalar that are our contents.

sub nextLine {

    my( $contents ) = shift;
    my( $line );

    if( $ { $contents } =~ m/^.*\r\n/ ) {
	$ { $contents } = $';
	return( $& );
    } else {
	return( undef );
    }

}

# ----------------------------------------------------------------

# Parse a section of ini-like cra^H^H^Hstuff.
#
# I knew being familiar with Windows ini files would be good for 
# something one day.  Not.

sub parseSection {

    my( @lines ) = @_;
    my( $line, $sectionName, %settings );
    my( @sectionData );

    # Don't be confused by what's going on here.  The steps are:

    # while we have lines left
    while( $line = shift( @lines ) ) {

	# skip lines that aren't a section header (paranoia check)
	next if( $line !~ m/^\[(.+)\]/ );

	# @sectionData gets returned, $1 is the key to the hash we build
	push( @sectionData, lc( $1 ) );

	my( %hash );

	# while we have lines left, inner loop
	while( $line = shift( @lines ) ) {

	    # finish inner loop if we reached next section header
	    last if( $line =~ m/^\[.+\]/ );

	    # split the line up on /=/ and stick it into the hash
	    my( $key, $value ) = split( /=/, $line, 2 );
	    $key = lc( $key );

	    # add value to hash, creating a new anon list as necessary
	    if( exists( $hash{ $key } ) ) {
		push( @{ $hash{ $key } }, $value );
	    } else {
		$hash{ $key } = [ $value ];
	    }

	}

	# put line back if it's a section header
	unshift( @lines, $line )
	  if( $line =~ m/^\[.+\]/ );

	# add the hash to the list we'll return
	push( @sectionData, \%hash );

    }

    return( @sectionData );

}

# ----------------------------------------------------------------

# Parse a given 'Src' line.
#
# These lines exist in the ini sections of the umod and contain 
# information on files packed into the umod.  We do some simple
# processing to return a hash ref atomically.

sub parseSourceLine {

    my( $line ) = shift;
    my( %hash );

    $line =~ s/^\((.+)\)$/$1/;
    %hash = map {
	my( $key, $value ) = split( /=/, $_, 2 );
	( lc( $key ), $value )
    } split( /,/, $line );
#    $hash{ 'src' } =~ s/\\/\//g;

    return( \%hash );

}

# ----------------------------------------------------------------

# Parse a given 'Ini' line.
#
# These lines exist in the umodINIGroup sections of the umod and
# contain information on changes to the ini files.  We do some
# simple processing to return a hash ref atomically.

sub parseIniLine {

    my( $line ) = shift;
    my( %hash );

    # section can contain "."
    $line =~ s/(.*),(.*)\.(.*)=(.*)/
	$hash{ 'file' } = $1;
	$hash{ 'section' } = $2;
	$hash{ 'key' } = $3;
	$hash{ 'value' } = $4/e;

    return( \%hash );

}

# ----------------------------------------------------------------

# Extract the listed files.
#
# Files are expected to come in hash refs as returned from
# parseSourceLine above.  These hashes contain filenames, sizes, etc.

sub extract {

    my( $self     ) = shift;
    my( @srcFiles ) = @_;
    my( $srcFile );

    foreach $srcFile ( @srcFiles ) {

	my( $file ) = $srcFile->{ 'src' };
	my( $depth ) = 0;

	# Silly DOS backslashes ... 
	$file =~s-\\-/-g;

	# Set our working dir to UT base.
	my( $workDir ) = $self->utbase . '/';

	# First take apart $file makeing any directories
	while( $file =~ m-^(.+)/- ) {
	    my( $dir ) = $1;
	    $file = $';
	    if( ! -d "${workDir}$dir"
		and ! mkdir( "${workDir}$dir", 0755 ) ) {
		warn( "$0: error making dir $workDir$dir: $!\n" );
		return( undef );
	    }
	    # Add this to our 'working dir', as used above and below.
	    $workDir .= "$dir/";
	}

	# Don't clobber file if it exists, skip to next.
	#
	# Should add a 'force' option to get around this.
	if( -e "$workDir$file" ) {
	    warn( "$0: file exists, skipping: $file\n" );
	    next;
	}

	if( !open( OUTFILE, ">${workDir}$file" ) ) {
	    warn( "$0: error opening file for write '$workDir$file': $!\n" );
	    return( undef );
	}
	print( OUTFILE substr( $self->{ 'contents' },
			       $srcFile->{ 'start' },
			       $srcFile->{ 'size' } ) );
	if( !close( OUTFILE ) ) {
	    warn( "$0: error closeing or writing file '$workDir$file': $!\n" );
	    return( undef );
	}

    }

    return( 1 );

}

# ----------------------------------------------------------------

# Return the contents of the readme file, if it exists and if found.

sub readme {

    my( $self ) = shift;
    my( $readmeSrc );

    # Make sure there is a readme file as listed in Setup group.
    return( undef )
      if( !defined( $self->readmefile ) );

    ( $readmeSrc ) = grep( $_->{ 'src' } eq $self->readmefile, 
			   $self->packingList );

    return( $readmeSrc 
	    ? substr( $self->{ 'contents' }, $readmeSrc->{ 'start' }, $readmeSrc->{ 'size' } )
	    : undef );
		    

}

# ----------------------------------------------------------------