#!/bin/bash

if [ -x /usr/local/bin/privx-agent-unix ]; then
    PRIVX_AGENT_DIR=`mktemp -d -t 'mytmpdir'`
    export SSH_AUTH_SOCK="$PRIVX_AGENT_DIR/agent"
    export PRIVX_AGENT_PROXY="$PRIVX_AGENT_DIR/proxy"
    /usr/local/bin/privx-agent-unix -a $SSH_AUTH_SOCK -x $PRIVX_AGENT_PROXY -config $HOME/.privx/agent.json > /dev/null 2>&1 &
    export SSH_AGENT_PID=$!
    launchctl setenv SSH_AUTH_SOCK $SSH_AUTH_SOCK
    launchctl setenv SSH_AGENT_PID $SSH_AGENT_PID
    launchctl setenv PRIVX_AGENT_PROXY $PRIVX_AGENT_PROXY
    trap 'trap - TERM; kill -s TERM -- -$$' TERM
    tail -f /dev/null & wait
    exit 0
fi