package MDV::Snapshot::Hal;

################################################################################
# Backup Configuration Tool                                                    #
#                                                                              #
# Copyright (C) 2008 Mandriva                                                  #
#                                                                              #
# Thierry Vignaud <tvignaud at mandriva dot com>                               #
#                                                                              #
# This program is free software; you can redistribute it and/or modify         #
# it under the terms of the GNU General Public License Version 2 as            #
# published by the Free Software Foundation.                                   #
#                                                                              #
# 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.   #
################################################################################


use strict;
use warnings;
use lib qw(/usr/lib/libDrakX);
use MDK::Common;
use dbus_object;

use Exporter;
our @ISA = qw(Exporter);
our @EXPORT = qw($hal_dn $hal_manager $manager_path find_removable_volumes get_system_bus is_proper_device);

our $hal_dn = 'org.freedesktop.UDisks2';
our $hal_manager = 'org.freedesktop.DBus.ObjectManager';
our $manager_path = '/org/freedesktop/UDisks2';
our $dbus_object_manager ='org.freedesktop.DBus.ObjectManager';
our $block_devices='org.freedesktop.UDisks2.Block';
our $path_to_drives='/org/freedesktop/UDisks2/drives';
our $path_to_drive_prop='org.freedesktop.UDisks2.Drive';
our $path_to_block_devices='/org/freedesktop/UDisks2/block_devices';
our $path_to_block_prop_part='org.freedesktop.UDisks2.Partition';
our $path_to_block_prop_file='org.freedesktop.UDisks2.Filesystem';
our $path_to_block_prop_block='org.freedesktop.UDisks2.Block';

sub get_system_bus() {
    eval { dbus_object::system_bus() };
}


sub is_proper_device {
    my ($device, $o_is_first_check) = @_;  # perl_checker: $device = Net::DBus::RemoteObject->new
    #my $device_name = $device->QueryCapability('block') && $device->GetProperty('block.device');
    #return if !$device_name;
    my $bool=0;
    my @is_mounted;
    if ( grep /[0-9]$/, $device ) {

   my ($dbus) = Net::DBus->system;
   my $service = $dbus->get_service($hal_dn);
    my $block_device_properties_object= $service->get_object($device,'org.freedesktop.DBus.Properties');
    my $is_read_only=$block_device_properties_object->Get($path_to_block_prop_block,"ReadOnly");
    print("Read only status of $device is :$is_read_only \n");
    if ( $is_read_only == 0) { # device is not readonly
    my $is_zero_size=$block_device_properties_object->Get($path_to_block_prop_block,"Size");
    print("Size of $device is : $is_zero_size \n");
    if ( $is_zero_size > 0){
        @is_mounted=$block_device_properties_object->Get($path_to_block_prop_file,"MountPoints");
    if (@is_mounted != []){
    print("Block Device $device is proper for backups \n");
      $bool = 1; }
      else
      {
      print("Block Device $device is NOT proper for backups \n");
      $bool = 0;}
      }
      }
      }

          #return $o_is_first_check ? $bool && $device_name && cat_('/media/.hal-mtab') =~ m!^$device_name\s!sg : $bool;
    #print("Value returned by is_proper_device: $bool \n");
    return $bool && @is_mounted;
}

sub find_removable_volumes {
    #my ($dbus) = Net::DBus->system;
    my ($dbus) = @_; # perl_checker: $dbus = Net::DBus->new
    my $service = $dbus->get_service($hal_dn); # perl_checker: $dbus = Net::DBus->new
    my $manager = $service->get_object($manager_path,$hal_manager); # perl_checker: $manager = Net::DBus::RemoteObject
    my @eligible_drives;#To fill a list of drive that have the property MediaRemovable
    my @eligible_block_devices;
    my @drives= (grep /\/org\/freedesktop\/UDisks2\/drives\//,%{$manager->GetManagedObjects}); # To get only the list of present drives on computer whatever they are
    my @block_devices=(grep /\/org\/freedesktop\/UDisks2\/block_devices\//,%{$manager->GetManagedObjects}); # To get the block_devices list on system to check against the drives
    for(my $k=0;$k < scalar @drives;$k= $k+1){
    print ("Drive: $drives[$k]\n");
    }

    for(my $i=0;$i < scalar @block_devices;$i= $i+1){
    print ("block_device: $block_devices[$i]\n");
    }
    for (my $i=0;$i < scalar @drives;$i= $i+1){
    my $drives_properties_object= $service->get_object($drives[$i],'org.freedesktop.DBus.Properties');
    my $is_removable=$drives_properties_object->Get($path_to_drive_prop,"MediaRemovable");# testing is drive is removable
    if ($is_removable) {
       push(@eligible_drives,$drives[$i]); # drive is removable; adding it to list to test block_device against
       print("Removable device: $drives[$i] added \n");
    }
    for (my $j=0;$j < scalar @block_devices;$j= $j+1){
    my $block_device_properties_object= $service->get_object($block_devices[$j],'org.freedesktop.DBus.Properties');
    my $get_drive_from_block_device=$block_device_properties_object->Get($path_to_block_prop_block,"Drive");
     if (grep /^$get_drive_from_block_device$/,@eligible_drives) {
     print("testing:$block_devices[$j], from $get_drive_from_block_device\n");
     push(@eligible_block_devices,$block_devices[$j]);
     is_proper_device($block_devices[$j], 1);
     }
    }
   }
  #grep { is_proper_device($_, 1) } map { $service->get_object($_, "$block_devices") } @eligible_block_devices;
  #grep { is_proper_device($_, 1) } map { $service->get_object($_, "$block_devices") } @{$manager->GetManagedObjects};
 }

#my $dbus_test=get_system_bus;
#$dbus_test.find_removable_volumes;

1;
