#!/bin/bash
#######################################################################
#       Openvpn supports inline certs and keys
#       so you have one client script, instead of script plus 4 keys and certs
#
#       This tool requires
#       1) openvpn script, certs and keys are in same directory
#       2) The names of openvpn script, certs and keys as follows
#

ca="ca.crt"
cert="xclientx.crt"
key="xclientx.key"
tlsauth="ta.key"
ovpndest="xclientx.ovpn"

########################################################################
#	Backup to new subdirectory, just in case
#
mkdir -p backup
cp $ca $cert $key $tlsauth $ovpndest ./backup

########################################################################
#	Delete existing call to keys and certs
#
	sed -i \
	-e '/ca .*'$ca'/d'  \
	-e '/cert .*'$cert'/d' \
	-e '/key .*'$key'/d' \
	-e '/tls-auth .*'$tlsauth'/d' $ovpndest 

########################################################################
#	Add keys and certs inline
#
echo "key-direction 1" >> $ovpndest

echo "<ca>" >> $ovpndest
awk /BEGIN/,/END/ < ./$ca >> $ovpndest
echo "</ca>" >> $ovpndest

echo "<cert>" >> $ovpndest
awk /BEGIN/,/END/ < ./$cert >> $ovpndest
echo "</cert>" >> $ovpndest

echo "<key>" >> $ovpndest
awk /BEGIN/,/END/ < ./$key >> $ovpndest
echo "</key>" >> $ovpndest

echo "<tls-auth>" >> $ovpndest
awk /BEGIN/,/END/ < ./$tlsauth >> $ovpndest
echo "</tls-auth>" >> $ovpndest

########################################################################
#	Delete key and cert files, backup already made hopefully
#
rm $ca $cert $key $tlsauth