#!/usr/bin/php -q
<?php
#     Copyright (C) 2005 Stewart Adam
#    This file is part of FWBackups.

#    FWBackups 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.

#    FWBackups is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    You should have received a copy of the GNU General Public License
#    along with FWBackups; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

# Perform a backup, run by cron on a nightly basis.
function cloneing()
{
`zenity --info --text "YUM INSTALL HOWTO:
Once you have the RPMS_for_yum.txt file ready change the active directory to where this file is found.
Then, execute this command as root: (After installing the repo's.)
yum install \`echo RPMS_for_yum.txt\`
This will clone your packages installed."`;
}
function prefs()
{
	# Crontab Hour
	$crontab = `cat /etc/crontab | grep -v "/etc/cron.backup"`;
	`rm /etc/crontab`;
	`echo "$crontab" > /etc/crontab`;
	$hour = `zenity --list --radiolist --text "Please enter an hour to have your backup run at: (Scroll down for more hours)" --column "" --column "Hour:" TRUE "0" "12am" FALSE "1" "1am" FALSE "2" "2am" FALSE "3" "3am" FALSE "4" "4am" FALSE "5" "5am" FALSE "6" "6am" FALSE "7" "7am" FALSE "8" "8am" FALSE "9" "9am" FALSE "10" "10am" FALSE "11" "11am" FALSE "12" "12pm" FALSE "13" "1pm" FALSE "14" "2pm" FALSE "15" "3pm" FALSE "16" "4pm" FALSE "17" "5pm" FALSE "18" "6pm" FALSE "19" "7pm" FALSE "20" "8pm" FALSE "21" "9pm" FALSE "22" "10pm" FALSE "23" "11pm" --column "12 Clock Hour" --height=400`;
	$hour = trim("$hour");
	`echo "0 $hour * * * root run-parts /etc/cron.backup" >> /etc/crontab`;
	if(file_exists('/etc/backup.conf.d/dir.conf'))
	{
	$dirmade = `cat /etc/backup.conf.d/dir.conf`;
	$dirmade = trim($dirmade);
	}
	else
	{
	$dirmade = "";
	}
	$dir = `zenity --entry --entry-text "$dirmade" --text 'Please enter the destination directory to backup to: (Make sure it exists)'`;
	$dir = trim("$dir");
	if(!is_dir("$dir"))
	{$check = "1";
	while( $check == "1")
	{
		$dir = `zenity --entry --text 'Invalid Entry. Please enter a valid destination directory:
(Make sure the drive is mounted beforehand if needed, and that the directory exists.)'`;
		$dir = trim("$dir");
		if(is_dir("$dir"))
		{
			$check = "0";
		}
	}}
		
	$drive = `mount | awk '{printf \$1" "\$3 "\\n"}' | grep "$dir" | awk '{printf \$1}'`;
	$drive = trim("$drive");
	if($drive != "")
	{
		`zenity --info --text "Drive $drive was found."`;
	}
	else
	{
		`/sbin/fdisk -l | less`;
	if(file_exists('/etc/backup.conf.d/drive.conf'))
	{
	$drivemade = `cat /etc/backup.conf.d/drive.conf`;
	$drivemade = trim($drivemade);
	}
	else
	{
	$drivemade = "";
	}
		$drive = `zenity --entry --entry-text "$drivemade" --text "Drive not found automatically. Please enter manually:"`;
		if($drive == "")
		{
			$drive = `zenity --entry --entry-text "$drivemade" --text "You did not type a drive. This is your last chance to enter a drive:"`;
		}
	}
`echo "# This file is automatically generated with the backupconfig command." > /etc/backup.conf.d/paths.conf`;
`echo "$dir" > /etc/backup.conf.d/dir.conf`;
`echo "$drive" > /etc/backup.conf.d/drive.conf`;
}

function addpaths()
{	
	$path = `zenity --entry --text "Enter path that you wish to backup; It can either be a file or a directory."`;
	$path = trim("$path");
	if(!file_exists("$path"))
	{
		$check = "1";
		while($check == "1")
		{
			if($path == "")
			{break;}
			$path = `zenity --entry --text "You entered a path that did not exist. Please enter a correct path. Make sure the drive is mounted."`;
			$path = trim("$path");
			if(file_exists("$path"))
			{
				$check = "0";
			}
		}
	}
	$path = addslashes($path);
	`echo "$path" >> /etc/backup.conf.d/paths.conf`;
}

function rmpaths()
{
	if(!file_exists('/etc/backup.conf.d/paths.conf'))
	{
		addpaths();
	}
	$file = file('/etc/backup.conf.d/paths.conf');
	$cmd = "zenity --list --list-text \"Choose line(s) to remove:\" --checklist --column \"\" --column 'Line' --list-separator=\";\" --width=\"400\" --height=\"200\"";
	foreach ($file as $key => $value)
	{
		if($key != "0")
		{$cmd = "$cmd \"\" \"{$value}\"";}
	}
	$result = `$cmd`;
	$result = trim("$result");
	if($result != "")
	{
	$array = explode(";",$result);
	$remove = "";
	$counter = "0";
	foreach($array as $key => $value)
	{##
		$value = eregi_replace(' ','\ ',$value);
		$remove[] = "$value";
		$counter++;
	}##
	$sure = `zenity --question --text "Are you sure you want to remove $counter lines?";echo $?`;
	$sure = trim($sure);
	if($sure == "0")
	{
		foreach($remove as $key => $value)
		{
			$cron = "";
			$value = trim("$value");
			$cron = `cat /etc/backup.conf.d/paths.conf | grep -v "$value"`;
			`rm /etc/backup.conf.d/paths.conf`;
			`echo -n "$cron" >  /etc/backup.conf.d/paths.conf`;
		}
	}}
}

function paths()
{
$cmd = "zenity --list --list-text \"Choose a line to edit:\" --radiolist --column \"\" --column 'Line' --width=\"400\" --height=\"200\"";
$file = file('/etc/backup.conf.d/paths.conf');
	foreach ($file as $key => $value)
	{
		if($key != "0")
		{$cmd = "$cmd \"\" \"{$value}\"";}
	}
$result = `$cmd`;
if($result != "")
{
$result = trim("$result");
$else = `cat /etc/backup.conf.d/paths.conf | grep -v "$result"`;
$else = trim("$else");
$change = `zenity --entry --text "Change line to:" --entry-text "$result"`;
if($change != "")
{
$change = trim("$change");
`rm /etc/backup.conf.d/paths.conf`;
`echo "# This file is automatically generated with the backupconfig command." > /etc/backup.conf.d/paths.conf`;
`echo "$else" >> /etc/backup.conf.d/paths.conf`;
`echo -n $change >> /etc/backup.conf.d/paths.conf`;
}
}
}

############################################################################################################################
$dir = "";
$drive = "";

$user = `echo \$USER`;
$user = trim("$user");
if($user != "root")
{
echo "Permission denied. Make sure you are root.\n";
exit();
}

if(file_exists('/etc/backup.conf.d'))
{
	#### EXISTS
	echo "/etc/backup.conf.d/ exists. OK!\n";
}
else
{
	#### NOT EXISTING
	echo "/etc/backup.conf.d/ does not exist. Creating...\n";
	`mkdir /etc/backup.conf.d`;
	`zenity --info --text "We will now setup preferences. These setting can later be changed
by selection 'Edit preferences' when running the 'backupconf' tool."`;
	prefs();
}

	$selection = `zenity --list --radiolist --text "Choose an action:" --column "" --column "Action #" --column "Description:" FALSE 1 "Edit preferences" TRUE 2 "Edit current backup entries" FALSE 3 "Add a new backup entry" FALSE 4 "Delete a backup entry" FALSE 5 "View Yum Install Howto (Cloning packages)" FALSE 6 Manually Edit Files --width="400" --height="250"`;
	$selection = trim("$selection");
switch($selection)
{
	case "1" :
		prefs();
		break;
	case "2" :
		paths();
		break;
        case "3" :
		addpaths();
		break;
	case "4" :
		rmpaths();
		break;
	case "5" :
		cloneing();
		break;
	case "6" :
		`gedit /etc/backup.conf.d/paths.conf /etc/backup.conf.d/drive.conf /etc/backup.conf.d/dir.conf /etc/cron.backup/backup.sh`;
		break;
	default :
		exit();
		break;
}
`/usr/bin/backupconfig`;
?>
