#!/usr/bin/perl
############################################################################################
#Open Installer v0.2 http://www.openinstaller.com/
#
#See http://www.openinstaller.com/ for the latest updates to this installer.
############################################################################################
#
#Copyright (c) 2004 cPanel, Inc.
#Copyright (c) 2004 Lifeless People Networks.
#
#This auto install script, "install" is hereinafter referred to as the "Software."  No other parts of this package
#will be considered, infered or implied to be part of the the "Software."
#
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
#documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
#rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
#permit persons to whom the Software is furnished to do so, subject to the following conditions:
#
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
#Software.
#
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
#WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
#COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
#OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#

$|=1;

#app specific config
my $tarball = 'http://voxel.dl.sourceforge.net/sourceforge/phpbb/phpBB-2.0.10.tar.gz';
my $md5sum = 'b902bf48fa371b7dca9a4f13ca176652';
my $app = 'phpBB';
#end app specific config

print "Installing ${app} Open Installer...";
if ($pid = fork()) {
	while(waitpid($pid,1) == 0) {
		print ".";
		sleep(1);
	}
} else {
	removefiles("${app}-*.tar.gz");
	my $file = getfilename($tarball);
	fetchfile($tarball,$file);
	if (getmd5sum($file) ne $md5sum) {
		print "We're sorry but, the MD5 hashes do not match. See http://www.openinstaller.com/forum/ for details.\n";
		removefiles("${app}-*.tar.gz");
		exit();
	}
	removefiles("pkg.tar.gz*");
	system("tar","zxf","$file");
	
	#app specific stuff starts here
	system("mv","-f","phpBB2","pkg");
	system("tar","cfz","pkg.tar.gz","pkg/");
	system("rm","-rf","pkg");
	#app specific stuff ends here

	removefiles("${app}-*.tar.gz");
	exit();
}
print "Complete.\n";
exit();

sub getmd5sum {
   my($file) = @_;
   my($md5);
                                                                                                                  
   if (-e "/bin/md5sum" || -e "/usr/bin/md5sum" || -e "/usr/local/bin/md5sum") {
      open(MD5,"-|") || exec 'md5sum',$file;
   } else {
      open(MD5,"-|") || exec 'md5',"-r",$file;
   }
   while(<MD5>) {
      chomp();
      ($md5,undef) = split(/\s+/, $_);
      last();
   }
   close(MD5);
   return($md5);
}
                                                                                                                  

sub removefiles {
	my($pat) = @_;
	my(@FILES) = glob($pat);
	foreach my $file (@FILES) {
		unlink($file);
	}
}

sub fetchfile {
	my($file) = @_;
        if (-e "/usr/bin/wget" || -e "/usr/local/bin/wget") {
                system("wget","-qq","$file");
        } elsif (-e "/usr/bin/fetch" || -e "/usr/local/bin/fetch" || -e "/bin/fetch") {
                system("fetch","$file");
        }
}

sub getfilename {
	my($url) = @_;
	my (@F) = split(/\//, $url);
	return($F[$#F]);
}
