+our $debugprefix;
+our $debuglevel = 0;
+
+sub initdebug ($) {
+ ($debugprefix) = @_;
+ open DEBUG, ">/dev/null" or die $!;
+}
+
+sub enabledebug () {
+ open DEBUG, ">&STDERR" or die $!;
+ DEBUG->autoflush(1);
+ $debuglevel ||= 1;
+}
+
+sub enabledebuglevel ($) {
+ die if $debuglevel;
+ ($debuglevel) = @_ + 0;
+ enabledebug();
+}
+
+sub printdebug {
+ print DEBUG $debugprefix, @_ or die $! if $debuglevel>0;
+}
+
+sub shellquote {
+ my @out;
+ local $_;
+ foreach my $a (@_) {
+ $_ = $a;
+ if (!length || m{[^-=_./0-9a-z]}i) {
+ s{['\\]}{'\\$&'}g;
+ push @out, "'$_'";
+ } else {
+ push @out, $_;
+ }
+ }
+ return join ' ', @out;
+}
+
+sub printcmd {
+ my $fh = shift @_;
+ my $intro = shift @_;
+ print $fh $intro," " or die $!;
+ print $fh shellquote @_ or die $!;
+ print $fh "\n" or die $!;
+}
+
+sub debugcmd {
+ my $extraprefix = shift @_;
+ printcmd(\*DEBUG,$debugprefix.$extraprefix,@_) if $debuglevel>0;
+}
+