#!/bin/bash
#
# recordum_ssh_keys
#
# v0.1 2015-11-20 RE
#
# generate the ssh user keys for the recordum user
#
# sample output:
#
# Generating public/private rsa key pair.
# Your identification has been saved in /var/home/recordum/.ssh/id_rsa.
# Your public key has been saved in /var/home/recordum/.ssh/id_rsa.pub.
# The key fingerprint is:
# 3f:55:51:7f:80:14:11:38:86:97:49:e4:cb:1c:46:60 recordum@airqrate-2014-00ts1
# The key's randomart image is:
# +--[ RSA 2048]----+
# |        E=+===o..|
# |       ..oB .  o.|
# |         o+.  . o|
# |         + o .  .|
# |        S + .    |
# |         . .     |
# |          o      |
# |           .     |
# |                 |
# +-----------------+
#
# show the id_rsa.pub content in the web-interface
# thus allow us to use copy/paste on the portal
# to copy the content of /var/home/recordum/.ssh/id_rsa.pub
# on the target add pub key to /root/.ssh/authorized_keys

MYSELF=$( basename $0 )
MYHOME=/var/home/recordum
WEBTARGET=/var/www/config

if [[ $UID -gt 0 ]]
then
    echo "$MYSELF needs to be run as superuser. You are $USER."
    exit 1
fi
if [[ ! -d ${MYHOME} ]]
then
    echo "$MYHOME does not exist"
    exit 2
fi

# clean up old keys
KEYS=${MYHOME}/.ssh/id*
for f in ${KEYS}
do
    rm -f ${f}
done
if [[ -f ${WEBTARGET}/recordum_public.key ]]
then
    rm -f ${WEBTARGET}/recordum_public.key
fi
# generate the keys and be verbose about it
su - recordum -c "ssh-keygen -v -t rsa -b 2048 -N '' -f '/var/home/recordum/.ssh/id_rsa'"
# copy the public key onto the web tree
cp -f ${MYHOME}/.ssh/id_rsa.pub ${WEBTARGET}/recordum_public.key
chown recordum:www-data ${WEBTARGET}/recordum_public.key

exit 0
