<?php
	/**************************************************************************\
	* phpGroupWare - Experimental tools                                        *
	* http://www.phpgroupware.org                                              *
	* Written by Joseph Engo <jengo@phpgroupware.org>                          *
	* --------------------------------------------                             *
	*  This program is free software; you can redistribute it and/or modify it *
	*  under the terms of the GNU General Public License as published by the   *
	*  Free Software Foundation; either version 2 of the License, or (at your  *
	*  option) any later version.                                              *
	\**************************************************************************/

	/* $Id: create_ramdisk.php.txt 13520 2003-09-24 02:14:54Z skwashd $ */

	/* !!! WARNING !!!
	** This is highley experimental!  Do NOT run it unless you know what you are doing!
	** You can serious screaw things up!
	**
	** Requirements:
	**  - Must be run as root
	**  - You need to have RAM disk support complied into the kernel
	**  - You have to have the CGI binary for PHP
	**  - This ONLY works with Linux
	**  - The 2.2 kernel is limited to 20 RAM disks, so you will have to cut down on what you copy over
	**  - I wouldn't run this on a server with less then 196 MB of RAM.  If it has less, performance
	**    will only decrease.  Since proccess will need to swap out to disk.
	**
	** The phpGroupWare development team does not support this program in anyway.  If it breaks
	** or messes up your system, don't email us.  Don't submit bug reports.  If you do find ways to make
	** it better, please submit patches directly to me. jengo@phpgroupware.org
	*/

	$debug = True;

	// Locations of your permanent copy, you will need it to be setup already
	define('HARD_COPY','/home/jengo/public_html');
	// Where you want your install to be
	define('RAM_COPY','/var/www/html');

	function command($command)
	{
		global $debug;

		if ($debug)
		{
			echo $command . "\n";
		}
		else
		{
			system($command);
		}
	}

	command('mke2fs /dev/ram0 200');
	command('mkdir ' . RAM_COPY . '/phpgroupware');
	command('mount -t ext2 /dev/ram0 ' . RAM_COPY . '/phpgroupware');
	command('cp ' . HARD_COPY . '/phpgroupware/* ' . RAM_COPY . '/phpgroupware');

	$ram_drive_num = 1;
	$dh = opendir(HARD_COPY . '/phpgroupware/');
	while ($file = readdir($dh))
	{
		// The 2.2 kernel can only have 20 ram disks
		if ($ram_drive_num == 21)
		{
			exit;
		}

		if ($file != '.' && $file != '..' && $file != 'CVS' && $file != 'phpgroupware' && is_dir($file))
		{
			$_du_string = 'du -s ' . HARD_COPY . '/phpgroupware/' . $file;
			$_du        = `$_du_string`;;
			preg_match('/(\w+)\s/',$_du,$du);
			$du_size    = ereg_replace(' ','',$du[0]);

			// Make it slighty larger, so the files copy correctly
			$du_size    = $du_size + 400;

			command('mke2fs /dev/ram' . $ram_drive_num . ' ' . $du_size);
			command('mkdir ' . RAM_COPY . '/phpgroupware/' . $file);
			command('mount -t ext2 /dev/ram' . $ram_drive_num . ' ' . RAM_COPY . '/phpgroupware/' . $file);
			command('cp -R ' . HARD_COPY . '/phpgroupware/' . $file . ' ' . RAM_COPY . '/phpgroupware/' . $file);

			$ram_drive_num++;
			echo "\n";
		}
	}
