#! /usr/bin/env bash

symlink_recursive_copy() {
    local src_dir=$1 ; shift
    local target_dir=$1
    local basename
    local cdir
    local full_path_cdir
    local full_path_target_dir
    local opwd=$PWD

    if [ -e "$target_dir" ]
    then
        >&2 echo "$target_dir already exists"
        return 1
    fi

    target_dir="$opwd/${target_dir##*/}"

    cd "$src_dir"

    while IFS= read -r -d '' file
    do
        basename="${file##*/}"
        cdir="${file%/*}"
        cd "$cdir" ; full_path_cdir=$PWD ; cd "$OLDPWD"
        mkdir -p "$target_dir/$cdir"
        ln -s "$full_path_cdir/$basename" "$target_dir/$cdir/$basename"
    done < <( find . -type f -print0 )

    cd "$opwd"
}

symlink_recursive_copy "$@"
