summaryrefslogtreecommitdiff
path: root/bin/cle.pl
blob: 03f8785f7cdebbe4956dbf3e6f85c62d74adbeb9 (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
eval '(exit $?0)' && eval 'exec perl -S $0 ${1+"$@"}'
    & eval 'exec perl -S $0 $argv:q'
    if 0;

# ******************************************************************
#      Author: Chad Elliott
#        Date: 6/18/2002
#         $Id$
# ******************************************************************

use strict;
use Cwd;
use File::Basename;
use Sys::Hostname;

if ( $^O eq 'VMS' ) {
  require VMS::Filespec;
  import VMS::Filespec qw(unixpath);
}

unshift(@INC, getExecutePath($0) . 'ChangeLogEditor');

require ChangeLogEdit;
require EmailTranslator;

# ************************************************************
# Subroutine Section
# ************************************************************

sub which {
  my($prog)   = shift;
  my($exec)   = $prog;
  my($part)   = '';
  if ( $^O eq 'VMS' ) {
    my($envSep) = ';';
    if (defined $ENV{'PATH'}) {
      foreach $part (split(/$envSep/, $ENV{'PATH'})) {
        $part .= "$prog";
        if ( -x $part ) {
          $exec = $part;
          last;
        }
      }
    }
  }
  else  {
    my($envSep) = ($^O eq 'MSWin32' ? ';' : ':');
    if (defined $ENV{'PATH'}) {
      foreach $part (split(/$envSep/, $ENV{'PATH'})) {
        $part .= "/$prog";
        if ( -x $part ) {
          $exec = $part;
          last;
        }
      }
    }
  }

  return $exec;
}


sub getExecutePath {
  my($prog) = shift;
  my($loc)  = '';

  if ( $^O eq 'VMS' ) {
    if ($prog ne basename($prog)) {
      my($dir) = unixpath( dirname($prog) );
      if ($prog =~ /^[\/\\]/) {
        $loc = $dir;
      }
      else {
        $loc = unixpath(getcwd()) . $dir;
      }
    }
    else {
      $loc = unixpath( dirname(which($prog)) );
    }

    if ($loc eq '.') {
      $loc = unixpath( getcwd() );
    }
  } else {
    if ($prog ne basename($prog)) {
      if ($prog =~ /^[\/\\]/ ||
          $prog =~ /^[A-Za-z]:[\/\\]?/) {
        $loc = dirname($prog);
      }
      else {
        $loc = getcwd() . '/' . dirname($prog);
      }
    }
    else {
      $loc = dirname(which($prog));
    }

    $loc =~ s/\/\.$//;

    if ($loc eq '.') {
      $loc = getcwd();
    }

    if ($loc ne '') {
      $loc .= '/';
    }
  }

  return $loc;
}

sub getDefaultDomain {
  my($domain) = undef;
  my($host)   = hostname();

  if (defined $host) {
    ## First try the hostname
    if ($host =~ /[^\.]+\.(.*)/) {
      $domain = $1;
    }
    else {
      ## Next try the hosts file
      my($hosts) = ($^O eq 'MSWin32' ?
                      "$ENV{SystemRoot}/system32/drivers/etc/hosts" :
                      '/etc/hosts');
      my($fh)    = new FileHandle();
      if (open($fh, $hosts)) {
        while(<$fh>) {
          if (/$host\.(.*)/) {
            $domain = $1;
            last;
          }
        }
        close($fh);
      }

      if (!defined $domain) {
        ## Next try ipconfig on Windows
        if ($^O eq 'MSWin32') {
          if (open($fh, 'ipconfig /all |')) {
            while(<$fh>) {
              if (/Primary\s+DNS\s+Suffix[^:]+:\s+(.*)/) {
                $domain = $1;
              }
              elsif (/DNS\s+Suffix\s+Search[^:]+:\s+(.*)/) {
                $domain = $1;
              }
            }
            close($fh);
          }
        }
        else {
          ## Try /etc/resolv.conf on UNIX
          if (open($fh, '/etc/resolv.conf')) {
            while(<$fh>) {
              if (/search\s+(.*)/) {
                $domain = $1;
                last;
              }
            }
            close($fh);
          }
        }
      }
    }
  }
  return $domain;
}


sub usageAndExit {
  my($arg)  = shift;
  my($base) = basename($0);
  if (defined $arg) {
    print "$arg\n\n";
  }
  print "Usage: $base [ChangeLog File] [user name] [email address]\n" .
        "        " . (' ' x length($base)) . "[-d <dir1 dir2 ... dirN>]\n\n" .
        "       Uses cvs to determine which files are modified or added\n" .
        "       and generates a bare ChangeLog entry based on those files.\n" .
        "       This script should be run at the same directory level in\n" .
        "       which the ChangeLog exists.  The entry is prepended to the\n" .
        "       existing ChangeLog.\n" .
        "\n" .
        "       Email addresses are generated with a certain set of\n" .
        "       defaults and can be modified using various environment\n" .
        "       variables.  By default email addresses are generated\n" .
        "       using the user last name followed by an underscore and\n" .
        "       the first initial of the user first name followed by the\n" .
        "       email domain.\n" .
        "\n" .
        "       REPLYTO      If this environment variable is set, the value\n" .
        "                    is used as the email address.\n" .
        "       CL_USERNAME  This environment variable is used to override\n" .
        "                    the user name (obtained from the password file).\n" .
        "\n" .
        "       The user name and email address can be passed as a parameter to\n" .
        "       this script.  If either is not passed, then the script will try\n" .
        "       to determine it automatically.\n" .
        "\n" .
        "       If -d is used, everything on the command line after it is\n" .
        "       considered a directory or file to be considered in the\n" .
        "       ChangeLog entry.\n";
  exit(0);
}


# ************************************************************
# Subroutine Section
# ************************************************************

my($file)     = undef;
my($name)     = undef;
my($email)    = undef;
my(@dirs)     = ();
my($restdirs) = 0;

foreach my $arg (@ARGV) {
  if ($restdirs) {
    push(@dirs, $arg);
  }
  elsif ($arg eq '-h') {
    usageAndExit();
  }
  elsif ($arg eq '-d') {
    $restdirs = 1;
  }
  elsif ($arg =~ /^\-/) {
    usageAndExit("Unrecognized parameter: $arg");
  }
  elsif (!defined $file) {
    $file = $arg;
  }
  elsif (!defined $name) {
    $name = $arg;
  }
  elsif (!defined $email) {
    $email = $arg;
  }
}

if (!defined $file) {
  $file = 'ChangeLog';
}
if (!defined $name) {
  my(@pwd)    = ();
  if (defined $ENV{CL_USERNAME}) {
    $pwd[6] = $ENV{CL_USERNAME};
  }
  else {
    if ($^O eq 'MSWin32' || $^O eq 'cygwin') {
      $pwd[6] = 'unknown';
    }
    else {
      @pwd = getpwuid($<);
      $pwd[6] =~ s/,//g;
    }
  }
  $name = $pwd[6];
}

if (!defined $email) {
  my($trans) = new EmailTranslator(getDefaultDomain());
  $email = $trans->translate($name);
}

my($editor) = new ChangeLogEdit($name, $email);
my($status, $error, $unknown) = $editor->edit($file, @dirs);

if (defined $unknown) {
  my(@uarray) = @$unknown;
  if ($#uarray >= 0) {
    print "WARNING: The following files are unknown to the ",
          "revsion control system:\n";
    foreach my $unk (@uarray) {
      print "$unk\n";
    }
    print "\n";
  }
}

if ($status) {
  print "You are now ready to edit the $file.\n";
}
else {
  print "$error\n";
}

exit($status ? 0 : 1);