--- /dev/null
+#!/usr/bin/perl
+
+use warnings;
+use strict;
+use Text::CSV qw/ csv /;
+use DBI;
+
+# take a SIP2 account username and an org_unit shortname and generate
+# a four-digit PIN, insert the user and address, then generate copy-
+# and-pastable XML to add to the oils_sip.xml file.
+
+sub usage {
+ print "$0 <account-username> <library-shortname>\nOR: $0 <file-with-account-and-usernames>\n";
+ exit;
+}
+
+my ($username, $org_unit, $file);
+my $arg_count = @ARGV;
+
+if ( $arg_count == 2 ) {
+ # assume we're adding a single account
+ ($username, $org_unit) = @ARGV;
+} else {
+ $file = $ARGV[0];
+}
+
+
+unless (($username && $org_unit) || $file) {
+ usage;
+}
+
+# set up the DB connection
+my $dbh = DBI->connect
+(
+ "dbi:Pg:service=evergreen",
+ undef,
+ undef,
+ {
+ AutoCommit => 0,
+ RaiseError => 1,
+ PrintError => 0
+ }
+) or die DBI->errstr;
+
+# check that we don't already have a user with this barcode/usrname
+my $barcode_check_sql = qq(
+select 1
+from actor.card
+where barcode = ?
+);
+
+my $username_check_sql = qq(
+select 1
+from actor.usr
+where usrname = ?
+);
+
+my $org_unit_sql = qq(
+select ou.id,
+ addr.street1,
+ addr.street2,
+ addr.city,
+ addr.county,
+ addr.state,
+ addr.country,
+ addr.post_code
+from actor.org_unit ou
+join actor.org_address addr on (ou.mailing_address = addr.id)
+where ou.shortname = ?
+);
+
+my $insert_user_sql = qq(
+insert into actor.usr (
+ profile,
+ usrname,
+ passwd,
+ ident_type,
+ ident_value,
+ first_given_name,
+ family_name,
+ home_ou,
+ dob
+) values (
+ 36,
+ ?,
+ ?,
+ 3,
+ 'na',
+ ?,
+ 'SIP',
+ ?,
+ '1970-01-01'
+));
+
+my $insert_address_sql = qq(
+insert into actor.usr_address (
+ usr,
+ street1,
+ street2,
+ city,
+ county,
+ state,
+ country,
+ post_code
+) values (
+ (select id from actor.usr where usrname = ?),
+ ?,
+ ?,
+ ?,
+ ?,
+ ?,
+ 'USA',
+ ?
+));
+
+my $link_addr_sql = qq(
+update actor.usr
+set mailing_address = (
+ select id
+ from actor.usr_address
+ where usr in (
+ select id
+ from actor.usr
+ where usrname = ?
+ )
+)
+where usrname = ?
+);
+
+my $insert_card_sql = qq(
+insert into actor.card (
+ usr,
+ barcode
+) values(
+ (select id from actor.usr where usrname = ?),
+ ?
+));
+
+my $link_card_sql = qq(
+update actor.usr
+set card = (
+ select id
+ from actor.card
+ where barcode = ?
+)
+where usrname = ?
+and not deleted
+);
+
+sub generate_password {
+ my @nums = map { int(rand(10)) } (1..4);
+ my $pin = join( "", @nums);
+ return $pin;
+}
+
+my $sip_accounts = csv (in => "$file");
+my @completed_accounts;
+foreach my $sip_acct (@$sip_accounts) {
+ my $username = @$sip_acct[0];
+ my $org_unit = @$sip_acct[1];
+ my $barcode = $username;
+ my $password = generate_password();
+ # check that we don't already have a user with this username or barcode
+ my $sth = $dbh->prepare($barcode_check_sql);
+ $sth->execute($barcode);
+ my @barcode_check = $sth->fetchrow_array;
+ my $already_have_barcode = $barcode_check[0];
+ $sth = $dbh->prepare($username_check_sql);
+ $sth->execute($username);
+ my @username_check = $sth->fetchrow_array;
+ my $already_have_username = $username_check[0];
+ unless ($already_have_username || $already_have_barcode) {
+ # gather the rest of the data we need
+ $sth = $dbh->prepare($org_unit_sql);
+ $sth->execute($org_unit);
+ my @address_data = $sth->fetchrow_array;
+ my $ou_id = $address_data[0];
+ my $street1 = $address_data[1];
+ my $street2 = $address_data[2];
+ my $city = $address_data[3];
+ my $county = $address_data[4];
+ my $state = $address_data[5];
+ my $country = $address_data[6];
+ my $post_code = $address_data[7];
+ my $first_name = $username;
+ # do the import
+ $sth = $dbh->prepare($insert_user_sql);
+ $sth->execute($username, $password, $first_name, $ou_id);
+ $sth = $dbh->prepare($insert_address_sql);
+ $sth->execute($username, $street1, $street2, $city, $county, $state, $post_code);
+ $sth = $dbh->prepare($link_addr_sql);
+ $sth->execute($username, $username);
+ $sth = $dbh->prepare($insert_card_sql);
+ $sth->execute($username, $barcode);
+ $sth = $dbh->prepare($link_card_sql);
+ $sth->execute($barcode, $username);
+ $dbh->commit;
+ $sth->finish;
+ my @credentials = ($org_unit, $username, $password);
+ my $credentials_ref = \@credentials;
+ push @completed_accounts, $credentials_ref;
+ }
+}
+
+$dbh->disconnect();
+
+foreach my $account (@completed_accounts) {
+ print qq(\t\t<!-- @$account[0] -->\n\t\t<login id="@$account[1]" password="@$account[2]" institution="gapines"/>\n);
+}
+