#!/bin/bash
# Easy script to create OpenVPN client configuration with the user, pre-generating user's
# RSA key and certificate.
# 
# Configuration template must exist in the same directory, with only missing part: certificates.
#
# (c) Dmytro Kovalov, 2015
#

cd $(dirname ${BASH_SOURCE[0]})

read -p "Please type in user name for the new config: " USER

[ -z ${USER} ] && { echo "Cannot be empty"; exit 1; }

[ -f keys/${USER}.crt ] && { echo "Certificate keys/${USER}.crt already exists"; exit 2; }

source ./vars

./build-key ${USER}


(
# This should be existing config template, with only missing certificates, and keys sections.
cat config.ovpn.tpl

echo '<key>'
cat keys/${USER}.key
echo '</key>'

echo '<cert>'
cat keys/${USER}.crt
echo '</cert>'

echo '<ca>'
cat keys/ca.crt
echo '</ca>'
) > openvpn_${USER}.ovpn