1 |
#!/usr/local/bin/perl -w |
2 |
# |
3 |
# program pretvara ch, zh, sh, cc, dh u odgovrajuce |
4 |
# ISO 8859-2 znakove |
5 |
# ozujak 1998. |
6 |
# Hana Breyer Priselac |
7 |
# |
8 |
# poziva se s ./kvake.sh PUT |
9 |
# gdje je PUT puni UNIX path do html dokumenata koje |
10 |
# konvertirate |
11 |
# |
12 |
# 2000-02-16 Dobrica Pavlinusic <dpavlin@pliva.hr> prva perl verzija |
13 |
# 2000-03-01 DbP poravljen ignore_dirs grep bug |
14 |
# |
15 |
# 2000-03-31 Hana sada ignorira sve sto ^eng$, ^eng_ , ^adresar$ u imenu direktorija |
16 |
# 2000-05-03 DbP lock support |
17 |
|
18 |
$mod_time = 10*60; # 10 minuta |
19 |
#$mod_time = 24*60*60; # 1 day |
20 |
|
21 |
$verbose = 0; |
22 |
|
23 |
use Fcntl ':flock'; # import LOCK_* |
24 |
$lock="/tmp/kvake.lock"; |
25 |
|
26 |
#@ignore_dirs=( "/data/web/public/adresar/", "/data/web/public/eng" ); |
27 |
|
28 |
while (defined($ARGV[0]) && $ARGV[0] eq "-v") { |
29 |
$verbose++; |
30 |
shift @ARGV; |
31 |
} |
32 |
print STDERR "verbosity: $verbose\n" if ($verbose); |
33 |
|
34 |
if (! defined(@ARGV) || ($#ARGV < 0)) { |
35 |
@dirs=( "/data/web/public", |
36 |
"/usr/users/it/dpeterc" ); |
37 |
} else { |
38 |
@dirs=@ARGV; |
39 |
} |
40 |
|
41 |
print STDERR "Scanning dirs: @dirs\n" if ($verbose); |
42 |
|
43 |
print STDERR "locking\n" if ($verbose); |
44 |
while (-e "$lock") { |
45 |
sub read_lock_pid { |
46 |
my $pid; |
47 |
open(LOCK,"$lock") || die "can't open lock $lock: $!"; |
48 |
$pid=<LOCK>; |
49 |
close(LOCK); |
50 |
return $pid; |
51 |
} |
52 |
$run_pid=read_lock_pid; |
53 |
print STDERR "lock on pid $run_pid found\n"; |
54 |
# if not over in 15 min. kill (next re-run will do actual work) |
55 |
sleep 15*60; |
56 |
$new_pid=read_lock_pid; |
57 |
if ($run_pid == $new_pid) { |
58 |
print STDERR "killing process $run_pid and clearing lock\n"; |
59 |
kill 9,$run_pid; |
60 |
unlink $lock; |
61 |
die; |
62 |
} |
63 |
} |
64 |
|
65 |
open(LOCK,">$lock") || die "can't open lock $lock: $!"; |
66 |
print LOCK "$$"; |
67 |
flock(LOCK,LOCK_EX); |
68 |
|
69 |
foreach $dir (@dirs) { |
70 |
opendir(DIR,"$dir") || warn "can't open $dir: $!"; |
71 |
foreach $f (grep { !/^\./ } readdir(DIR)) { |
72 |
if (-d "$dir/$f" && $f !~ /^eng$/ && $f !~ /^adresar$/ && $f !~ /^eng_/) { |
73 |
$push_it = 1; |
74 |
push @dirs,"$dir/$f" if ($push_it); |
75 |
} |
76 |
elsif (-f "$dir/$f" && ! -l "$dir/$f") { |
77 |
@stat = stat(_); |
78 |
if ($f =~ /\.html/ && (time() - $stat[9]) < $mod_time) { |
79 |
print "filek za mjenjanje `$dir/$f\n" if ($verbose); |
80 |
push @files,"$dir/$f"; |
81 |
} |
82 |
else { |
83 |
print "ovo je staro: $dir/$f\n" if ($verbose > 1); |
84 |
} |
85 |
} |
86 |
elsif ($f =~ /^eng$/ || $f =~ /adresar/ || $f =~ /^eng_/) { |
87 |
print "preskacem jer je eng ili adresar $dir/$f\n" if ($verbose); |
88 |
} |
89 |
else { |
90 |
print "ovo je link $dir/$f\n" if ($verbose); |
91 |
} |
92 |
} |
93 |
closedir DIR; |
94 |
} |
95 |
|
96 |
foreach $file (@files) { |
97 |
if (open(IN,"$file")) { |
98 |
print "$file\n" if ($verbose); |
99 |
if (open(OUT,"> $file.$$")) { |
100 |
while(<IN>) { |
101 |
replace(); |
102 |
print OUT "$_"; |
103 |
} |
104 |
} else { |
105 |
warn "can't open $file.$$: $!"; |
106 |
} |
107 |
close(OUT); |
108 |
} else { |
109 |
warn "can't open $file for reading: $!"; |
110 |
} |
111 |
close(IN); |
112 |
@stat = stat($file); |
113 |
rename "$file.$$","$file"; |
114 |
chmod 0664,"$file"; |
115 |
# chmod $stat[2],"$file"; |
116 |
chown $stat[4],$stat[5],"$file"; |
117 |
} |
118 |
|
119 |
print STDERR "unlocking\n" if ($verbose); |
120 |
flock(LOCK,LOCK_UN); |
121 |
close(LOCK); |
122 |
unlink $lock; |
123 |
|
124 |
1; |
125 |
|
126 |
sub replace { |
127 |
do '/root/kvake/zamjene.pl'; |
128 |
} |