#!/bin/bash
set -x

kra_admin_port=10445
pki_host=`hostname`
pki_security_domain_host=`hostname`
pki_security_domain_admin_name=admin
pki_security_domain_admin_password=testpasswd
kra_instance_name=pki-kra

## CA ports
ca_nonssl_port=9180
ca_agent_port=9443
ca_ee_port=9444
ca_admin_port=9445

pki_silent_security_database_repository=/tmp/testdb
pki_silent_security_database_password=testpasswd
kra_preop_pin=`cat /var/lib/${kra_instance_name}/conf/CS.cfg \
        | grep preop.pin | grep -v grep | awk -F= '{print $2}'`

pki_security_domain_name=silentdom
pki_silent_admin_user=admin
pki_silent_admin_password=testpasswd
pki_silent_admin_email=pki-kra-admin@test.com
kra_agent_name=kra-agent

pki_ldap_host=localhost
pki_ldap_port=389
pki_bind_dn="cn=Directory\ Manager"
pki_bind_password=testpasswd

kra_base_dn="dc=${pki_host}-${kra_instance_name}"
kra_db_name="${pki_host}-${kra_instance_name}"
kra_key_size=2048
kra_key_type=rsa
kra_token_name=internal
kra_token_password=testpasswd
kra_agent_key_size=2048
kra_agent_key_type=rsa
kra_agent_cert_subject="cn=KRA\ Administrator\ of\ Instance\ ${kra_instance_name},uid=admin,e=${pki_silent_admin_email},o=${pki_security_domain_name}"
kra_subsystem_name="Data\ Recovery\ Manager"
kra_backup_password=testpasswd

kra_transport_cert_subject_name="cn=DRM\ Transport\ Certificate,o=${pki_security_domain_name}"
kra_subsystem_cert_subject_name="cn=DRM\ Subsystem\ Certificate,o=${pki_security_domain_name}"
kra_storage_cert_subject_name="cn=DRM\ Storage\ Certificate,o=${pki_security_domain_name}"
kra_server_cert_subject_name="cn=${pki_host},o=${pki_security_domain_name}"
kra_audit_signing_cert_subject_name="cn=DRM\ Audit\ Signing\ Certificate,o=${pki_security_domain_name}"

pki_silent_kra_log=/tmp/pki-silent-kra-log

pkisilent ConfigureDRM \
        -cs_hostname "${pki_host}" \
        -cs_port ${kra_admin_port} \
        -sd_hostname "${pki_security_domain_host}" \
        -sd_ssl_port ${ca_ee_port} \
        -sd_agent_port ${ca_agent_port} \
        -sd_admin_port ${ca_admin_port} \
        -sd_admin_name "${pki_security_domain_admin_name}" \
        -sd_admin_password ${pki_security_domain_admin_password} \
        -ca_hostname ${pki_security_domain_host} \
        -ca_port ${ca_nonssl_port} \
        -ca_ssl_port ${ca_ee_port} \
        -client_certdb_dir ${pki_silent_security_database_repository} \
        -client_certdb_pwd ${pki_silent_security_database_password} \
        -preop_pin ${kra_preop_pin} \
        -domain_name "${pki_security_domain_name}" \
        -admin_user ${pki_silent_admin_user} \
        -admin_password ${pki_silent_admin_password} \
        -admin_email "${pki_silent_admin_email}" \
        -agent_name ${kra_agent_name} \
        -ldap_host ${pki_ldap_host} \
        -ldap_port ${pki_ldap_port} \
        -bind_dn "${pki_bind_dn}" \
        -bind_password ${pki_bind_password} \
        -base_dn "${kra_base_dn}" \
        -db_name "${kra_db_name}" \
        -key_size ${kra_key_size} \
        -key_type ${kra_key_type} \
        -token_name ${kra_token_name} \
        -token_pwd ${kra_token_password} \
        -agent_key_size ${kra_agent_key_size} \
        -agent_key_type ${kra_agent_key_type} \
        -agent_cert_subject "${kra_agent_cert_subject}" \
        -subsystem_name ${kra_subsystem_name} \
        -backup_pwd ${kra_backup_password} \
        -drm_transport_cert_subject_name "${kra_transport_cert_subject_name}" \
        -drm_subsystem_cert_subject_name "${kra_subsystem_cert_subject_name}" \
        -drm_storage_cert_subject_name "${kra_storage_cert_subject_name}" \
        -drm_server_cert_subject_name "${kra_server_cert_subject_name}" \
        -drm_audit_signing_cert_subject_name \
        "${kra_audit_signing_cert_subject_name}" \
        | tee ${pki_silent_kra_log}

