#!/bin/bash

# This shell script is part of package cp-vpn
#
# Copyright (c) 2014 Com-Pro, Germany
#
# It will be executed to load the basic configuration
# from the registered User.
# If there's no registration file found, it will exit
# and the service is not started.

EXITSTATE=1
CONFIGDIR="$1"

if [ -e "${CONFIGDIR}cp-vpn.reg" ]; then
  AUTHSTRING=`cat ${CONFIGDIR}cp-vpn.reg`
  if [ -e "${CONFIGDIR}cp-vpn.conf" ]; then
    mv "${CONFIGDIR}cp-vpn.conf" "${CONFIGDIR}cp-vpn.conf.old"
  fi
  wget https://vpn.dsx1.org/peerconf.php?as=${AUTHSTRING} --ca-certificate="${CONFIGDIR}cp-vpn-ca.crt" --certificate="${CONFIGDIR}cp-vpn.crt" --private-key="${CONFIGDIR}cp-vpn.key" -O "${CONFIGDIR}cp-vpn.conf"
  
  EXITSTATE=$?
fi

if [ $EXITSTATE != 0 ]; then
  if [ -e "${CONFIGDIR}cp-vpn.conf.old" ]; then
    mv "${CONFIGDIR}cp-vpn.conf.old" "${CONFIGDIR}cp-vpn.conf"
  fi
fi

exit ${EXITSTATE}
