updating apt repo's now work.... apt really annoys
[glcas.git] / bin / updateaptrepo.php
1 <?php
2
3 $url = "";
4 $file = "";
5 if(isset($argv[1])) {
6         $url = $argv[1];
7 }
8
9 if(isset($argv[2])) {
10         $file = $argv[2];
11 }
12
13 if($url == "" || $file == "") {
14         echo "need a url and file\n";
15         exit(0);
16 }
17
18 if(file_exists("$file/update.lock")) {
19         // try get a lock
20         $lf = fopen("$file/update.lock", "r");
21         if($lf) {
22                 $lockres = flock($lf, LOCK_EX|LOCK_NB);
23                 if(!$lockres) {
24                         error_log("flock did fail, all is right with the world a download is in progress");
25                         exit(0);
26                 }
27         } else {
28                 error_log("weird problem");
29         }
30         fclose($lf);
31 }
32 $lf = fopen("$file/update.lock", "w");
33 flock($lf, LOCK_EX|LOCK_NB);
34
35 // next we go into $file/dists and open each dir
36 // download "release"
37 // then check every file in release against what we have... this is giong to be a nightmare
38 $dh = opendir("$file/dists/");
39 while(($fn = readdir($dh))!==false) {
40         if($fn != "." && $fn != "..") {
41                 echo "checkin dist, $fn with $url\n";
42                 $releasefile = file_get_contents("$url/dists/$fn/Release");
43                 file_put_contents("$file/dists/$fn/Release.new", $releasefile);
44                 if(file_exists("$file/dists/$fn/Release")) {
45                         unlink("$file/dists/$fn/Release");
46                         rename("$file/dists/$fn/Release.new", "$file/dists/$fn/Release");
47                 } else {
48                         rename("$file/dists/$fn/Release.new", "$file/dists/$fn/Release");
49                 }
50                 
51                 echo "got release file\n";
52                 
53                 // now open that release file and check all our ... versions and stuff, this bit sucks, i hate parsing text files
54                 $relfile = fopen("$file/dists/$fn/Release", "r");
55                 $continue = true;
56                 $chkfile = false;
57                 if($relfile) while($continue) {
58                         $line = fgets($relfile);
59                         if(feof($relfile)) $continue = false;
60                         // first try to parse a tag
61                         $tags = explode(":", $line);
62                         // check for a tag we're into
63                         if(strcasecmp($tags[0], "md5sum") == 0) {
64                                 $chkfile = true;
65                                 $chktype = "md5";
66                                                         
67                         } else if(strcasecmp($tags[0], "SHA1") == 0) {
68                                 $chkfile = true;
69                                 $chktype = "sha1";
70                                 
71                         } else if(strcasecmp($tags[0], "SHA256") == 0) {
72                                 $chkfile = true;
73                                 $chktype = "sha256";
74                                 
75                                 
76                         } else if($chkfile) {
77                                 //echo "in chkfile on $line\n";
78                                 $line = trim($line);
79                                 $lspl = preg_split("/ +/", $line);
80                                 if(isset($lspl[2])) {
81                                         $sum = $lspl[0];
82                                         $size = ((int)($lspl[1]));
83                                         $distrofile = $lspl[2];
84                                         if(file_exists("$file/dists/$fn/$distrofile")) {
85                                                 $updatefile = false;
86                                                 $mysum = hash_file($chktype, "$file/dists/$fn/$distrofile");
87                                                 $mysize = filesize("$file/dists/$fn/$distrofile");
88                                                 //echo "would check $sum, $size, $distrofile against $distrofile/dists/$fn, $sum\n";
89                                                 if(strtolower($mysum) == strtolower($sum)) {
90                                                         echo "file $distrofile is clean for sum\n";
91                                                 } else {
92                                                         echo "File $distrofile needs updating with $sum, $mysum, $chktype, $url\n";
93                                                         $updatefile = true;
94                                                 }
95                                                 
96                                                 if($size != $mysize) {
97                                                         echo "File $distrofile needs updating with $size, $mysize, $url\n";
98                                                         $updatefile = true;
99                                                 } else {
100                                                         echo "file $distrofile is clean for size\n";
101                                                 }
102                                                 if($updatefile) {
103                                                         $getthisfile = file_get_contents("$url/dists/$fn/$distrofile");
104                                                         unlink("$file/dists/$fn/$distrofile");
105                                                         file_put_contents("$file/dists/$fn/$distrofile", $getthisfile);
106                                                         echo "Updated file\n";
107                                                 }
108                                         }
109                                 } else {
110                                         $chkfile = false;
111                                 }
112                         }
113                 }
114         }
115 }
116 fclose($lf);
117 unlink("$file/update.lock");
118 echo "im at an end\n";
119
120 ?>