1#!/usr/bin/php
2<?php
3/*
4 Copyright (C) 2011 Hewlett-Packard Development Company, L.P.
5
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 */
19
20/**
21 * \brief Create an 'empty' test file for use in testing
22 *
23 * @version "$Id $"
24 *
25 * @todo need to add:
26 * - parameters, usage
27 * - log of what is getting created with what license
28 *
29 * Created on Mar 15, 2011 by Mark Donohoe
30 */
31
32global $commentChar;
33global $endChar;
34
35 /*
36   * General flow:
37   * - use random suffix if none passed in.
38   * - create the default name, if none passed in
39   *   - Determine comment style
40   * - pick a license randomly
41   * - construct all needed file parts (choosing comment style before)
42   * - write the file.
43   *
44   */
45
46/*
47 * usage: createTestFiles($name, $suffix, $lic?)
48 * if no name, output is to stdout
49 * if no suffix, one is picked
50 * if name, used as a prefix so multiple names can be generated
51 */
52
53function createPHP($FD, $license)
54{
55  global $commentChar;
56  global $endChar;
57
58  $newLine = "\n";
59
60  $startTag = "#!/usr/bin/php\n<?php\n";
61  $endTag = "\n?>\n";
62
63  $sayHey = "echo \"Hello World!$newLine\";\n";
64
65  if(empty($license))
66  {
67    $license = _("Copyright Randy Rando, licensed under the BSD license\n");
68  }
69  $cLic = $commentChar . "\n" . $license . "\n" . $endChar . "\n";
70  $body = $startTag . $cLic . $sayHey . $endTag;
71  //echo "Body is:\n$body\n";
72  $howMany = fwrite($FD,$body);
73  fclose($FD);
74}
75
76function createPerl($FD, $license)
77{
78  global $commentChar;
79  $startTag = "#!/usr/bin/perl\n";
80
81  $sayHey = "print \"Hello World!$newLine\";\n";
82
83  if(empty($license))
84  {
85    $license = _("Copyright Randy Rando, licensed under the BSD license\n");
86  }
87  $cLic = $commentChar . "\n" . $license . "\n" . $endChar . "\n";
88  $body = $startTag . $cLic . $sayHey . $endTag;
89  //echo "Body is:\n$body\n";
90  $howMany = fwrite($FD,$body);
91  fclose($FD);
92  echo "Perl files not implimented\n";
93}
94
95function createCprog($FD, $license)
96{
97  global $commentChar;
98  echo "C files not implimented\n";
99}
100
101function createHeader($FD, $license)
102{
103  global $commentChar;
104  echo "header files not implimented\n";
105}
106
107function createSh($FD, $license)
108{
109  global $commentChar;
110  echo "shell files not implimented\n";
111}
112function createTxt($FD, $license)
113{
114  global $commentChar;
115  echo "Text files not implimented\n";
116}
117function createJs($FD, $license)
118{
119  global $commentChar;
120  echo "Javascript files not implimented\n";
121}
122
123function createFile($suffix=NULL, $name=NULL, $license=NULL)
124{
125  require_once 'licenseText.php';
126  global $commentChar;
127  global $endChar;
128
129  echo "after require\n";
130
131  $licenses = array(
132  $gpl2Text,
133  $gpl3Text,
134  $bsd_d,
135  $apache2,
136  );
137
138  $suffix = array(
139  '.c',
140  '.h',
141  '.php',
142  '.pl',
143  '.sh',
144  '.txt',
145  '.js',
146  );
147
148  $defaultName = 'TestFile';
149
150  $sufixNum = rand(0,count($suffix)-1);
151  $licensePick = rand(0,count($licenses)-1);
152
153  /*
154   * General flow:
155   * - use random suffix if none passed in.
156   * - create the default name, if none passed in
157   *   - Determine comment style
158   * - pick a license randomly
159   * - construct all needed file parts (choosing comment style before)
160   * - write the file.
161   *
162   */
163
164  // first imp: just use defaults for now.
165
166  $name = $defaultName . $suffix[$sufixNum];
167  echo "***** Getting license *****\n";
168  $license = $licenses[$licensePick];
169
170  // create the file
171  echo "name is:$name\n";
172  $FD = fopen($name,'w');
173
174  $commentChar = NULL;
175  $endChar = NULL;
176  $newLine = "\n";
177
178  switch ($suffix[$sufixNum])
179  {
180    case '.c':
181      $commentChar = '/*';
182      $endChar = '*/';
183      $rtn = createCprog($FD, $license);
184      break;
185    case '.h':
186      $commentChar = '/*';
187      $endChar = '*/';
188      $rtn = createHeader($FD, $license);
189      break;
190    case '.php':
191      $commentChar = '/*';
192      $endChar = '*/';
193      $rtn = createPHP($FD, $license);
194      break;
195    case '.js':
196      $commentChar = '/*';
197      $endChar = '*/';
198      break;
199    case '.pl':
200      $commentChar = '#';
201      $rtn = createPerl($FD, $license);
202      break;
203    case '.js':
204      $commentChar = '/*';
205      $endChar = '*/';
206      $rtn = createJs($FD, $license);
207      break;
208    case '.sh':
209      $commentChar = '#';
210      $rtn = createSh($FD, $license);
211      break;
212    case '.txt':
213      $commentChar = '#';
214      $rtn = createTxt($FD, $license);
215      break;
216
217    default:
218      $commentChar = NULL;   // should never happen
219      break;
220  }
221}
222
223echo "*********** starting.... *************\n";
224createFile();
225
226?>