Change wich remove commercial gui licence
[mdictionary] / dh_make
1 #!/scratchbox/tools/bin/perl 
2 #
3 # dh_make - Script to Debianize a source archive
4 #
5 use Getopt::Long;
6 use Cwd;
7
8 #Getopt::Long::Configure ("bundling");  
9 #Getopt::Long::Configure ("bundling_override");  
10
11 # Some important parameters
12 $DHLIB="/scratchbox/devkits/debian/share/debhelper/dh_make";
13 $POLICY_VERSION="3.6.0";
14 $DH_MAKE_VERSION="0.36";
15 %PACKAGE_TYPES = ( 's' => 'Single', 'l' => 'Library', 'm' => 'Multi-Binary',
16                    'k' => 'Kernel Module');
17 $DASHLINE="";
18
19 $license="";
20 $email="";
21 $username="";
22 $package_name="";
23 $cap_package_name="";
24 $version="";
25 $fullname = "";
26 $source_file="";
27 $debian_native = 0;
28 $package_type="";
29 $CHANGELOG="";
30 $PRESERVE="";
31 $add_missing = 0;
32 $custom = "";
33 $no_defaults = 0;
34 $overlay = "";
35 $forced_package_name="";
36
37 my $dummy;
38
39 sub process_file(@)
40 {
41    my ($infile, $outfile) = @_;  
42    my $line;
43
44    if ( $main::overlay eq "" )
45    {
46      if ( $main::add_missing && -f $outfile) {
47        print "File $outfile exists, skipping.\n";
48        return;
49      }
50    }
51
52     open IN, "<$infile" or die "Unable to open template file $infile for reading: $! \n";
53     open OUT, ">$outfile" or die "Unable to open file $outfile for writing: $! \n";
54     while (defined($line = <IN>))
55     {
56       $line =~ s/#PACKAGE#/$main::package_name/g;
57       $line =~ s/#UCPACKAGE#/$main::uc_package_name/g;
58       $line =~ s/#VERSION#/$main::version/g;
59       $line =~ s/#EMAIL#/$main::email/g;
60       $line =~ s/#DATE#/$main::date/g;
61       $line =~ s/#SHORTDATE#/$main::shortdate/g;
62       $line =~ s/#CHANGELOGS#/$main::CHANGELOG/g;
63       $line =~ s/#PRESERVE#/$main::PRESERVE/g;
64           $line =~ s/#CONFIG_STATUS#/$main::CONFIG_STATUS/g;
65       $line =~ s/#CONFIGURE#/$main::CONFIGURE/g;
66           $line =~ s/#CONFIGURE_STAMP#/$main::CONFIGURE_STAMP/g;
67       $line =~ s/#DPKG_ARCH#/$main::DPKG_ARCH/g;
68       $line =~ s/#INSTALL#/$main::INSTALL/g;
69       $line =~ s/#CLEAN#/$main::CLEAN/g;
70       $line =~ s/#USERNAME#/$main::username/g;
71       $line =~ s/#POLICY#/$main::POLICY_VERSION/g;
72       $line =~ s/#DASHLINE#/$main::DASHLINE/g;
73           $line =~ s/#PHONY_CONFIGURE#/$main::PHONY_CONFIGURE/g;
74
75       print OUT $line;
76     }
77     close IN;
78     close OUT;
79 }
80
81 sub show_version
82 {
83   print "dh_make - Script to Debianize a regular source archive, version $main::DH_MAKE_VERSION\n\n";
84   print "Copyright (C) 1998-2004 Craig Small <csmall\@debian.org>\n";
85   print "This is free software; see the source for copying conditions.  There is NO\n";
86   print "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n";
87 }
88 sub show_help
89 {
90   show_version();
91   print <<EOF
92   Usage: dh_make [options]
93   -c, --copyright <type>    use <type> of license in copyright file
94                             (gpl|lgpl|artistic|bsd)
95   -e, --email <address>     use <address> as the maintainer e-mail address
96   -n, --native              the program is Debian native, don\'t generate .orig
97   -f, --file <file>         specify file to use as the original source archive
98   -s, --single              set package class to single
99   -m, --multi               set package class to multiple binary
100   -l, --library             set package class to library
101   -k, --kmod                set package class to kernel module
102   -a, --addmissing          reprocess package and add missing files
103   -t, --templates <dir>      apply customizing templates in <dir>
104   -d  --defaultless         skip the default debian and package class templates
105   -o, --overlay <dir>       reprocess package using template in <dir>
106   -p, --packagename <name>  force package name to be <name>
107   -h, --help                display this help screen and exit
108   -v, --version             show the version and exit
109
110 By Craig Small <csmall\@debian.org>
111 Based on deb-make by Christoph Lameter <clameter\@debian.org>.
112 Custom template support by Bruce Sass <bsass\@edmc.net>.
113 EOF
114 }
115
116 sub parse_args
117 {
118   my ($dohelp,$doversion, $single,$multi,$library );
119   %options = ('copyright' => \$main::license,
120               'email' => \$main::email,
121               'file' => \$main::source_file,
122               'help' => \$dohelp,
123               'version' => \$doversion,
124               'native' => \$main::debian_native,
125               'single' => \$single,
126               'multi' => \$multi,
127               'library' => \$library,
128               'kmod' => \$kmod,
129               'addmissing' => \$main::add_missing,
130               'templates' => \$main::custom,
131               'defaultless' => \$main::no_defaults,
132               'overlay' => \$main::overlay,
133                   'packagename' => \$main::forced_package_name,
134               );
135   if (GetOptions(\%options, "copyright=s", "email=s", "file=s", "templates=s", "overlay=s", "packagename=s", "help", "version", "native", "single", "multi", "library", "kmod", "addmissing", "defaultless") == 0)
136   {
137     show_help();
138     exit;
139   }
140   if ($doversion)
141   {
142     show_version();
143     exit;
144   }
145   if ($dohelp)
146   {
147     show_help();
148     exit;
149   }
150   if ($single)
151   {
152     $main::package_type = 's';
153   }
154   if ($multi)
155   {
156     $main::package_type = 'm';
157   }
158   if ($library)
159   {
160     $main::package_type = 'l';
161   }
162   if ($kmod)
163   {
164     $main::package_type = 'k';
165   }
166   if ($addmissing)
167   {
168     $main::add_missing = 1;
169   }
170   if ($defaultless)
171   {
172     $main::no_defaults = 1;
173   }
174   $main::license = lc $main::license;
175   if ($main::lincense ne "" && grep(!/gpl|lgpl|artistic|bsd/, $main::license)) {
176     print "Copyright type \"$main::license\" is not gpl, lgpl, artistic or bsd\n";
177     exit;
178   }
179
180 }
181
182 sub get_username
183 {
184   my $tmpusername;
185
186   $tmpusername = $ENV{'DEBFULLNAME'};
187   return $tmpusername if ($tmpusername ne "");
188
189   if (-x '/usr/bin/getent')
190   {
191     $tmpusername = `/usr/bin/getent passwd $ENV{LOGNAME}|awk -F: '\{ print \$5; \}' | cut -f1 -d,`;
192   }
193   chomp($tmpusername);
194   return $tmpusername if ($tmpusername ne "");
195
196   $tmpusername =`awk -F: -vUSER=$ENV{LOGNAME} '\$1 == USER \{ print \$5; \}' /etc/passwd | cut -f1 -d,`;
197   chomp($tmpusername);
198   return $tmpusername if ($tmpusername ne "");
199   
200   if (-x '/usr/bin/ypmatch')
201   {
202     $tmpusername=`ypmatch $ENV{LOGNAME} passwd.byname|awk -F: '\{ print \$5; \}' | cut -f1 -d,`;
203   }
204   chomp($tmpusername);
205   return $tmpusername if ($tmpusername ne "");
206
207   if (-x '/usr/bin/ldapsearch')
208   {
209     $tmpusername = [map {/^(?:gecos|cn): (.*)/} `ldapsearch -Q -LLL uid=$ENV{LOGNAME} gecos cn`]->[0];
210   }
211   chomp($tmpusername);
212   return $tmpusername if ($tmpusername ne "");
213
214   return "unknown";
215 }
216
217 sub get_email() 
218 {
219   if ($ENV{DEBEMAIL} )
220   {
221     return $ENV{DEBEMAIL};
222   }
223   if ($ENV{EMAIL} )
224   {
225     return $ENV{EMAIL};
226   }
227   if (-x '/usr/bin/ldapsearch')
228   {
229     my $mail;
230     $mail = [map {/^mail: (.*)/ && $1} `ldapsearch -Q -LLL uid=$ENV{LOGNAME} mail`]->[0];
231       return $mail if $mail;
232   }
233   if ($ENV{LOGNAME} )
234   {
235     my $mailhost;
236     if ( -e '/etc/mailname'){
237       chomp($mailhost = `cat /etc/mailname`);
238     } else {
239       $mailhost='unknown';
240     } 
241     return  ($ENV{LOGNAME} . '@' . $mailhost);
242   }
243 }
244
245 sub get_package
246 {
247   my $pwd = `pwd`;
248
249   if (( ($main::forced_package_name) && 
250         ($pwd =~ /.*\/($main::forced_package_name)-([0-9][0-9a-zA-Z+\.\-]*)$/)
251           ) || (
252         ($pwd =~ /.*\/(.*)-([0-9][0-9a-zA-Z+\.\-]*)$/)
253           ))
254   {
255     if ($main::forced_package_name) {
256           $main::package_name = $main::forced_package_name;
257         } else {
258       $main::package_name = $1;
259         }
260     $main::uc_package_name = uc $main::package_name;
261     $main::version = $2;
262         # Fullname stays as the original dir
263     $main::fullname = $1 . "-"  . $2;
264   } else {
265     my $pwd = cwd();
266         print <<"EOF";
267 The directory name must be <package>-<version> for dh_make to work!
268 I cannot understand the directory name or you have an invalid directory name!
269
270 Your current directory is $pwd, perhaps you could try going to
271 directory where the sources are?
272 EOF
273     exit 1;
274   }
275   if (! ($main::package_name =~ /^[a-z0-9+.-]+$/)) {
276     print <<"EOF";
277 Package name "$main::package_name" is not in a valid format.
278 Debian policy manual states:
279   "Package names must only consist of lower case letters, digits (0-9),
280    plus (+) or minus (-) signs, and periods (.)"
281 EOF
282     exit 1;
283   }
284 }
285
286 sub get_date
287 {
288   my $tmpdate;
289   if (-x "/scratchbox/devkits/debian/bin/822-date")
290   {
291     $tmpdate = `/scratchbox/devkits/debian/bin/822-date`;
292     chomp($tmpdate);
293     return $tmpdate;
294   } else {
295     die "Unable to find 822-date program in /scratchbox/devkits/debian/bin!\n";
296   }
297 }
298
299 $username = get_username();
300 $email = get_email();
301 $date = get_date();
302 $shortdate = `date '+%B %e, %Y'`;
303 chomp $shortdate;
304 parse_args();
305 if ( ! $overlay eq "" )
306 {
307   #setup for overlay mode
308   $no_defaults = 1;
309   $add_missing = 1;
310   $customer = $overlay;
311 }
312 get_package();
313
314 # Generate a line of dashes, which is as long as '#PACKAGE# for Debian'.
315 for ($i=0; $i<length("$main::package_name for Debian"); $i++)
316 {
317  $DASHLINE = $DASHLINE . '-';
318 }
319
320 if ( ! $no_defaults )
321 {
322   while ($package_type eq "")
323   {
324     print "\nType of package: single binary, multiple binary, library, or kernel module?\n [s/m/l/k] ";
325     $type = <STDIN>;
326     chomp($type);
327     print "\n";
328     $type = lc($type);
329     $main::package_type = 's' if $type eq 's';
330     $main::package_type = 'm' if $type eq 'm';
331     $main::package_type = 'l' if $type eq 'l';
332     $main::package_type = 'k' if $type eq 'k';
333   }
334 }
335
336 # Print what we have found
337 print "Maintainer name : $username\n";
338 print "Email-Address   : $email \n";
339 print "Date            : $date\n";
340 print "Package Name    : $package_name\n";
341 print "Version         : $version\n";
342 print "Type of Package : ";
343 if (exists $PACKAGE_TYPES{$package_type})
344 {
345   print $PACKAGE_TYPES{$package_type};
346 } else {
347   print 'unknown';
348 }
349 if ( $customer ne "" )
350 {
351   print "\nCustomer template : $custom";
352 }
353 if ( $overlay ne "" )
354 {
355   print " (overlay)";
356 }
357 if ( $no_defaults )
358 {
359   print "\nDefault debian and package class templates will not be applied.";
360 }
361 print "\nHit <enter> to confirm: hacked by STRanger, 2006 - you do not have to hit <enter> \n";
362 # $dummy = <STDIN>;
363
364 if (! $debian_native)
365 {
366   if ($source_file)
367   {
368     if (-f $source_file)
369     {
370       system('cp', '-a', "$source_file", "../$package_name\_$version.orig.tar.gz");
371     } else {
372       print "Source archive you specified was not found!\n";
373       exit 1;
374     }
375   } else {
376     if (-d "..$fullname.orig")
377     {
378       print "Skipping copying to $fullname.orig since $fullname.orig exists.\n";
379     } else {
380           if ( -f "../$fullnane.orig.tar.gz")
381           {
382             print "Skipping copying to $fullname.orig since $fullname.org.tar.gz exists.\n";
383           } else {
384         system('cp', '-a', "../$fullname", "../$fullname.orig");
385           }
386     }
387   }
388 }
389 # Figure out where documentation is
390 @DOCS= split / |\n/, `ls -1 N[Ee][Ww][Ss] *[Ff][Aa][Qq]* *.[Tt][Xx][Tt] README* *.README [rR]eadme* *.[rR]eadme [Bb][Uu][Gg][Ss] *[tT][oO][dD][oO]* 2>/dev/null`;
391 # What are our info files
392 @INFOS= split / |\n/, `find . -regex '.*\\.info\\(-[0-9]+\\)?'`;
393 # Figure out where is the first changelog, assign other similar files to docs
394 @changelogs= split / |\n/, `ls *[cC][hH][aA][nN][gG][eE][lL][oO][gG]* [cC][hH][aA][nN][gG][eE][sS]* 2>/dev/null`;
395 $CHANGELOG = $changelogs[0] if ($#changelogs != -1);
396 shift @changelogs;
397 @DOCS = (@DOCS,@changelogs);
398 # Are there any .orig files in the upstream sources
399 @ORIG= split /[ \n]/, `find . -name '\*.orig'`;
400
401 foreach $orig (@ORIG)
402 {
403   $PRESERVE="$PRESERVE --exclude $orig";
404 }
405
406
407 # Setup debian/rules
408 if (-x "./configure" )
409 {
410   $CONFIG_STATUS="config.status";
411   $CONFIGURE_STAMP='';
412   $PHONY_CONFIGURE='';
413   $CONFIGURE="config.status: configure\n".
414              "\tdh_testdir\n".
415                          "\t# Add here commands to configure the package.\n".
416                          "\t".'CFLAGS="$(CFLAGS)" ./configure --host=$(DEB_HOST_GNU_TYPE) --build=$(DEB_BUILD_GNU_TYPE) --prefix=/usr --mandir=\$${prefix}/share/man --infodir=\$${prefix}/share/info'."\n";
417   $DPKG_ARCH="# These are used for cross-compiling and for saving the configure script\n".
418              "# from having to guess our platform (since we know it already)\n".
419                          'DEB_HOST_GNU_TYPE   ?= $(shell dpkg-architecture -qDEB_HOST_GNU_TYPE)'."\n".
420                          'DEB_BUILD_GNU_TYPE  ?= $(shell dpkg-architecture -qDEB_BUILD_GNU_TYPE)'."\n";
421
422
423   # If it is automaked, use DESTDIR insteadof prefix
424   if ( -f 'Makefile.am' ) {
425     # If it is a library then install into tmp
426     if ( $package_type eq "l") {
427       $INSTALL="\$(MAKE) install DESTDIR=\$(CURDIR)/debian/tmp";
428     } else {
429       $INSTALL="\$(MAKE) install DESTDIR=\$(CURDIR)/debian/$package_name";
430     }
431   } else {
432     if ( $package_type eq "l") {
433       $INSTALL="\$(MAKE) install prefix=\$(CURDIR)/debian/tmp/usr";
434     } else {
435       $INSTALL="\$(MAKE) install prefix=\$(CURDIR)/debian/$package_name/usr";
436     }
437   }
438   $CLEAN="\$(MAKE) distclean\n".
439          "ifneq \"\$(wildcard /usr/share/misc/config.sub)\" \"\"\n".
440                  "\tcp -f /usr/share/misc/config.sub config.sub\n".
441                  "endif\n".
442                  "ifneq \"\$(wildcard /usr/share/misc/config.guess)\" \"\"\n".
443                  "\tcp -f /usr/share/misc/config.guess config.guess\n".
444                  "endif\n";
445 } else {
446   if (! -f 'Makefile' && ! -f 'makefile' && ! -f 'GNUmakefile')
447   {
448     print "Currently there is no top level Makefile. This may require additional tuning.\n";
449   }
450   $CONFIGURE_STAMP='configure-stamp';
451   $CONFIG_STATUS='';
452   $CONFIGURE="configure: configure-stamp\n".
453              "configure-stamp:\n". 
454                          "\tdh_testdir\n".
455                          "\t# Add here commands to configure the package.\n\n".
456                          "\ttouch configure-stamp\n";
457   $PHONY_CONFIGURE='configure';
458   $DPKG_ARCH='';
459   if ($package_type eq "l") {
460     $INSTALL="\$(MAKE) install DESTDIR=\$(CURDIR)/debian/tmp";
461   } else {
462     $INSTALL="\$(MAKE) install DESTDIR=\$(CURDIR)/debian/$package_name";
463   }
464   $CLEAN='$(MAKE) clean';
465 }
466
467 # Customize files
468 if ( $add_missing )
469 {
470   if ( ! -d 'debian' )
471   {
472     die "--addmissing  or --overlay flag used but cannot find debian subdirectory\n";
473   }
474 } else {
475   if ( ! -d 'debian')
476   {
477     mkdir 'debian', 0755 or die "Unable to make debian subdirectory: $! \n";
478   } else {
479     print "You already have a debian/ subdirectory in the source tree.\n";
480     print "dh_make will not try to overwrite anything.\n";
481     exit 1;
482   }
483 }
484 chdir 'debian' or die "Unable to chdir to debian subdirectory: $! \n";
485
486 if ( ! -d $DHLIB )
487 {
488 die "Unable to find dh_make's template directory: $! \n";
489 }
490
491 if ( ! $no_defaults )
492 {
493   # General Files
494   @filenames= split / |\n/, `(cd $DHLIB/debian && ls)`;
495   foreach $filename (@filenames)
496   {
497     process_file("$DHLIB/debian/$filename", $filename);
498   }
499
500   # Copyright file
501   if ($license eq '') 
502   {
503     process_file("$DHLIB/licenses/blank", copyright);
504   } else {
505     if ( -r "$DHLIB/licenses/$license" )
506     {
507       process_file("$DHLIB/licenses/$license", copyright);
508     } else {
509       die "Unable to find copyright template file $DHLIB/licenses/$license";
510     }
511   }
512
513   # Special Files
514   @filenames = split / |\n/, `(cd $DHLIB/debian$package_type && ls)`;
515   foreach $filename (@filenames)
516   {
517     process_file("$DHLIB/debian$package_type/$filename", $filename);
518   }
519 }
520
521 # Custom template
522 if ( $custom ne "" )
523 {
524   if ( -d $custom )
525   {
526     @filenames = split /[\n]/, `(cd $custom && ls)`;
527     foreach $filename (@filenames)
528     {
529       process_file("$custom/$filename", $filename);
530     }
531   } else {
532     print "Unable to find the customization directory, $custom\n";
533   }
534 }
535
536
537 if ( -f "docs" )
538 {
539   print "File docs already exists, skipping.\n";
540 } else {
541   open (DOCSFILE,">docs");
542   foreach $doc (@DOCS)
543   {
544     print DOCSFILE "$doc\n";
545   }
546   close (DOCSFILE);
547 }
548
549 if ( -f "info" )
550 {
551   print "File info already exists, skipping.\n";
552 } else {
553   if ($#INFOS > -1 )
554   {
555     open (INFOFILE,">info");
556     foreach $info (@INFOS)
557     {
558       $info =~ s/^\.\///;
559       print INFOFILE "$info\n";
560     }
561     close (INFOFILE);
562   }
563 }
564
565 if ( ! $no_defaults )
566 {
567   if ($debian_native)
568   {
569   @filenames= split / |\n/, `(cd $DHLIB/native;ls)`;
570   foreach $filename (@filenames)
571   {
572       process_file("$DHLIB/native/$filename", $filename);
573     }
574   }
575 }
576
577 @filenames = split / |\n/, `ls package* 2>/dev/null`;
578 if ($#filenames != -1)
579 {
580   foreach $filename (@filenames)
581   {
582     my $oldname = $filename;
583     $filename =~ s/^package/$package_name/;
584     if ( -f $filename)
585     {
586       print "File $filename already exists, skipping.\n";
587     } else {
588       system('mv', $oldname, $filename);
589     }
590   }
591 }
592 chmod 0755, 'rules';
593
594 if ($CONFIG_STATUS ne "")
595 {
596   print "Done. Please edit the files in the debian/ subdirectory now. $package_name\n";
597   print "uses a configure script, so you probably don't have to edit the Makefiles.\n";
598 } else {
599   print "Done. Please edit the files in the debian/ subdirectory now. You should also\n";
600   print "check that the $package_name Makefiles install into \$DESTDIR and not in / .\n";
601 }
602 if ($package_type eq 'l')
603 {
604   print "Make sure you change the package name from $package_name"."BROKEN to something\n".
605         "else, such as $package_name"."1 in the debian/control file.\n";
606 }
607
608 exit 0;