]> git.donarmstrong.com Git - debhelper.git/blob - dh_getopt.pl
r103: Initial revision
[debhelper.git] / dh_getopt.pl
1 #!/usr/bin/perl
2 #
3 # Because the getopt() program is so horribly broken, I wrote my own argument
4 # processer that uses the find Getopt::Long module. This is used by all
5 # debhelper scripts.
6 #
7 # Joey Hess, GPL copyright 1998.
8
9 # Returns a list of packages in the control file.
10 # Must pass "arch" or "indep" to specify arch-dependant or -independant
11 # packages.
12 sub GetPackages { $type=shift;
13         my $package;
14         my $arch;
15         my @list;
16         open (CONTROL,"<debian/control") || 
17                 ( $parse_error="cannot read debian/control: $!\n" );
18         while (<CONTROL>) {
19                 chomp;
20                 s/\s+$//;
21                 if (/^Package:\s+(.*)/) {
22                         $package=$1;
23                 }
24                 if (/^Architecture:\s+(.*)/) {
25                         $arch=$1;
26                 }
27                 if (!$_ or eof) { # end of stanza.
28                         if ($package &&
29                             (($type eq 'indep' && $arch eq 'all') ||
30                                    ($type eq 'arch' && $arch ne 'all'))) {
31                                 push @list, $package;
32                                 undef $package, $arch;
33                         }
34                 }
35         }
36         close CONTROL;
37
38         return @list;
39 }
40
41 # Passed an option name and an option value, adds packages to the list
42 # of packages. We need this so the list will be built up in the right
43 # order.
44 sub AddPackage { my($option,$value)=@_;
45         if ($option eq 'i' or $option eq 'indep') {
46                 push @packages, GetPackages('indep');
47                 $indep=1;
48         }
49         elsif ($option eq 'a' or $option eq 'arch') {
50                 push @packages, GetPackages('arch');
51                 $arch=1;
52         }
53         elsif ($option eq 'p' or $option eq 'package') {
54                 push @packages, $value;
55         }
56         else {
57                 $parse_error="bad option $option - should never happen!\n";
58         }
59 }
60
61 use Getopt::Long;
62
63 # Enable bundling of short command line options.
64 Getopt::Long::config("bundling");
65
66 # Parse options.
67 GetOptions(
68         "v" => \$verbose,
69         "verbose" => \$verbose,
70
71         "i" => \&AddPackage,
72         "indep" => \&AddPackage,
73
74         "a" => \&AddPackage,
75         "arch" => \&AddPackage,
76
77         "p=s" => \&AddPackage,
78   "package=s" => \&AddPackage,
79
80         "n" => \$noscripts,
81         "noscripts" => \$noscripts,
82
83         "x" => \$include, # is -x for some unknown historical reason..
84         "include-conffiles" => \$include,
85         
86         "d" => \$d_flag,
87         "remove-d" => \$d_flag,
88
89         "r" => \$r_flag,
90         "no-restart-on-upgrade" => \$r_flag,
91
92         "k" => \$k_flag,
93         "keep" => \$k_flag,
94
95         "P=s" => \$tmpdir,
96         "tmpdir=s" => \$tmpdir,
97
98         "u=s", => \$u_params,
99         "update-rcd-params=s", => \$u_params,
100   "dpkg-shlibdeps-params=s", => \$u_params,
101
102         "m=s", => \$major,
103         "major=s" => \$major,
104
105         "V:s", => \$version_info,
106         "version-info:s" => \$version_info,
107
108         "A" => \$all,
109         "all" => \$all,
110 );
111
112 # Check to see if -V was specified. If so, but no parameters were passed,
113 # the variable will be defined but empty.
114 if (defined($version_info)) {
115         $version_info_set=1;
116 }
117
118 # Now output everything, in a format suitable for a shell to eval it. 
119 # Note the last line sets $@ in the shell to whatever arguements remain.
120 print qq{
121 DH_VERBOSE='$verbose'
122 DH_DOPACKAGES='@packages'
123 DH_DOINDEP='$indep'
124 DH_DOARCH='$arch'
125 DH_NOSCRIPTS='$noscripts'
126 DH_EXCLUDE='$include'
127 DH_D_FLAG='$d_flag'
128 DH_R_FLAG='$r_flag'
129 DH_K_FLAG='$k_flag'
130 DH_TMPDIR='$tmpdir'
131 DH_U_PARAMS='$u_params'
132 DH_M_PARAMS='$major'
133 DH_V_FLAG='$version_info'
134 DH_V_FLAG_SET='$version_info_set'
135 DH_PARAMS_ALL='$all'
136 DH_PARSE_ERROR='$parse_error'
137 set -- @ARGV
138 };