#! /usr/bin/python2.5 -OOt
# -*- python -*-
# -*- coding: UTF-8 -*-
#   TimeVault - automated file backup and restore
#   Copyright (C) 2007 A. Bashi <sourcecontact@gmail.com>
#
#   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.
#
#   This program 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 this program; if not, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

import os, os.path
import sys
import random
import time

from TimeVault import dbusclient
from TimeVault.base import *

def PickOne(L):
	return L[random.randint(0, len(L)-1)]

def Spider(path, depth, filelist):
	if depth<0:
		return
	
	for name in os.listdir(path):
		file = os.path.join(path, name)
		if not os.path.isdir(file):
			filelist.append(file)
		else:
			Spider(file, depth-1, filelist)

# Only root
if os.geteuid()!=0:
	print('This program must be run as root')
	sys.exit(-1)

commands = ParseCommandLineOptions()
path = commands.get('path', '/home')
start = int(time.time() + commands.get('start', -30)*3600*24)
stop = int(time.time() + commands.get('stop', 0)*3600*24)
number = commands.get('number', 1000)
depth = 1
eventlist = 'CNDMMMMM'

files = []
Spider(path, depth, files)

dbclient = dbusclient.DBusClient()

snaps = []
while number>0:
	for i in range(random.randint(0, 10)):
		tm = random.randint(start, stop)
		tm += random.randint(0, 3600)
		for j in range(random.randint(0, 50)):
			event = PickOne(eventlist)
			file = PickOne(files)

			Debug(D_NORM, "Generating %s event on %s at %s\n" % (EVENTNAME[event], file, time.strftime("%X %x", time.localtime(tm))))
			snaps.append((file, event, tm))
			number -= 1

		dbclient.interface.ForceSnapshot(snaps)
		time.sleep(1.0)
