#! /bin/bash
# this script is to be used *only* in a post-trans action
# by an "internal" RPM. It installs the ssh private key
# file to allow pbench to move/copy results to the server.

# Usage: pbench-agent-config-ssh-key <path/to/pbench-agent.cfg> <path/to/local/private/key>

# This script installs the local config file that is used by the
# scripts to discover local settings. One of these is the results
# server that move-results uses to copy results to. The move-results
# script uses scp to do that under the user id "pbench". The private
# key for that user, as given by the second argument to this script,
# is copied to the place where move-results expects it.

configfile=$1
keyfile=$2
user=$(getconf.py -C $configfile pbench_user pbench-agent)
group=$(getconf.py -C $configfile pbench_group pbench-agent)

# fallbacks assume the existence of user/group pbench
if [ -z "$user" ] ;then
    user=pbench
fi
if [ -z "$group" ] ;then
    group=pbench
fi

pbenchdir=$(getconf.py -C $configfile install-dir pbench-agent)
# copy the key file and adjust perms
if [ -f $keyfile ] ;then
    cp $keyfile $pbenchdir/id_rsa
    chown $user.$group $pbenchdir/id_rsa
    chmod 600 $pbenchdir/id_rsa
else
    exit 3
fi

exit 0
