#! /bin/ksh
#################### Header ####################
#
# Name        : ${HOME}/Documents/InstallSshKeys.sh
#
# Version     : 1.1
#
# Author      : Gerard van Wageningen
#
# Description : Add public ssh keys to .ssh directory
#
# 1.0   First Version
# 1.1	Use /bin/sh instead of ksh (bash is more supported on Open Source)
#
################# Declarations #################
typeset ID
typeset PROGRAM
typeset FULL_PATH

#
# Perl like exit function
#
Die()
{

  echo "\n${PROGRAM}: $@, bailing out..." 1>&2

  exit 1

} # Die()
#################### Start ####################

if [[ "${DEBUG:-xxx}" != 'xxx' ]]
then
  set -x
fi

PROGRAM="${0##*/}"
FULL_PATH=` \type -p "${0}" 2>/dev/null `
if [[ $? != 0 ]]
then
  echo "type -p \"${0}\" does not seem to work"
  FULL_PATH="${HOME}/bin/${PROGRAM}"
else
  if [[ "${FULL_PATH}" = *\ is\ * ]]
  then
    # InstallSshKeys.sh is /home/${USER}/bin/InstallSshKeys.sh
    FULL_PATH="${FULL_PATH##* }"
  fi
fi  

# Go to the .ssh directory, create it if it does not exists. The cd command
# is escaped to use the build-in command and not some alias. 
mkdir ${HOME}/.ssh 2>/dev/null
\cd ${HOME}/.ssh || Die "Could not cd to ${HOME}/.ssh"

if (( $# != 1 ))
then
  Die "Usage: $0 <remoteHost>"
fi

if [[ "${1}" != __*__ ]]
then
  # We are starting the installation, first generate keys if they are not yet
  # present
  echo "Generate keys if necessary..."
  [[ -f identity ]] || ssh-keygen -t rsa1 -f ${HOME}/.ssh/identity \
  || Die "Could not generate rsa1 key"
  [[ -f id_rsa ]]   || ssh-keygen -t rsa  -f ${HOME}/.ssh/id_rsa   \
  || Die "Could not generate rsa key"
  [[ -f id_dsa ]]   || ssh-keygen -t dsa  -f ${HOME}/.ssh/id_dsa   \
  || Die "Could not generate dsa key"

  if [[ -z "${TRY_SSH_FROM:-}" ]]
  then
    ID=` awk '{print $4}' ${HOME}/.ssh/identity.pub `
  else
    ID=` awk '{print $5}' ${HOME}/.ssh/identity.pub `

    for i in identity.pub id_dsa.pub id_rsa.pub
    do
      if ! grep ^from ${i} 1>/dev/null 2>&1
      then 
        sed -e "s/^/from=\"${HOSTNAME},${HOSTNAME_FQDN}\" /g" ${i} > ${i}.$$
        mv ${i}.$$ ${i}
      fi
    done
  fi
  
  echo "Copying ssh public keys for ${ID} to ${1}..."
  scp identity.pub id_dsa.pub id_rsa.pub "${FULL_PATH}" "${1}:" \
  || Die "Could not copy files to $1"

  echo "Installing ssh public keys..."
  ssh "${1}" "./${PROGRAM}" "__${1}__" \
  || Die "Could not execute ${PROGRAM} on $1"

  exit 0
fi
  
# Pre-create the public key files to avoid errors 
touch authorized_keys authorized_keys2 \
|| Die "Error creating public key files"

# Set ID to the comment string
if [[ -z "${TRY_SSH_FROM:-}" ]]
then
  ID=` awk '{print $4}' ${HOME}/identity.pub `
else
  ID=` awk '{print $5}' ${HOME}/identity.pub `
fi

echo "Installing ssh public keys for ${ID} on ${HOSTNAME:-` uname -n `}"

# Process authorized_keys file
grep -v "${ID}$"  authorized_keys > tempFile
\cat ${HOME}/identity.pub tempFile > authorized_keys \
|| Die "Error setting the .ssh/authorized_keys file"

# Process authorized_keys2 file
sed -e "/ssh-dss [^ ]* ${ID}$/d; /ssh-rsa [^ ]* ${ID}$/d" authorized_keys2 > tempFile
\cat ${HOME}/id_dsa.pub ${HOME}/id_rsa.pub tempFile > authorized_keys2 \
|| Die "Error setting the .ssh/authorized_keys file"

# Remove the temporary files
\cd ${HOME}
rm -f .ssh/tempFile identity.pub id_dsa.pub id_rsa.pub "${PROGRAM}"

# Set the right rigths
chmod -R 700 ${HOME}/.ssh \
|| Die "Error setting the rights"

exit 0

################ EOF ################
