#!/bin/bash
set -x

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

## DRM ports
kra_nonssl_port=10180
kra_agent_port=10443
kra_ee_port=10444
kra_admin_port=10445

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


## TPS ports
tps_nonssl_port=7888
tps_clientauth_port=7889
tps_nonclientauth_port=7890


##  PKI Silent Admin Variables
##  (e. g. - Import Admin Certificate into PKI Silent "browser" database)
pki_silent_admin_user=admin
pki_silent_admin_password=testpasswd
pki_silent_admin_email=pki-tps-admin@test.com


##  PKI Subsystem Host (computed by default)
pki_host=`hostname`

# PKI Subsystem Instance Names
tps_instance_name=pki-tps

#PKI Subsystem Name
tps_subsystem_name="Token\ Processing\ System"


#  PKI Silent Security Database Variables
pki_silent_security_database_repository=/tmp/testdb
pki_silent_security_database_password=testpasswd


# PKI Subsystem Instance PINS
tps_preop_pin=`cat /var/lib/${tps_instance_name}/conf/CS.cfg \
        | grep preop.pin | grep -v grep | awk -F= '{print $2}'`



##  PKI Security Domain Variables
##  (e. g. - Security Domain Login Panel)
pki_security_domain_name=silentdom
pki_security_domain_host=`hostname`
pki_security_domain_admin_name=admin
pki_security_domain_admin_password=testpasswd


##  PKI Internal LDAP Database Variables
pki_ldap_host=localhost
pki_ldap_port=389
pki_bind_dn="cn=Directory\ Manager"
pki_bind_password=testpasswd




#Misc Variables
tps_chosen_ca_hostname=${pki_security_domain_host}
tps_chosen_ca_nonssl_port=${ca_nonssl_port}
tps_chosen_ca_ssl_port=${ca_ee_port}
tps_chosen_ca_admin_port=${ca_admin_port}
tps_chosen_tks_hostname=${pki_host}
tps_chosen_tks_ssl_port=${tks_ee_port}
tps_chosen_drm_hostname=${pki_host}
tps_chosen_drm_ssl_port=${kra_ee_port}
tps_agent_name="TPS\ Administrator\'s\ ${pki_security_domain_name}\ ID"
tps_agent_key_size=2048
tps_agent_key_type=rsa
tps_agent_cert_subject="cn=TPS\ Administrator,uid=admin,e=${pki_silent_admin_email},o=${pki_security_domain_name}"
tps_ldap_auth_host=localhost
tps_ldap_auth_port=389
tps_ldap_auth_base_dn="dc=pnq,dc=testpasswd,dc=com"
tps_base_dn="dc=${pki_host}-${tps_instance_name}"
tps_db_name="${pki_host}-${tps_instance_name}"
tps_key_size=2048
tps_key_type=rsa
tps_ss_keygen=true
tps_server_cert_subject_name="cn=${pki_host},ou=${tps_instance_name},o=${pki_security_domain_name}"
tps_server_cert_nickname="Server-Cert\ cert-${tps_instance_name}"
tps_subsystem_cert_subject_name="cn=TPS\ Subsystem\ Certificate,ou=${tps_instance_name},o=${pki_security_domain_name}"
tps_subsystem_cert_nickname="subsystemCert\ cert-${tps_instance_name}"
tps_audit_signing_cert_subject_name="cn=TPS\ Audit\ Signing\ Certificate,ou=${tps_instance_name},o=${pki_security_domain_name}"
tps_audit_signing_cert_nickname="auditSigningCert\ cert-${tps_instance_name}"

pki_silent_tps_log=/tmp/pki-silent-tps.log
tps_token_name=internal
tps_token_password=testpasswd

################################################################################################################
##Configure TPS
printf "'${pki_silent_script}':  Configuring '${tps_instance_name}' . . .\n"
pkisilent ConfigureTPS \
        -cs_hostname "${pki_host}" \
        -cs_port ${tps_nonclientauth_port} \
        -cs_clientauth_port ${tps_clientauth_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 ${tps_chosen_ca_hostname} \
        -ca_port ${tps_chosen_ca_nonssl_port} \
        -ca_ssl_port ${tps_chosen_ca_ssl_port} \
        -ca_admin_port ${tps_chosen_ca_admin_port} \
        -drm_hostname ${tps_chosen_drm_hostname} \
        -drm_ssl_port ${tps_chosen_drm_ssl_port} \
        -ss_keygen ${tps_ss_keygen} \
        -tks_hostname ${tps_chosen_tks_hostname} \
        -tks_ssl_port ${tps_chosen_tks_ssl_port} \
        -client_certdb_dir ${pki_silent_security_database_repository} \
        -client_certdb_pwd ${pki_silent_security_database_password} \
        -preop_pin ${tps_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 ${tps_agent_name} \
        -ldap_host ${pki_ldap_host} \
        -ldap_port ${pki_ldap_port} \
        -bind_dn "${pki_bind_dn}" \
        -bind_password ${pki_bind_password} \
        -base_dn "${tps_base_dn}" \
        -db_name "${tps_db_name}" \
        -key_size ${tps_key_size} \
        -key_type ${tps_key_type} \
        -token_name ${tps_token_name} \
        -token_pwd ${tps_token_password} \
        -agent_key_size ${tps_agent_key_size} \
        -agent_key_type ${tps_agent_key_type} \
        -agent_cert_subject "${tps_agent_cert_subject}" \
        -subsystem_name ${tps_subsystem_name} \
        -ldap_auth_host ${tps_ldap_auth_host} \
        -ldap_auth_port ${tps_ldap_auth_port} \
        -ldap_auth_base_dn ${tps_ldap_auth_base_dn} \
        -tps_server_cert_subject_name "${tps_server_cert_subject_name}" \
        -tps_server_cert_nickname "${tps_server_cert_nickname}" \
        -tps_subsystem_cert_subject_name "${tps_subsystem_cert_subject_name}" \
        -tps_subsystem_cert_nickname "${tps_subsystem_cert_nickname}" \
        -tps_audit_signing_cert_subject_name \
        "${tps_audit_signing_cert_subject_name}" \
        -tps_audit_signing_cert_nickname \
        "${tps_audit_signing_cert_nickname}" \
        | tee ${pki_silent_tps_log}

##  Restart TPS
#/sbin/service ${tps_instance_name} restart

