#! /bin/bash
# (c) by ennit interactive GmbH, 2015
# Author jbradler@ennit.com

# Include defaults if available
if [ -f /etc/default/hosting-tools ]; then
    . /etc/default/hosting-tools
fi

TYPE="nginx"

if [[ $# -eq 0 ]] ; then
    echo "  Missing argument. Source file name for symlink is missing. Aborting."
    exit 1
fi

ORIGIN=$1
BASENAME=`basename $ORIGIN`
SYMLINK_DIR="/etc/$TYPE/sites-enabled"

find -L ${SYMLINK_DIR} -type l -exec rm -f {} +

if [ ! -f $ORIGIN ] || [ "" == "${BASENAME}" ]; then
    echo "  Could not find config file. Aborting."
    exit 1
fi

if [ -f "${SYMLINK_DIR}/${BASENAME}" ]; then
    echo "  Symlink target exists already. Aborting."
    exit 1
fi

ln -s ${ORIGIN} ${SYMLINK_DIR}/${BASENAME} 2> /dev/null

if [ $? -eq 0 ]; then
    exit 0
else
    echo "  Could not create symlink."
    exit 1
fi