#!/bin/bash
TARGET=$1
ALIASPATH=$2

get_script_dir () {
     #https://www.ostricher.com/2014/10/the-right-way-to-get-the-directory-of-a-bash-script/
     SOURCE="${BASH_SOURCE[0]}"
     # While $SOURCE is a symlink, resolve it
     while [ -h "$SOURCE" ]; do
          DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
          SOURCE="$( readlink "$SOURCE" )"
          # If $SOURCE was a relative symlink (so no "/" as prefix, need to resolve it relative to the symlink base directory
          [[ $SOURCE != /* ]] && SOURCE="$DIR/$SOURCE"
     done
     DIR="$( cd -P "$( dirname "$SOURCE" )" && pwd )"
     echo "$DIR"
}
MY_PATH=$(get_script_dir)

DETECTED_HOST_OS=`${MY_PATH}/DetectedHostOS`

# target file must exist for MakeSymbolicLink, but we dont want to really create an empty file, so remove after
# Comes up in Skel script
FORCE_FILE_EXISTS_FIRST=0
if [[ "$DETECTED_HOST_OS" == "Cygwin"  ]] ; then
    export CYGWIN="winsymlinks:nativestrict;$CYGWIN"
    if [ ! -e $TARGET ] ; then
        FORCE_FILE_EXISTS_FIRST=1
    fi
fi
if [[ "$DETECTED_HOST_OS" == "MSYS"  ]] ; then
    export MSYS="winsymlinks:nativestrict;$MSYS"
    if [ ! -e $TARGET ] ; then
        FORCE_FILE_EXISTS_FIRST=1
    fi
fi

if [ $FORCE_FILE_EXISTS_FIRST == 1 ] ; then
    touch $TARGET
fi
if [ $# == 2 ] ; then
    ln -s $TARGET $ALIASPATH
else
    ln -s $TARGET
fi
if [ $FORCE_FILE_EXISTS_FIRST == 1 ] ; then
    rm $TARGET
fi
