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