#!/bin/bash
#
# @package      hubzero-submit-distributor
# @file         genuserpki
# @author       Steven Clark <clarks@purdue.edu>
# @copyright    Copyright (c) 2004-2015 HUBzero Foundation, LLC.
# @license      http://opensource.org/licenses/MIT MIT
#
# Copyright (c) 2004-2015 HUBzero Foundation, LLC.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# HUBzero is a registered trademark of HUBzero Foundation, LLC.
#
trap onerror ERR

onerror() {
   if [ "${LOCK}" != "" ] ; then
      rm -f ${LOCK}
   fi
   exit 1
}

JOBKEYNAME=${1:-condorpki}
SUBMITHOST=`hostname --fqdn`

PKI=""
LOCK=""

if [ ! -d ${HOME}/.ssh ] ; then
   mkdir ${HOME}/.ssh
   chmod 700 ${HOME}/.ssh
fi
cd ${HOME}/.ssh

START=`date +"%s"`
while [ -f ${JOBKEYNAME}.lock ] ; do
   sleep 2
   NOW=`date +"%s"`
   ELAPSED=$(( ${NOW} - ${START} ))
   if [ ${ELAPSED} -gt 60 ] ; then
      rm ${JOBKEYNAME}.lock
   fi
done
LOCK=${PWD}/${JOBKEYNAME}.lock
touch ${LOCK}

if [ -f authorized_keys ] ; then
   KEYCOUNT=`grep "${JOBKEYNAME}@${SUBMITHOST}" authorized_keys | wc -l`
   if [ ${KEYCOUNT} -ne 1 ] ; then
      rm -f ${JOBKEYNAME} ${JOBKEYNAME}.pub
   fi
fi

if [ -f ${JOBKEYNAME} ] ; then
   if [ ! -f ${JOBKEYNAME}.pub ] ; then
      rm ${JOBKEYNAME}
   fi
fi
if [ -f ${JOBKEYNAME}.pub ] ; then
   if [ ! -f ${JOBKEYNAME} ] ; then
      rm ${JOBKEYNAME}.pub
   fi
fi
if [ -f ${JOBKEYNAME} ] ; then
   PKI=${HOME}/.ssh/${JOBKEYNAME}
else
   if [ -f authorized_keys ] ; then
      sed -e "/${JOBKEYNAME}@${SUBMITHOST}/d" authorized_keys >  authorized_keys.${JOBKEYNAME}
      echo -e '\n'                                            >> authorized_keys.${JOBKEYNAME}
      cat -s authorized_keys.${JOBKEYNAME} > authorized_keys
      rm -f authorized_keys.${JOBKEYNAME}
   fi
   ssh-keygen -N "" -f ${JOBKEYNAME} -C "${JOBKEYNAME}@${SUBMITHOST}" > /dev/null
   chmod 600 ${JOBKEYNAME}.pub
   echo -n 'from="'${SUBMITHOST}'" ' >> authorized_keys
   cat ${JOBKEYNAME}.pub             >> authorized_keys
   PKI=${HOME}/.ssh/${JOBKEYNAME}
fi

rm -f ${LOCK}

echo ${PKI}
exit 0
