#!/bin/bash
# ovpn-comp v4 by Thomas

function compconf {
#necessary files path
ca=ca.crt
cert=$keyname.crt
key=$keyname.key
tls=ta.key #comment this string to disable tls

#put parameters to complete key file
echo "client
dev tun
proto udp
remote vpn.thomas.pp.ua 27772
resolv-retry infinite
nobind
persist-key
persist-tun
cipher AES-128-CBC
auth SHA512
verb 3
#comp-lzo
#log-append openvpn-client.log
keepalive 10 120" > $keyname.ovpn
if [[ $tls ]] ; then
	echo "tls-version-min 1.2
	key-direction 1" >> $keyname.ovpn
fi

#put keys to complete key file
echo "<ca>" >> $keyname.ovpn
cat $ca >> $keyname.ovpn
echo "</ca>" >> $keyname.ovpn
if [[ $tls ]] ; then
	echo "<tls-auth>" >> $keyname.ovpn
	cat $tls >> $keyname.ovpn
	echo "</tls-auth>" >> $keyname.ovpn
fi
echo "<cert>" >> $keyname.ovpn
cat $cert >> $keyname.ovpn
echo "</cert>
<key>" >> $keyname.ovpn
cat $key >> $keyname.ovpn
echo "</key>" >> $keyname.ovpn
}

# script start

#load key name
if [[ $1 ]] ; then
	keyname=$1
	compconf
else
	for filename in *.crt; do
		[ -e "$filename" ] || continue
		keyname=`echo $filename | cut -d "." -f1`
		compconf
	done
fi