|
| 1 | +#!/usr/bin/php |
| 2 | +<?php # $Id$ |
| 3 | +/* piece together a windows pear distro */ |
| 4 | + |
| 5 | +if (!$argv[1] || !$argv[2]) { |
| 6 | + echo "Usage: {$argv[0]} dist_dir src_dir\n"; |
| 7 | + exit(1); |
| 8 | +} |
| 9 | + |
| 10 | +$dist_dir = $argv[1]; |
| 11 | +$cvs_dir = $argv[2]; |
| 12 | + |
| 13 | +/* very light-weight function to extract a single named file from |
| 14 | + * a gzipped tarball. This makes assumptions about the files |
| 15 | + * based on the PEAR info set in $packages. */ |
| 16 | +function extract_file_from_tarball($pkg, $filename, $dest_dir) /* {{{ */ |
| 17 | +{ |
| 18 | + global $packages; |
| 19 | + |
| 20 | + $name = $pkg . '-' . $packages[$pkg]; |
| 21 | + $tarball = $dest_dir . "/" . $name . '.tgz'; |
| 22 | + $filename = $name . '/' . $filename; |
| 23 | + $destfilename = $dest_dir . "/" . basename($filename); |
| 24 | + |
| 25 | + $fp = gzopen($tarball, 'rb'); |
| 26 | + |
| 27 | + $done = false; |
| 28 | + do { |
| 29 | + /* read the header */ |
| 30 | + $hdr_data = gzread($fp, 512); |
| 31 | + if (strlen($hdr_data) == 0) |
| 32 | + break; |
| 33 | + $checksum = 0; |
| 34 | + for ($i = 0; $i < 148; $i++) |
| 35 | + $checksum += ord($hdr_data{$i}); |
| 36 | + for ($i = 148; $i < 156; $i++) |
| 37 | + $checksum += 32; |
| 38 | + for ($i = 156; $i < 512; $i++) |
| 39 | + $checksum += ord($hdr_data{$i}); |
| 40 | + |
| 41 | + $hdr = unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/a1typeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", $hdr_data); |
| 42 | + |
| 43 | + $hdr['checksum'] = octdec(trim($hdr['checksum'])); |
| 44 | + |
| 45 | + if ($hdr['checksum'] != $checksum) { |
| 46 | + echo "Checksum for $tarball $hdr[filename] is invalid\n"; |
| 47 | + print_r($hdr); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + $hdr['size'] = octdec(trim($hdr['size'])); |
| 52 | + echo "File: $hdr[filename] $hdr[size]\n"; |
| 53 | + |
| 54 | + if ($filename == $hdr['filename']) { |
| 55 | + echo "Found the file we want\n"; |
| 56 | + $dest = fopen($destfilename, 'wb'); |
| 57 | + $x = stream_copy_to_stream($fp, $dest, $hdr['size']); |
| 58 | + fclose($dest); |
| 59 | + echo "Wrote $x bytes into $destfilename\n"; |
| 60 | + break; |
| 61 | + } |
| 62 | + |
| 63 | + /* skip body of the file */ |
| 64 | + $size = 512 * ceil((int)$hdr['size'] / 512); |
| 65 | + echo "Skipping $size bytes\n"; |
| 66 | + gzseek($fp, gztell($fp) + $size); |
| 67 | + |
| 68 | + } while (!$done); |
| 69 | + |
| 70 | +} /* }}} */ |
| 71 | + |
| 72 | +echo "Creating PEAR in $dist_dir\n"; |
| 73 | + |
| 74 | +/* Let's do a PEAR-less pear setup */ |
| 75 | +if (!file_exists($dist_dir)) { |
| 76 | + mkdir($dist_dir); |
| 77 | +} |
| 78 | +if (!file_exists($dist_dir)) { |
| 79 | + die("could not make $dist_dir"); |
| 80 | +} |
| 81 | +mkdir("$dist_dir/PEAR"); |
| 82 | +mkdir("$dist_dir/PEAR/go-pear-bundle"); |
| 83 | + |
| 84 | +/* grab the bootstrap script */ |
| 85 | +echo "Downloading go-pear\n"; |
| 86 | +copy("http://go-pear.org/", "$dist_dir/PEAR/go-pear.php"); |
| 87 | +echo "Downloading go-pear.bat\n"; |
| 88 | +copy("$cvs_dir/pear/go-pear.bat", "$dist_dir/go-pear.bat"); |
| 89 | + |
| 90 | +/* import the package list -- sets $packages variable */ |
| 91 | +include $cvs_dir . "/pear/go-pear-list.php"; |
| 92 | + |
| 93 | +/* download the packages into the destination */ |
| 94 | +echo "Fetching packages\n"; |
| 95 | + |
| 96 | +foreach ($packages as $name => $version) { |
| 97 | + $filename = "$name-$version.tgz"; |
| 98 | + $destfilename = "$dist_dir/PEAR/go-pear-bundle/$filename"; |
| 99 | + if (file_exists($destfilename)) |
| 100 | + continue; |
| 101 | + $url = "http://pear.php.net/get/$filename"; |
| 102 | + echo "Downloading $name from $url\n"; |
| 103 | + flush(); |
| 104 | + copy($url, $destfilename); |
| 105 | +} |
| 106 | + |
| 107 | +echo "Download complete. Extracting bootstrap files\n"; |
| 108 | + |
| 109 | +/* Now, we want PEAR.php, Getopt.php (Console_Getopt) and Tar.php (Archive_Tar) |
| 110 | + * broken out of the tarballs */ |
| 111 | +extract_file_from_tarball('PEAR', 'PEAR.php', "$dist_dir/PEAR/go-pear-bundle"); |
| 112 | +extract_file_from_tarball('Archive_Tar', 'Archive/Tar.php', "$dist_dir/PEAR/go-pear-bundle"); |
| 113 | +extract_file_from_tarball('Console_Getopt', 'Console/Getopt.php', "$dist_dir/PEAR/go-pear-bundle"); |
| 114 | +?> |
0 commit comments