#/bin/bash

set -x

#tks instance name
tks_instance_name="pki-tks"

#PKI Security Domain Variables
pki_security_domain_host=`hostname`
pki_security_domain_name=silentdom
pki_security_domain_admin_name=admin
pki_security_domain_admin_password=testpasswd

#pin
tks_preop_pin=`cat /var/lib/${tks_instance_name}/conf/CS.cfg \
        | grep preop.pin | grep -v grep | awk -F= '{print $2}'`


#######################
#port declaration
#######################
## TKS ports
tks_nonssl_port=13180
tks_agent_port=13443
tks_ee_port=13444
tks_admin_port=13445

## CA ports(CA which is already configured)
ca_ee_port=9444
ca_agent_port=9443
ca_admin_port=9445
ca_nonssl_port=9180

pki_silent_security_database_repository="/tmp/testdb"
pki_silent_security_database_password=testpasswd
#pki silent admin variables
pki_silent_admin_email=pki-tks-admin@test.com
pki_silent_admin_user=admin
pki_silent_admin_password=testpasswd

######################
#variable declaration
######################
pki_host=`hostname`
tks_agent_name="TKS\ Administrator\ of\ Instance\ ${tks_instance_name}\'s\ ${pki_security_domain_name}\ ID"
tks_agent_key_size=2048
tks_agent_key_type=rsa
tks_agent_cert_subject="cn=tks-admin-instance-of-pki-tks2,uid=admin,e=${pki_silent_admin_email},o=${pki_security_domain_name}"
tks_base_dn="dc=${pki_host}-${tks_instance_name}"
tks_db_name="${pki_host}-${tks_instance_name}"
tks_key_size=2048
tks_key_type=rsa
tks_subsystem_cert_subject_name="cn=TKS\ Administrator\ of\ Instance\ ${tks_instance_name},uid=admin,e=${pki_silent_admin_email},o=${pki_security_domain_name}"
tks_server_cert_subject_name="cn=testservercert,o=${pki_security_domain_name}"
tks_audit_signing_cert_subject_name="cn=tksauditcert,o=${pki_security_domain_name}"
tks_subsystem_name="Token\ Key\ Service"


pki_silent_tks_log=/tmp/pki-silent-tks.log

#ldap related
pki_ldap_host=localhost
pki_ldap_port=389
tks_base_dn="dc=${pki_host}-${tks_instance_name}"
pki_bind_dn="cn=Directory\ Manager"
pki_bind_password=testpasswd

#backup
tks_backup_password=testpasswd





pkisilent ConfigureTKS \
        -cs_hostname "${pki_host}" \
        -cs_port ${tks_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 ${tks_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 ${tks_agent_name} \
        -ldap_host ${pki_ldap_host} \
        -ldap_port ${pki_ldap_port} \
        -bind_dn "${pki_bind_dn}" \
        -bind_password ${pki_bind_password} \
        -base_dn "${tks_base_dn}" \
        -db_name "${tks_db_name}" \
        -key_size ${tks_key_size} \
        -key_type ${tks_key_type} \
	-token_name internal \
	-token_pwd testpasswd \
        -agent_key_size ${tks_agent_key_size} \
        -agent_key_type ${tks_agent_key_type} \
        -agent_cert_subject "${tks_agent_cert_subject}" \
        -subsystem_name ${tks_subsystem_name} \
	-backup_pwd ${tks_backup_password} \
        -tks_subsystem_cert_subject_name "${tks_subsystem_cert_subject_name}" \
        -tks_server_cert_subject_name "${tks_server_cert_subject_name}" \
        -tks_audit_signing_cert_subject_name \
        "${tks_audit_signing_cert_subject_name}" \
        | tee ${pki_silent_tks_log}

