#!/usr/bin/perl
use Astaro::ConfdPlRPC;   
use Storable qw( retrieve ); 

if ( !defined($ARGV[0]) ) {
	print "fail: missing first parameter that must be a directory where to find RED exported RED objects!\n";
} else {
	my $dir = $ARGV[0];
	my $sys = new Astaro::ConfdPlRPC(); 
	$sys->connect();

	print "reading objects ...\n";

	opendir(DH,$dir);
	my @files = readdir(DH);
	closedir(DH);

	$sys->lock();
	$sys->lock_override(); 

	my $settings = $sys->get('settings');
	my $ca_ref = $sys->get('ca','ca_red');
	
	for my $file (@files) {
		if ($file =~ /^A(.*)$/) {
			my $obj = retrieve("$dir/$file");
			my $red_id = $obj->{data}->{'red_id'};
			if ("$file" != "$red_id") {
				next;
			}
			print "\t$red_id\n";

			my $host_cert = $sys->ca_generate_host_key_cert(
			{	'name' => "red_client ".$red_id,
				'ca_ref' => $ca_ref,
				'key_size' => 1024,
				'subject_alt_name' => 'IP:127.0.0.1',
				'country' => $settings->{'country'},
				'state' => 'XX',
				'city' => $settings->{'city'},
				'organization' => $settings->{'organization'},
				'organizational_unit' => "RED Hub",
				'common_name' => $red_id,
				'email' => 'red@red.red'
			});

			print "... failed creating host_cert\n" unless $host_cert;
			goto FAILED unless $host_cert;

			my $unlock_code;
			my $rc = $sys->red_register_red($red_id, $obj->{data}->{'unlock_code'}, $obj->{data}->{'deployment_mode'});
			if ($rc) {
				chomp($rc);
				if ($rc =~ /^ERR/) { 
					print "... error registering red $red_id\n";
					goto FAILED;
				} elsif ($rc =~ /^OK\:(.+)/) { 
					$unlock_code = $1;
				}
			}

			my $red_server = {
			  'class' => 'itfhw',
			  'type' => 'red_server',
			  'data' => {
                        	'pin' => $obj->{data}->{'pin'},
				'fullbr_dns' => $obj->{data}->{'fullbr_dns'},
                        	'split_networks' => $obj->{data}->{'split_networks'},
                	        'tunnel_id' => $obj->{data}->{'tunnel_id'},
                       	        'local_networks' => $obj->{data}->{'local_networks'},
                              	'manual2_netmask' => $obj->{data}->{'manual2_netmask'},
              	         	'manual_address' => $obj->{data}->{'manual_address'},
               		        'state' => $obj->{data}->{'state'},
                        	'unlock_code' => $unlock_code, 
                  	        'password' => $obj->{data}->{'password'},
               		        'manual2_defgw' => $obj->{data}->{'manual2_defgw'},
                        	'hardware' => $obj->{data}->{'hardware'},
                  	        'prev_unlock_code' => $obj->{data}->{'unlock_code'},
                 	        'manual_netmask' => $obj->{data}->{'manual_netmask'},
                         	'mac_filter_type' => $obj->{data}->{'mac_filter_type'},
                       		'mac' => $obj->{data}->{'mac'},
                        	'remote_cert' => $host_cert,
                        	'pin_as_string' => $obj->{data}->{'pin_as_string'},
                        	'dial_string' => $obj->{data}->{'dial_string'},
                        	'authorized' => $obj->{data}->{'authorized'},
                        	'manual2_address' => $obj->{data}->{'manual2_address'},
                        	'manual_dns' => $obj->{data}->{'manual_dns'},
                        	'name' => $obj->{data}->{'name'},
                        	'protocol_version' => $obj->{data}->{'protocol_version'},
                        	'description' => $obj->{data}->{'description'},
                        	'activate_modem' => $obj->{data}->{'activate_modem'},
                        	'username' => $obj->{data}->{'username'},
                        	'fullbr_domains' => $obj->{data}->{'fullbr_domains'},
                        	'uplink_balancing' => $obj->{data}->{'uplink_balancing'},
                        	'type' => $obj->{data}->{'type'},
                        	'deployment_mode' => $obj->{data}->{'deployment_mode'},
                        	'mac_filter_entries_red50' => $obj->{data}->{'mac_filter_entries_red50'},
                        	'uplink2_mode' => $obj->{data}->{'uplink2_mode'},
                        	'manual2_dns' => $obj->{data}->{'manual2_dns'},
                        	'status' => $obj->{data}->{'status'},
                        	'debug_level' => $obj->{data}->{'debug_level'},
                        	'local_networks_target' => $obj->{data}->{'local_networks_target'},
                        	'failover_direct' => $obj->{data}->{'failover_direct'},
                        	'red_id' => $obj->{data}->{'red_id'},
                        	'hub2_hostname' => $obj->{data}->{'hub2_hostname'},
                        	'umts_lock' => $obj->{data}->{'umts_lock'},
                        	'apn' => $obj->{data}->{'apn'},
                        	'mobile_network' => $obj->{data}->{'mobile_network'},
                        	'hub_hostname' => $obj->{data}->{'hub_hostname'},
                        	'mac_filter_entries_red10' => $obj->{data}->{'mac_filter_entries_red10'},
                        	'comment' => $obj->{data}->{'comment'},
                        	'hostname_balancing' => $obj->{data}->{'hostname_balancing'},
                        	'manual_defgw' => $obj->{data}->{'manual_defgw'},
                        	'mac_filter_list' => $obj->{data}->{'mac_filter_list'},
                        	'uplink_mode' => $obj->{data}->{'uplink_mode'},
                        	'route_mode' => $obj->{data}->{'route_mode'}
			  }
                        };

			my $ref = $sys->set_object($red_server) or goto FAILED;
			my $servers = $sys->get('red','servers');
			push @{ $servers }, $ref;
			$sys->set($servers,'red','servers') or goto FAILED;
		}
	}

	$sys->commit();
	goto OK;
FAILED:
	print "SCRIPT FAILED !!!\n";
OK:
	$sys->disconnect();
}
