#!/bin/bash
#
# OpenVPN Configuration files converter to TunnelBlick files
# Support .conf and .ovpn
#
# Author: http://root.sx
# Usage: - Put the script in the directory who contains the conf/ovpn files
#        - Make it executable ($ chmod +x converter.sh)
#        - Execute it ($ ./converter.sh)
# License: Attribution-NonCommercial-ShareAlike 3.0 Unported (CC BY-NC-SA 3.0)
#          http://creativecommons.org/licenses/by-nc-sa/3.0/
#


for FILE in *.{ovpn,conf}
do
	echo "Converting $FILE"
	NAME=$FILE
	NAME=`echo ${NAME// /_}`
	NAME=`echo ${NAME//(/}`
	NAME=`echo ${NAME//)/}`
	NAME=`echo ${NAME//[/}`
	NAME=`echo ${NAME//]/}`
	DIR=$(basename "$NAME")
	DIR="${DIR%.*}"
	mkdir $DIR 2>/dev/null
	mv "$FILE" $DIR 2>/dev/null
	cp */*.crt $DIR 2>/dev/null || cp *.crt $DIR 2>/dev/null
	cp */*.key $DIR 2>/dev/null || cp *.key $DIR 2>/dev/null
	mv $DIR "$DIR.tblk" 2>/dev/null
	echo "done...$DIR.tblk"
done