1eval 'exec perl -wS $0 ${1+\"$@\"}'
2    if 0;
3
4# This program has to start for the new convwatch,
5# once on Windows environment and once on Linux environment
6# Solaris is handled by the linux also.
7#
8# This program polls the database (documentcompare) every 60s for new jobs
9# it runs over the given directory from documentpoolpath and pool, and create for every file
10# a new database entry in documents.
11#
12
13BEGIN
14{
15        #       Adding the path of this script file to the include path in the hope
16        #       that all used modules can be found in it.
17        $0 =~ /^(.*)[\/\\]/;
18        push @INC, $1;
19}
20
21use ConvwatchHelper;
22use CallExternals;
23use stringhelper;
24use filehelper;
25use oshelper;
26use timehelper;
27use cwstestresulthelper;
28
29use strict;
30use Cwd;
31use File::Basename;
32use English;                  # $OSNAME, ...
33use Getopt::Long;
34use File::Path;
35use Cwd 'chdir';
36
37my $cwd = getcwd();
38
39our $help;                    # Help option flag
40our $version;                 # Version option flag
41our $test;
42
43our $version_info = 'convwatch.pl $Revision: 1.24 $ ';
44
45our $SOLARENV;
46our $COMMON_ENV_TOOLS;
47
48
49our $documentpoolname;
50our $documentpoolpath;
51our $dbdistinct;
52our $sParentDistinct;
53our $sCurrentDocumentPool;
54
55our $fs;
56our @aEntries;
57
58# Prototypes
59# sub getJavaFileDirSeparator();
60sub readdirectory($$$);
61sub putDocumentInDB($$$);
62
63# flush STDOUT
64my $old_handle = select (STDOUT); # "select" STDOUT and save # previously selected handle
65$| = 1; # perform flush after each write to STDOUT
66select ($old_handle); # restore previously selected handle
67
68setPrefix("gfxcmp");
69
70if (!GetOptions(
71                "test"          => \$test,
72                "help"          => \$help,
73                "version"       => \$version
74                ))
75{
76    print_usage(*STDERR);
77    exit(1);
78}
79if ($help)
80{
81    print_usage(*STDOUT);
82    exit(0);
83}
84# Check for version option
85if ($version)
86{
87    print STDERR "$version_info\n";
88    exit(0);
89}
90
91# ------------------------------------------------------------------------------
92# within mysql it is better to use only '/'
93$fs = "/"; # getJavaFileDirSeparator();
94# ------------------------------------------------------------------------------
95sub readdirectory($$$)
96{
97    my $startdir = shift;
98    my $sValues  = shift;
99    my $hook     = shift;
100
101    my $myfile;
102
103    local *DIR;
104    chdir $startdir;
105    cwd();
106    if (! endswith($startdir, $fs))
107    {
108        $startdir .= $fs;
109    }
110
111    my $nCountFiles = 0;
112    if (opendir (DIR, $startdir))           # Directory oeffnen
113    {
114        while ($myfile = readdir(DIR))
115        {                                  # ein filename holen
116            #if (! -l $myfile)              # not a link
117            #{
118            if (-d $myfile ) # is a directory
119            {
120                if ( -l $myfile)
121                {
122                    next;
123                }
124                if ($myfile ne "." && $myfile ne "..")
125                {
126                    my $sNewStartDir = $startdir . $myfile ."/";    # neuen Directorystring erstellen
127                    if ($sNewStartDir =~ "^\/proc" ||
128                        $sNewStartDir =~ "^\/dev" ||
129                        $sNewStartDir =~ "^\/udev" ||
130                        $sNewStartDir =~ "lost+found" )
131                    {
132                        next;
133                    }
134                    # my $sNewDestDir  = $destdir . $myfile ."/";
135                    # do a recursive call
136                    # $nCountFiles++;
137                    my $nFileCount = readdirectory($sNewStartDir, $sValues, $hook);
138                    # workOnDir($sNewDir, $nFileCount);
139                    $nCountFiles += $nFileCount;
140
141                    chdir ($startdir);                      # zurueckwechseln.
142                    cwd();
143                }
144            }
145            else
146            {
147                # File must exist, be a regular file and must not be the $onlyOnFile
148                if (-f $myfile)
149                {
150                    # print " $startdir" . "$myfile\n";
151                    $nCountFiles++;
152                    # workOnFile($startdir, $myfile, $destdir);
153                    $hook->($startdir, $myfile, $sValues);
154                }
155            }
156            #}
157            #else
158            #{
159            #    print "linked file: $dir/$myfile\n";
160            #}
161        }
162        closedir(DIR);
163    }
164    else
165    {
166        print "could not open $startdir\n";
167    }
168    return $nCountFiles;
169}
170# ------------------------------------------------------------------------------
171sub putDocumentInDB($$$)
172{
173    my $currentDir = shift;
174    my $currentFile = shift;
175    my $sValues = shift;
176
177    my $sSourceFilename = $currentDir . $currentFile;
178    # we cut down all the previous names like documentpoolpath and the documentpoolname
179    $sSourceFilename = substr($sSourceFilename, length($sCurrentDocumentPool . $fs));
180
181    my $sSQL = "INSERT INTO documents (dbdistinct2, name, pagecount, priority, parentdistinct) VALUES";
182    $sSQL .= "('" . $dbdistinct . "', '" . $sSourceFilename . "', 0, 1, '". $sParentDistinct . "')";
183    # print $sSQL . "\n";
184
185    push(@aEntries, $sSQL);
186    # ExecSQL($sSQL);
187}
188
189# ------------------------------------------------------------------------------
190sub createDBEntriesForEveryDocument($)
191{
192    my $sStr = shift;
193    if ($sStr =~ /^MySQL-Error/ )
194    {
195        # we don't do anything if an error occured
196        return;
197    }
198
199    # interpret the follows string
200    # documentpoolpath='//so-gfxcmp-documents/doc-pool', documentpool='demo_lla', dbdistinct=62,
201
202    # my $sDocumentPoolDir;
203    if ( $sStr =~ /documentpoolpath='(.*?)',/ )
204    {
205        $documentpoolpath = $1;
206    }
207    if (! $documentpoolpath)
208    {
209        print "Error: no value for documentpoolpath found.\n";
210        return;
211    }
212
213    # my $sDocumentPool;
214    if ( $sStr =~ /documentpool='(.*?)',/ )
215    {
216        $documentpoolname = $1;
217    }
218    if (! $documentpoolname)
219    {
220        print "Error: no value for documentpool found.\n";
221        return;
222    }
223    # my $dbdistinct;
224    if ( $sStr =~ /dbdistinct2='(\S*?)',/ )
225    {
226        $dbdistinct = $1;
227    }
228    if (! $dbdistinct)
229    {
230        print "Error: no dbdistinct given.\n";
231        return;
232    }
233
234    if (! -d $documentpoolpath )
235    {
236        my $sEnv = getEnvironment();
237        if ( isUnixEnvironment() )
238        {
239            $documentpoolpath = "/net/so-gfxcmp-documents" . $documentpoolpath;
240        }
241        if ( -d $documentpoolpath )
242        {
243            print "Warning: given documentpoolpath seems to be local, fix to '$documentpoolpath'\n";
244            my $sSQL = "UPDATE documentcompare SET documentpoolpath='$documentpoolpath' WHERE dbdistinct2='$dbdistinct'";
245            print "$sSQL\n";
246            ExecSQL($sSQL);
247        }
248        else
249        {
250            print "Error: documentpoolpath '$documentpoolpath' not found. Don't insert anything.\n";
251            my $sSQL = "UPDATE documentcompare SET state='failed',info='documentpoolpath not found.' WHERE dbdistinct2='$dbdistinct'";
252            print "$sSQL\n";
253            ExecSQL($sSQL);
254            return;
255        }
256    }
257    # create the documentpool directory, to run through
258    $sCurrentDocumentPool = $documentpoolpath;
259    if (! endswith($sCurrentDocumentPool, $fs))
260    {
261        $sCurrentDocumentPool .= $fs;
262    }
263    $sCurrentDocumentPool .= $documentpoolname;
264
265    if ( -d $sCurrentDocumentPool )
266    {
267        if ( $sStr =~ /parentdistinct='(.*?)',/ )
268        {
269            $sParentDistinct = $1;
270        }
271        else
272        {
273            $sParentDistinct = "";
274        }
275
276        # remove any doubles, if any
277        my $sSQL = "DELETE FROM documents WHERE dbdistinct2='$dbdistinct'";
278        print "$sSQL\n";
279        ExecSQL($sSQL);
280
281        # run over the whole given document pool and store every found document name in the database
282        readdirectory($sCurrentDocumentPool, "", \&putDocumentInDB);
283
284        chdir $cwd;
285        cwd();
286
287        foreach $sSQL (@aEntries)
288        {
289            # print "# $sSQL\n";
290            print "$sSQL\n";
291            ExecSQL($sSQL);
292        }
293
294        my $sSQL = "UPDATE documentcompare SET state='inprogress' WHERE dbdistinct2='$dbdistinct'";
295        print "$sSQL\n";
296        ExecSQL($sSQL);
297        print "----------------------------------------------------------------------\n";
298        $sParentDistinct = "";
299        @aEntries = ();
300    }
301    else
302    {
303        print "Error: Given document pool '$sCurrentDocumentPool' doesn't exists.\n";
304        my $sSQL = "UPDATE documentcompare SET state='cancelled' WHERE dbdistinct2='$dbdistinct'";
305        ExecSQL($sSQL);
306        return;
307    }
308    # Send Mail, due to startconvwatch now
309    sendMail($sStr, $documentpoolname, $dbdistinct);
310}
311
312# ------------------------------------------------------------------------------
313sub sendMail($$$)
314{
315    my $sStr = shift;
316    my $documentpool = shift;
317    my $dbdistinct = shift;
318    my $sourceversion;
319    if ( $sStr =~ /sourceversion='(.*?)',/ )
320    {
321        $sourceversion = $1;
322    }
323    if (! $sourceversion)
324    {
325        print "Warning: no value for sourceversion found.\n";
326        return;
327    }
328    my $destinationversion;
329    if ( $sStr =~ /destinationversion='(.*?)',/ )
330    {
331        $destinationversion = $1;
332    }
333    if (! $destinationversion)
334    {
335        print "Warning: no value for destinationversion found.\n";
336        return;
337    }
338    my $mailaddress;
339    if ( $sStr =~ /mailfeedback='(.*?)',/ )
340    {
341        $mailaddress = $1;
342    }
343    if (! $mailaddress)
344    {
345        print "Warning: no value for mailfeedback found.\n";
346        return;
347    }
348
349    # state is 'inprogress', so send a mail
350    # my $sMailAddress = getMailAddress($sDoneStr);
351    my $sParams = "$sourceversion";
352    $sParams .= " $destinationversion";
353    $sParams .= " $documentpool";
354    $sParams .= " $dbdistinct";
355    $sParams .= " $mailaddress";
356    $sParams .= " starts";                    # run through state of convwatch
357
358    my $sMailProgram = appendPath(getQADEVToolsPath(), "mailsend.php");
359
360    my $err;
361    my @lines;
362    my $sLine;
363    ($err, @lines) = callphp(getPHPExecutable(), $sMailProgram, $sParams);
364    foreach $sLine (@lines)
365    {
366        log_print( "Mail: $sLine\n");
367    }
368
369    if ($documentpool eq "EIS-tests")
370    {
371        cwstestresult("running", $dbdistinct, $sourceversion, $destinationversion, $SOLARENV, $COMMON_ENV_TOOLS);
372    }
373}
374# ------------------------------------------------------------------------------
375# ------------------------------------------------------------------------------
376
377my $sEnvironmentCondition;
378if (isWindowsEnvironment())
379{
380    $sEnvironmentCondition = "environment='" . getEnvironment() . "'";
381}
382elsif (isUnixEnvironment())
383{
384    # $sEnvironmentCondition = " ( environment='unxlngi' OR environment='unxsoli' ) ";
385    $sEnvironmentCondition = " environment='" . getEnvironment() . "'";
386}
387else
388{
389    print "Error: wrong environment.\n";
390    exit(1);
391}
392my $sWhereClause = "WHERE ";
393if ($sEnvironmentCondition)
394{
395    $sWhereClause .= $sEnvironmentCondition . " AND ";
396}
397$sWhereClause .= " state='new'";
398
399setToolsPath(getQADEVToolsPath());
400
401# ---------------------------------- main loop ----------------------------------
402while (1)
403{
404    my @aResult;
405    my $sSQL = "SELECT documentpoolpath,documentpool,dbdistinct2,sourceversion,destinationversion,mailfeedback,parentdistinct FROM documentcompare $sWhereClause";
406    @aResult = ExecSQL($sSQL);
407
408    my $aValue;
409    foreach $aValue (@aResult)
410    {
411        # print "# $nValue\n";
412        createDBEntriesForEveryDocument($aValue);
413    }
414    if ($test)
415    {
416        last;
417    }
418
419    # wait 30sec.
420    # wait30seconds();
421    waitAMinute();
422    checkForStop("stop_fill_documents_loop");
423}
424