1#! /usr/bin/perl -w
2#
3# genresscript - Generates empty resource scripts based on existing binaries.
4#
5# Copyright (C) 1998-2005 Oskar Liljeblad
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program; if not, write to the Free Software Foundation,
19# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
20#
21
22use Getopt::Long;
23use File::Basename;
24
25# global stuff
26$PROGRAM = 'genresscript';
27$path_w32rtool = &path_or('wrestool','../wrestool/wrestool');
28
29# initialize options
30$arg_base = '';
31$arg_help = $arg_version = 0;
32$arg_type = 'group_icon';
33$arg_sort = 0;
34
35# parse options
36exit 1 if (!GetOptions("t|type=s"      => \$arg_type,
37                       "b|base=s"      => \$arg_base,
38                       "help"          => \$arg_help,
39                       "version"       => \$arg_version,
40                       "s|sort"        => \$arg_sort));
41
42# deal with standard options
43if ($arg_help) {
44	print "Usage: genresscript [OPTION]... [FILE]...\n";
45	print "Generates empty resource scripts based on existing PE or NE binaries.\n";
46	print "\n";
47	print "  -t, --type=ID     list resources of this type instead of group_icon\n";
48	print "  -b, --base=PATH   the default extraction name of all resources\n";
49	print "  -s, --sort        sort alphabetically instead of using order in binary\n";
50	print "\n";
51  print "      --help        display this help and exit\n";
52	print "      --version     output version information and exit\n";
53	print "\n";
54	print "The option --sort can be useful when making scripts from looking at image\n";
55	print "directories (where images are sorted by filename).\n";
56	print "\n";
57	print 'Report bugs to <frank.richter@gmail.com>', "\n";
58	exit;
59}
60if ($arg_version) {
61	print "$PROGRAM (icoutils) 0.32.3\n";
62	print "Written by Oskar Liljeblad.\n\n";
63	print "Copyright (C) 1998-2005 Oskar Liljeblad.\n";
64	print "This is free software; see the source for copying conditions.  There is NO\n";
65	print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
66	exit;
67}
68
69# got no arguments?
70if ($#ARGV == -1) {
71	print STDERR "$PROGRAM: missing file argument\n";
72	print STDERR "Try `$PROGRAM --help' for more information.\n"
73}
74
75# process each non-option argument
76my ($got_any) = 0;
77for ($c = 0 ; $c <= $#ARGV ; $c++) {
78	$got_any = 1 if (process_binary($ARGV[$c], ($c != 0 && $got_any)));
79}
80
81#
82# Subroutines
83#
84
85sub process_binary {
86	my ($file, $any_before) = @_;
87	my ($cmd,$line,$is_first,@res);
88
89	$cmd = "$path_w32rtool -l";
90	$cmd .= " -t$arg_type" if (defined $arg_type && $arg_type ne '');
91	$cmd .= " $file";
92
93	if (!open (IN, "$cmd|")) {
94		warn "$PROGRAM: execution of wrestool failed\n";
95		return;
96	}
97
98	$is_first = 1;
99	@res = ();
100	while (defined ($line = <IN>)) {
101		($type) = ($line =~ /\[type=([^ ]*)/);
102		($name) = ($line =~ / --name=([^ ]*)/);
103		($lang) = ($line =~ / --language=([^ ]*)/);
104
105		$name =~ s/^`(.*)'$/$1/;
106		$type =~ s/^`(.*)'$/$1/;
107		$lang =~ s/^`(.*)'$/$1/ if defined $lang;
108
109		if ($is_first) {
110			print "\n" if ($any_before);
111			print "file ", File::Basename::basename($file), "\n";
112			$is_first = 0;
113		}
114		print "resource $type, $name: $arg_base\n" if (!$arg_sort);
115		push @res, "resource $type, $name: $arg_base\n" if ($arg_sort);
116	}
117
118	print sort(@res) if ($arg_sort);
119
120	return !$is_first;
121}
122
123sub path_or {
124	my ($cmd,$def) = @_;
125
126	my $real = `which $cmd`;
127  return $def if !defined $real;
128  chop $real;
129  return $def if ($real eq '');
130
131	return $real;
132}
133