#!/usr/bin/env bash
existing_filepath="$1"
target_filepath="$2"

# 
# ensure FORNIX_FOLDER exists
# 
if [ -z "$FORNIX_FOLDER" ]
then
    # 
    # find fornix_core
    # 
    path_to_file=""
    file_name="settings/fornix_core"
    folder_to_look_in="$PWD"
    while :
    do
        # check if file exists
        if [ -f "$folder_to_look_in/$file_name" ]
        then
            path_to_file="$folder_to_look_in/$file_name"
            break
        else
            if [ "$folder_to_look_in" = "/" ]
            then
                break
            else
                folder_to_look_in="$(dirname "$folder_to_look_in")"
            fi
        fi
    done
    if [ -z "$path_to_file" ]
    then
        #
        # what to do if file never found
        #
        echo "Im a script running with a pwd of:$PWD"
        echo "Im looking for settings/fornix_core in a parent folder"
        echo "Im exiting now because I wasnt able to find it"
        echo "thats all the information I have"
        exit
    fi
    export FORNIX_NEXT_RUN_ONLY_DO_BASIC_INIT="true"
    # run the basic init of fornix to get the FORNIX_FOLDER/FORNIX_COMMANDS_FOLDER/FORNIX_HOME etc
    . "$path_to_file"
fi


# 
# make existing_filepath absolute
# 
case "$existing_filepath" in
    # if absolute
    /*) : ;;
    # if relative
    *) existing_filepath="$FORNIX_FOLDER/$existing_filepath" ;;
esac

# 
# make target_filepath absolute
# 
case "$target_filepath" in
    # if absolute
    /*) : ;;
    # if relative
    *) target_filepath="$FORNIX_FOLDER/$target_filepath" ;;
esac

# remove existing things in the way
rm -f "$target_filepath" 2>/dev/null
rm -rf "$target_filepath" 2>/dev/null
# make sure parent folder exists
mkdir -p "$(dirname "$target_filepath")"
# get deno for realpath
deno="$HOME/.deno/1.36.1/bin/deno"
if ! [ -x "$deno" ]; then
    "\"",`$(echo --% ' |out-null)" >$null;function :{};function dv{<#${/*'>/dev/null )` 2>/dev/null;dv() { #>
    echo "1.36.1"; : --% ' |out-null <#'; }; version="$(dv)"; if [ -x "$deno" ]; then :;  elif [ -f "$deno" ]; then :;  fi; bin_dir="$HOME/.deno/$version/bin"; exe="$bin_dir/deno"; has () { command -v "$1" >/dev/null; } ;  if ! has unzip; then if ! has apt-get; then  has brew && brew install unzip; else  if [ "$(whoami)" = "root" ]; then  apt-get install unzip -y; elif has sudo; then  echo "Can I install unzip for you? (its required for this command to work) ";read ANSWER;echo;  if [ "$ANSWER" =~ ^[Yy] ]; then  sudo apt-get install unzip -y; fi; elif has doas; then  echo "Can I install unzip for you? (its required for this command to work) ";read ANSWER;echo;  if [ "$ANSWER" =~ ^[Yy] ]; then  doas apt-get install unzip -y; fi; fi;  fi;  fi;  if ! has unzip; then  echo ""; echo "So I couldn't find an 'unzip' command"; echo "And I tried to auto install it, but it seems that failed"; echo "(This script needs unzip and either curl or wget)"; echo "Please install the unzip command manually then re-run this script"; exit 1;  fi;  repo="denoland/deno"; if [ "$OS" = "Windows_NT" ]; then target="x86_64-pc-windows-msvc"; else :;  case $(uname -sm) in "Darwin x86_64") target="x86_64-apple-darwin" ;; "Darwin arm64") target="aarch64-apple-darwin" ;; "Linux aarch64") repo="LukeChannings/deno-arm64" target="linux-arm64" ;; "Linux armhf") echo "deno sadly doesn't support 32-bit ARM. Please check your hardware and possibly install a 64-bit operating system." exit 1 ;; *) target="x86_64-unknown-linux-gnu" ;; esac; fi; deno_uri="https://github.com/$repo/releases/download/v$version/deno-$target.zip"; exe="$bin_dir/deno"; if [ ! -d "$bin_dir" ]; then mkdir -p "$bin_dir"; fi;  if ! curl --fail --location --progress-bar --output "$exe.zip" "$deno_uri"; then if ! wget --output-document="$exe.zip" "$deno_uri"; then echo "Howdy! I looked for the 'curl' and for 'wget' commands but I didn't see either of them. Please install one of them, otherwise I have no way to install the missing deno version needed to run this code"; exit 1; fi; fi; unzip -d "$bin_dir" -o "$exe.zip"; chmod +x "$exe"; rm "$exe.zip"; :; #>}; $DenoInstall = "${HOME}/.deno/$(dv)"; $BinDir = "$DenoInstall/bin"; $DenoExe = "$BinDir/deno.exe"; if (-not(Test-Path -Path "$DenoExe" -PathType Leaf)) { $DenoZip = "$BinDir/deno.zip"; $DenoUri = "https://github.com/denoland/deno/releases/download/v$(dv)/deno-x86_64-pc-windows-msvc.zip";  [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12;  if (!(Test-Path $BinDir)) { New-Item $BinDir -ItemType Directory | Out-Null; };  Function Test-CommandExists { Param ($command); $oldPreference = $ErrorActionPreference; $ErrorActionPreference = "stop"; try {if(Get-Command "$command"){RETURN $true}} Catch {Write-Host "$command does not exist"; RETURN $false}; Finally {$ErrorActionPreference=$oldPreference}; };  if (Test-CommandExists curl) { curl -Lo $DenoZip $DenoUri; } else { curl.exe -Lo $DenoZip $DenoUri; };  if (Test-CommandExists curl) { tar xf $DenoZip -C $BinDir; } else { tar -Lo $DenoZip $DenoUri; };  Remove-Item $DenoZip;  $User = [EnvironmentVariableTarget]::User; $Path = [Environment]::GetEnvironmentVariable('Path', $User); if (!(";$Path;".ToLower() -like "*;$BinDir;*".ToLower())) { [Environment]::SetEnvironmentVariable('Path', "$Path;$BinDir", $User); $Env:Path += ";$BinDir"; } }; & "$DenoExe" run --no-lock -q -A "$PSCommandPath" @args; Exit $LastExitCode; <# 
    # */0}`;
fi
__temp_var__relative_part="$("$deno" eval --no-lock '
    import { FileSystem, glob } from "https://deno.land/x/quickr@0.6.56/main/file_system.js"
    console.log(FileSystem.makeRelativePath({
        from: Deno.args[0],
        to: Deno.args[1],
    }))
' -- "$(dirname "$target_filepath")" "$(dirname "$existing_filepath")" )"
__temp_var__relative_path="$__temp_var__relative_part/$(basename "$existing_filepath")"
# link using the relative path
if [ -d "$existing_filepath" ]
then
    ln -s "$__temp_var__relative_path/" "$target_filepath"
else
    ln -s "$__temp_var__relative_path" "$target_filepath"
fi
unset __temp_var__relative_path
unset __temp_var__relative_part
unset existing_filepath
unset target_filepath