#!/bin/sh

PASSWORD=$(mktemp -d /tmp/temp-one-time-idXXXXXXXXXXXXX)
rmdir $PASSWORD
LEGACY_ROOTCERT=$(mktemp /tmp/safespring-legacy-root-ca-XXXXXXXXXXXXX)
ROOTCERT=$(mktemp /tmp/safespring-root-ca-XXXXXXXXXXXXX)
KDB=/opt/tivoli/tsm/client/ba/bin/dsmcert.kdb
GSK8CAPICMD=gsk8capicmd_64

rm -f /opt/tivoli/tsm/client/ba/bin/dsmcert.*

cat <<ROOT_CA_PEM > "$LEGACY_ROOTCERT"
-----BEGIN CERTIFICATE-----
MIIECDCCAvCgAwIBAgIBADANBgkqhkiG9w0BAQsFADBgMQswCQYDVQQGEwJOTzES
MBAGA1UECgwJSVBuZXR0IEFTMR4wHAYDVQQLDBVJUG5ldHQgQ2xvdWQgU2Vydmlj
ZXMxHTAbBgNVBAMMFElQbmV0dCBDbG91ZCBSb290IENBMB4XDTE1MDExNTEzMjkz
M1oXDTM1MDExMDEzMjkzM1owYDELMAkGA1UEBhMCTk8xEjAQBgNVBAoMCUlQbmV0
dCBBUzEeMBwGA1UECwwVSVBuZXR0IENsb3VkIFNlcnZpY2VzMR0wGwYDVQQDDBRJ
UG5ldHQgQ2xvdWQgUm9vdCBDQTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoC
ggEBAK8y5ni/lQvmMmIGMFCiTJhtKY7ErL7NpM7GyXziizZ0SfPOCsK2OcGN6+5i
tRNbZee1e6wqK71GAokrjMzCZTzdS0n0qWREM4EUBbn9b5cCCUlr5E+SRrjU0oWq
IcnMClsax26FJIXfro3m7gL6EJr5HENwwVZQg9FlCH4Xm/UHuspghTg2/2J3NYkj
5q5ybQaDwEI2L4hCoQUsWY6WzfLMYtOtcJ8bB5imeH/Eck2nxNfRfrfJYNZJQlx8
ac1/iRgBwsC7I4/XJinVi97Zfr16yp2Xl3WDHItYhqM62sPaBTzMKZsQlR3XW2nY
NomGExqALaCF5vkW/soT5hh56CUCAwEAAaOBzDCByTAPB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----
ROOT_CA_PEM

"$GSK8CAPICMD" -keydb -create -db "$KDB" -pw "$PASSWORD" -stash

"$GSK8CAPICMD" -cert -add -db "$KDB" -format ascii -stashed \
  -label "Safespring Cloud Root CA" \
  -file "$LEGACY_ROOTCERT"

"$GSK8CAPICMD" -cert -add -db "$KDB" -format ascii -stashed \
  -label "Safespring Backup Root CA" \
  -file "$ROOTCERT"

"$GSK8CAPICMD" -cert -list -db "$KDB" -stashed

rm -f "$LEGACY_ROOTCERT" "$ROOTCERT"

