#!/usr/bin/perl

use strict;
use File::Basename;

my $basedir = dirname(__FILE__);

sub file_get_contents
{
    my ($file) = @_;
    open(IN, "<$file");
    local $/ = undef;
    my $content = <IN>;
    close IN;

    return $content;
}

my $app = $ARGV[0];
my $s;

$| = 1;

if ($app eq 'idm') {
    my @sync_types;
    $sync_types[1] = 'Organization';
    $sync_types[2] = 'Group';
    $sync_types[3] = 'SecurityGroup';
    $sync_types[4] = 'Contact';

    my $tenant;
    for (;;) {
        print "テナント: ";
        $s = <STDIN>;
        chomp($s);
        if ($s) {
            $tenant = $s;
            last;
        } else {
            print "値を設定して下さい。\n";
        }
    }

    my $ldapuri;
    for (;;) {
        print "LDAPサーバ URI: ";
        $s = <STDIN>;
        chomp($s);
        if ($s) {
            $ldapuri = $s;
            last;
        } else {
            print "値を設定して下さい。\n";
        }
    }

    my $basedn;
    for (;;) {
        print "LDAPサーバ ベースDN: ";
        $s = <STDIN>;
        chomp($s);
        if ($s) {
            $basedn = $s;
            last;
        } else {
            print "値を設定して下さい。\n";
        }
    }

    my $binddn;
    for (;;) {
        print "LDAPサーバ ユーザDN: ";
        $s = <STDIN>;
        chomp($s);
        if ($s) {
            $binddn = $s;
            last;
        } else {
            print "値を設定して下さい。\n";
        }
    }

    my $bindpw;
    for (;;) {
        print "LDAPサーバ パスワード: ";
        $s = <STDIN>;
        chomp($s);
        if ($s) {
            $bindpw = $s;
            last;
        } else {
            print "値を設定して下さい。\n";
        }
    }

    my $adminuser;
    for (;;) {
        print "送信先ユーザID: ";
        $s = <STDIN>;
        chomp($s);
        if ($s) {
            $adminuser = $s;
            last;
        } else {
            print "値を設定して下さい。\n";
        }
    }

    my $adminpw;
    for (;;) {
        print "送信先パスワード: ";
        $s = <STDIN>;
        chomp($s);
        if ($s) {
            $adminpw = $s;
            last;
        } else {
            print "値を設定して下さい。\n";
        }
    }

    print "同期するエントリ[1.組織 2.ユーザグループ 3.セキュリティグループ 4.連絡先]: ";
    $s = <STDIN>;
    chomp($s);
    my $type = $s;

    print "組織のベースDN: ";
    $s = <STDIN>;
    chomp($s);
    my $obasedn = $s;

    print "ユーザグループのベースDN: ";
    $s = <STDIN>;
    chomp($s);
    my $gbasedn = $s;

    print "連絡先のベースDN: ";
    $s = <STDIN>;
    chomp($s);
    my $cbasedn = $s;

    print "組織から除外するOU: ";
    $s = <STDIN>;
    chomp($s);
    my $exorgs = $s;

    my $content = &file_get_contents("$basedir/templates/lism-idp.conf");
    $content =~ s/LDAPURI/$ldapuri/;
    $content =~ s/LDAPBASEDN/$basedn/;
    $content =~ s/LDAPBINDDN/$binddn/;
    $content =~ s/LDAPBINDPW/$bindpw/;
    $content =~ s/TENANTID/$tenant/g;
    $content =~ s/ADMINID/$adminuser/g;
    $content =~ s/ADMINPW/$adminpw/;
    if ($type) {
        foreach my $n (split(/, */g, $type)) {
            my $sync_type = $sync_types[$n];
            $content =~ s/<!-- $sync_type/<!-- $sync_type  -->/g;
            $content =~ s/ $sync_type -->/<!--  $sync_type -->/g;
        }
    }
    $obasedn .= !$obasedn || $obasedn =~ /,$/ ? '' : ',';
    $content =~ s/OBASEDN/$obasedn/g;
    $gbasedn .= !$gbasedn || $gbasedn =~ /,$/ ? '' : ',';
    $content =~ s/GBASEDN/$gbasedn/g;
    $cbasedn .= !$cbasedn || $cbasedn =~ /,$/ ? '' : ',';
    $content =~ s/CBASEDN/$cbasedn/g;
    if ($exorgs) {
        $exorgs =~ s/, */\|/g;
        $exorgs = '|'.$exorgs;
    } else {
        $exorgs = '';
    }
    $content =~ s/EXORGS/$exorgs/;
    open FD, ">/opt/secioss/etc/lism-idp.conf";
    print FD $content;
    close FD;
} elsif ($app eq 'sso') {
    my $host;
    for (;;) {
        print "ホスト名（例: https://idp.example.com）: ";
        $s = <STDIN>;
        chomp($s);
        if ($s) {
            $host = $s;
            last;
        } else {
            print "値を設定して下さい。\n";
        }
    }

    my $ldapuri;
    for (;;) {
        print "LDAPサーバ URI: ";
        $s = <STDIN>;
        chomp($s);
        if ($s) {
            $ldapuri = $s;
            last;
        } else {
            print "値を設定して下さい。\n";
        }
    }

    my $basedn;
    for (;;) {
        print "LDAPサーバ ベースDN: ";
        $s = <STDIN>;
        chomp($s);
        if ($s) {
            $basedn = $s;
            last;
        } else {
            print "値を設定して下さい。\n";
        }
    }

    my $binddn;
    for (;;) {
        print "LDAPサーバ ユーザDN: ";
        $s = <STDIN>;
        chomp($s);
        if ($s) {
            $binddn = $s;
            last;
        } else {
            print "値を設定して下さい。\n";
        }
    }

    my $bindpw;
    for (;;) {
        print "LDAPサーバ パスワード: ";
        $s = <STDIN>;
        chomp($s);
        if ($s) {
            $bindpw = $s;
            last;
        } else {
            print "値を設定して下さい。\n";
        }
    }

    my $winauth;
    for (;;) {
        print "認証方式[1.ID/パスワード認証 2.統合Windows認証]: ";
        $s = <STDIN>;
        chomp($s);
        if ($s) {
            $winauth = $s;
            last;
        } else {
            print "値を設定して下さい。\n";
        }
    }

    my $content = &file_get_contents("$basedir/templates/saml20-idp-hosted.php");
    $content =~ s/https:\/\/idp\.example\.com/$host/;
    if ($winauth == 1) {
        $content =~ s/login-env\.php/login.php/;
    }
    open FD, ">/opt/secioss/share/simplesamlphp/metadata/saml20-idp-hosted.php";
    print FD $content;
    close FD;

    $content = &file_get_contents("$basedir/templates/ldap.php");
    $content =~ s/'auth\.ldap\.hostname'   => 'localhost'/'auth.ldap.hostname'   => \'$ldapuri\'/;
    $content =~ s/'auth\.ldap\.search\.base' => 'dc=example,dc=com'/'auth.ldap.search.base' => \'$basedn\'/;
    $content =~ s/'auth\.ldap\.search\.username' => 'cn=Manager,dc=example,dc=com'/'auth.ldap.search.username' => \'$binddn\'/;
    $content =~ s/'auth\.ldap\.search\.password' => 'secret'/'auth.ldap.search.password' => \'$bindpw\'/;
    open FD, ">/opt/secioss/share/simplesamlphp/config/ldap.php";
    print FD $content;
    close FD;

    $content = &file_get_contents("$basedir/templates/idsync.ini");
    $content =~ s/uri = "ldap:\/\/localhost"/uri = \"$ldapuri\"/;
    $content =~ s/basedn = "DC=example,DC=com"/basedn = \"$basedn\"/;
    open FD, ">/opt/secioss/var/www/conf/idsync.ini";
    print FD $content;
    close FD;
} else {
    print "usage: config.pl [sso|idm]\n";
}
