#!/bin/bash
# This script file does the same as mkSymbLink.exe for Windows.
# $1 is the link-path, its last element should be the symbolic link,
# given relative (usual) or absolute.
# $2 is the target-path.
# $3 is an option argument, admissible -F -L -T -FT -LT -TL
# (any combination of characters F, L, T).
#
# T means: create the target path if not existing, as empty directory.
# L means: remove an existing symbolic link for $1 and replace it.
# F means: remove a file or normal directory referred to by $1.
#          Never follow a symbolic link when removing a directory.
# F also means: remove a file on $2 and replace it with an empty directory.
#
# See https://vishia.org/Linux/html/mkSymbLink-de.html
# This software may be used, modified, and redistributed under the
# terms of the GNU Lesser General Public License (LGPL).
#
# Author: Dr. Hartmut Schorrig, Germany
# Note: This file was developed with assistance from ChatGPT.
# @date 2026-11-10: modifications by Hartmut 
# @date 2026-11-09: created by ChatGPT
#
## On windows execute the Windows version should be found beside.
if test -d /c/Windows; then "$(dirname $0)/mkSymbLink.exe" "$1" "$2" "$3"; exit $?; fi  
###echo LINUX

LINK="$1"
TARGET="$2"
OPTS="${3-}"

## Check arguments
if [[ -z "$LINK" || -z "$TARGET" ]]; then
  echo "ERROR mkSymbLink: missing arguments"
  exit 8
fi

## Check option argument: leading '-' and only F, L, T
if [[ -n "$OPTS" && ! "$OPTS" =~ ^-[FLT]+$ ]]; then
  echo "ERROR mkSymbLink: faulty option $OPTS"
  exit 8
fi

FORCE=false
LINKREMOVE=false
TARGETCREATE=false

### only for test printf debug:
###echo $TARGETCREATE
###if $TARGETCREATE; then echo targetCreate = true; else echo targetCreate = false; fi
###if ! $TARGETCREATE; then echo targetCreateNo = false; else echo targetCreateNo = true; fi

[[ "$OPTS" == *F* ]] && FORCE=true
[[ "$OPTS" == *L* ]] && LINKREMOVE=true
[[ "$OPTS" == *T* ]] && TARGETCREATE=true

## The directory containing the link must already exist
LINKDIR="$(dirname -- "$LINK")"
if [[ ! -d "$LINKDIR" ]]; then
  echo "ERROR mkSymbLink: directory for >>$LINK<< does not exist"
  exit 1
fi

## Process target path $2
if [[ ! -d "$TARGET" ]]; then

  if [[ -e "$TARGET" || -L "$TARGET" ]]; then
    ## Something exists, but it is not a directory.
    ## F permits removing a file or a symbolic link here.
    if $FORCE; then
      if ! rm -f -- "$TARGET"; then
        echo "ERROR mkSymbLink: cannot remove target >>$TARGET<<"
        exit 1
      fi
    else
      echo "ERROR mkSymbLink: target >>$TARGET<< exists but is not a directory"
      exit 1
    fi
  elif ! $TARGETCREATE; then
    echo "ERROR mkSymbLink: target >>$TARGET<< does not exist (option T required)"
    exit 1
  fi

  ## Create the target directory, if T or F permits it.
  if ! mkdir -p -- "$TARGET"; then
    echo "ERROR mkSymbLink: cannot create target directory >>$TARGET<<"
    exit 1
  fi
fi

## Use only an absolute canonical path for the link target
DST="$(realpath -e -- "$TARGET")"
if [[ ! -d "$DST" ]]; then
  echo "ERROR mkSymbLink: >>$TARGET<< is not a directory"
  exit 1
fi

## Check whether the existing link is already correct
if [[ -L "$LINK" ]]; then
  if [[ "$(realpath -e -- "$LINK")" == "$DST" ]]; then
    echo "mkSymbLink: already OK"
    exit 0
  fi
fi

## Process existing path $1
if [[ -e "$LINK" || -L "$LINK" ]]; then

  if [[ -L "$LINK" ]]; then
    ## Existing symbolic link: remove only with option L.
    if $LINKREMOVE; then
      if ! rm -- "$LINK"; then
        echo "ERROR mkSymbLink: cannot remove symbolic link >>$LINK<<"
        exit 1
      fi
    else
      echo "ERROR mkSymbLink: >>$LINK<< is already a symbolic link (option L required)"
      exit 1
    fi

  elif [[ -d "$LINK" ]]; then
    ## Existing normal directory: remove recursively only with F.
    if $FORCE; then
      if [[ "$(realpath -e -- "$LINK")" == "$DST" ]]; then
        echo "ERROR mkSymbLink: link path and target path are identical"
        exit 1
      fi
      if ! rm -r -- "$LINK"; then
        echo "ERROR mkSymbLink: cannot remove directory >>$LINK<<"
        exit 1
      fi
    else
      echo "ERROR mkSymbLink: >>$LINK<< is a directory (option F required)"
      exit 1
    fi

  else
    ## Existing normal file: remove only with F.
    if $FORCE; then
      if ! rm -f -- "$LINK"; then
        echo "ERROR mkSymbLink: cannot remove file >>$LINK<<"
        exit 1
      fi
    else
      echo "ERROR mkSymbLink: >>$LINK<< is a file (option F required)"
      exit 1
    fi
  fi
fi

## Create the symbolic link
if ! ln -s -- "$DST" "$LINK"; then
  echo "ERROR mkSymbLink: ln -s $DST $LINK failed"
  exit 1
fi

## echo "mkSymbLink: created >>$LINK<< ==>>$DST<<"  ## activate only for test
exit 0
