#!/bin/bash

# create ssh folder
if [ ! -d ~/.ssh ]; then mkdir ~/.ssh; fi
chmod 700 ~/.ssh

# get server and username
echo "Enter hostname for job submission and press [ENTER] :" 
read SERVER
if [ "$SERVER" == "" ]; then exit 0; fi

echo "Enter your username on ${SERVER} and press [ENTER] :" 
read USERNAME
if [ "$USERNAME" == "" ]; then exit 0; fi

# create ssh key and add to config file
if [ ! -e ~/.ssh/${SERVER} ]; then
  ssh-keygen -q -t rsa -N "" -f ~/.ssh/${SERVER}

  cat >> ~/.ssh/config << EOF

Host ${SERVER}
HostName ${SERVER}
User ${USERNAME}
IdentityFile ~/.ssh/${SERVER}
ServerAliveInterval 300
ControlMaster auto 
ControlPath /tmp/%r@%h:%p
EOF
  chmod 600 ~/.ssh/config
fi

# add ebi-cluster to known_hosts
if [ ! -e ~/.ssh/known_hosts ]; then
  if [ "$SERVER" == "ebi-cluster" -o "$SERVER" == "ebi-cluster.igb.uiuc.edu" -o "$SERVER" == "ebi-cluster.igb.illinois.edu" ]; then
    cat >> ~/.ssh/known_hosts << EOF
|1|l+VkFK7zeYBwudLstZfm37DK48E=|MQDja2Ro4KoY2dMz45D5GPKPS8M= ssh-rsa [payload removed: 372 characters]
|1|xR8lfSy1VnfuBhkIQ7i//qa3TUY=|cYqjphhu4IG9DCac/PXhiyZgVUI= ssh-rsa [payload removed: 372 characters]
EOF
  fi
fi

# show key
echo "Please add ~/.ssh/${SERVER}.pub to your ~/.ssh/authorized_keys on $SERVER" 
echo "cat ~/.ssh/${SERVER}.pub | ssh ${USERNAME}@${SERVER} \"cat >> ~/.ssh/authorized_keys\"" 
