#!/usr/bin/env nix-shell
#!nix-shell -i bash

set -e

cpto="$1"
cpfrom="$2"

cpOverwrite() {
  # -f flag in case the destination perms were set to 000
  # --no-dereference in case the destination already exists as a symlink
  # however, "no-dereference" has the edge case of copying `cpfrom` to `cpto`
  #   when `cpto` already exists as a symlink to `cpfom`:
  #   "cp: <cpto> and <cpfrom> are the same file"
  # use `--remove-destination` for that
  cp --no-dereference -f "$cpfrom" "$cpto" \
    || cp --no-dereference --remove-destination "$cpfrom" "$cpto"
}

cpNoOverwrite() {
  # --no-dereference in case the destination already exists as a symlink.
  #                  it will still follow directory-level intermediate symlinks: just not a leaf symlink
  cp --no-clobber --no-dereference "$cpfrom" "$cpto" || test -f "$cpto"
}

cpNoOverwrite
