use FS::Record qw(qsearch qsearchs dbh);
 use FS::part_export;
 use FS::svc_phone;
+use FS::inventory_class;
+use FS::inventory_item;
 use IO::Socket::INET;
 use Data::Dumper;
+use MIME::Base64 qw(decode_base64);
+use Storable qw(thaw);
 
 use strict;
 
   'pwd'       => { label=>'Operator password' },
   'tplid'     => { label=>'Template number' },
   'hlrsn'     => { label=>'HLR serial number' },
+  'k4sno'     => { label=>'K4 serial number' },
+  'cardtype'  => { label  => 'Card type',
+                   type   => 'select', 
+                   options=> ['SIM', 'USIM']
+                 },
+  'alg'       => { label  => 'Authentication algorithm',
+                   type   => 'select',
+                   options=> ['COMP128_1',
+                              'COMP128_2',
+                              'COMP128_3',
+                              'MILENAGE' ],
+                 },
+  'opcvalue'  => { label=>'OPC value (for MILENAGE only)' },
+  'opsno'     => { label=>'OP serial number (for MILENAGE only)' },
   'timeout'   => { label=>'Timeout (seconds)', default => 120 },
   'debug'     => { label=>'Enable debugging', type=>'checkbox' },
 ;
 END
 );
 
+sub actions {
+  'Import SIMs' => 'misc/part_export/huawei_hlr-import_sim.html'
+}
+
 sub _export_insert {
   my( $self, $svc_phone ) = (shift, shift);
   # svc_phone::check should ensure phonenum and sim_imsi are numeric
   \%return;
 }
 
+sub process_import_sim {
+  my $job = shift;
+  my $param = thaw(decode_base64(shift));
+  $param->{'job'} = $job;
+  my $exportnum = delete $param->{'exportnum'};
+  my $export = __PACKAGE__->by_key($exportnum);
+  my $file = delete $param->{'uploaded_files'};
+  $file =~ s/^file://;
+  my $dir = $FS::UID::cache_dir .'/cache.'. $FS::UID::datasrc;
+  open( $param->{'filehandle'}, '<', "$dir/$file" )
+    or die "unable to open '$file'.\n";
+  my $error = $export->import_sim($param);
+}
+
+sub import_sim {
+  # import a SIM list
+  local $FS::UID::AutoCommit = 1; # yes, 1
+  my $self = shift;
+  my $param = shift;
+  my $job = $param->{'job'};
+  my $fh = $param->{'filehandle'};
+  my @lines = $fh->getlines;
+
+  my @command = 'ADD KI';
+  push @command, ('HLRSN', $self->option('hlrsn')) if $self->option('hlrsn');
+
+  my @args = ('OPERTYPE', 'ADD');
+  push @args, ('K4SNO', $self->option('k4sno')) if $self->option('k4sno');
+  push @args, ('CARDTYPE', $self->option('cardtype'),
+               'ALG',      $self->option('alg'));
+  push @args, ('OPCVALUE', $self->option('opcvalue'),
+               'OPSNO',    $self->option('opsno'))
+    if $self->option('alg') eq 'MILENAGE';
+
+  my $agentnum = $param->{'agentnum'};
+  my $classnum = $param->{'classnum'};
+  my $class = FS::inventory_class->by_key($classnum)
+    or die "bad inventory class $classnum\n";
+  my %existing = map { $_->item, 1 } 
+    qsearch('inventory_item', { 'classnum' => $classnum });
+
+  my $socket = $self->login;
+  my $num=0;
+  my $total = scalar(@lines);
+  foreach my $line (@lines) {
+    $num++;
+    $job->update_statustext(int(100*$num/$total).',Provisioning IMSIs...')
+      if $job;
+
+    chomp $line;
+    my ($imsi, $iccid, $pin1, $puk1, $pin2, $puk2, $acc, $ki) = 
+      split(' ', $line);
+    # the only fields we really care about are the IMSI and KI.
+    if ($imsi !~ /^\d{15}$/ or $ki !~ /^[0-9A-Z]{32}$/) {
+      warn "misspelled line in SIM file: $line\n";
+      next;
+    }
+    if ($existing{$imsi}) {
+      warn "IMSI $imsi already in inventory, skipped\n";
+      next;
+    }
+
+    # push IMSI/KI to the HLR
+    my $return = $self->command($socket,
+      @command,
+      'IMSI', $imsi,
+      'KIVALUE', $ki,
+      @args
+    );
+    if ( $return->{success} ) {
+      # add to inventory
+      my $item = FS::inventory_item->new({
+          'classnum'  => $classnum,
+          'agentnum'  => $agentnum,
+          'item'      => $imsi,
+      });
+      my $error = $item->insert;
+      if ( $error ) {
+        die "IMSI $imsi added to HLR, but not to inventory:\n$error\n";
+      }
+    } else {
+      die "IMSI $imsi could not be added to HLR:\n".$return->{error}."\n";
+    }
+  } #foreach $line
+  $self->logout($socket);
+  return;
+}
+
 1;
 
--- /dev/null
+<& /elements/header-popup.html, 'Import SIMs' &>
+Import a file containing SIM card properties.<BR>
+Each row should contain the following fields, separated by spaces:<BR>
+IMSI, ICCID, PIN1, PUK1, PIN2, PUK2, ACC, Ki<BR>
+<BR>
+<& /elements/form-file_upload.html,
+     'name'      => 'ImportForm',
+     'action'    => 'process/huawei_hlr-import_sim.html',
+     'num_files' => 1,
+     'fields'    => [ 'exportnum', 'classnum', 'agentnum', ],
+     'message'   => 'Inventory import successful',
+     'onsubmit'  => "document.ImportForm.submitButton.disabled=true;",
+&>
+<TABLE CLASS="inv" WIDTH="100%">
+  <INPUT TYPE="hidden" NAME="exportnum" VALUE="<%$exportnum%>">
+  <& /elements/file-upload.html,
+    'field' => 'file',
+    'label' => 'Filename',
+  &>
+  <& /elements/tr-select-agent.html,
+    'disable_empty' => 1,
+  &>
+  <& /elements/tr-select-table.html,
+    'table'     => 'inventory_class',
+    'name_col'  => 'classname',
+    'label'     => 'Inventory class',
+    'disable_empty' => 1,
+  &>
+
+  <TR>
+    <TD COLSPAN=2 ALIGN="center" STYLE="padding-top:6px">
+      <INPUT TYPE  = "submit"
+             NAME  = "submitButton"
+             ID    = "submitButton"
+             VALUE = "Import file"
+      >
+    </TD>
+  </TR>
+
+</TABLE>
+
+</FORM>
+
+<%init>
+die "access denied"
+  unless $FS::CurrentUser::CurrentUser->access_right('Configuration');
+
+my ($exportnum) = $cgi->keywords;
+$exportnum =~ /^\d+$/ or die "bad exportnum '$exportnum'";
+my $part_export = FS::part_export->by_key($exportnum)
+  or die "export $exportnum not found";
+</%init>