#!/bin/sh
# Copyright (c) 2020 Mastercard

# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

#   http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# wrapper script for SoftHSM2 tokens
# see with_pkcs11_common for usage and alternate invocations

PKCS11PASSWORD=${PKCS11PASSWORD:-changeit}

_p11_vendor=softhsm

# vendor-specific settings
# SoftHSM2 is built as a libtool module, so the library keeps the .so
# extension on macOS as well (there is no .dylib variant).
PKCS11_LIBNAME=libsofthsm2.so
PKCS11_LIBPATHS="/usr/local/lib/softhsm /usr/lib/softhsm /usr/local/opt/softhsm/lib/softhsm"
if [ -n "${HOMEBREW_PREFIX:-}" ]; then
	PKCS11_LIBPATHS="$PKCS11_LIBPATHS $HOMEBREW_PREFIX/lib/softhsm"
elif command -v brew > /dev/null 2>&1; then
	_brew_prefix=$(brew --prefix 2>/dev/null)
	if [ -n "$_brew_prefix" ]; then
		PKCS11_LIBPATHS="$PKCS11_LIBPATHS $_brew_prefix/lib/softhsm"
	fi
	unset _brew_prefix
fi
LIB_ENVVARS="SOFTHSM2_CONF"

# load common logic
. "$(dirname "$0")/with_pkcs11_common" "$@"
