#!/bin/bash

if [ -z $1 ] || [ -z $2 ]; then
	echo Usage $0 SOURCE_FOLDER DESTINATION_FOLDER
	echo Note that SOURCE_FOLDER must be relative path to a directory and have a trailing slash
	echo EXAMPLE: ./copy_folder_as_rel_links.sh ./DEFAULT/V6.2/ ./GLI11
	echo This will make a copy of ./DEFAULT/V6.2 into ./GLI11, by creating a folder V6.2 into GLI11, but files with relative symilinks

exit 1
fi


#this will make a copy of V6.1 directory into folder DEFAULT, everything with symilinks
#Note that SOURCE must be an absolute path and have a trailing slash

#copy source to destination with abosolute sym-links
cp -as "$(pwd)/$1/" $2/

#we have to convert absolute sym-links into relative, so that they work with FTP server
find $2 -type l | while read l; do
    target="$(realpath "$l")"
    ln -fs "$(realpath --relative-to="$(dirname "$(realpath -s "$l")")" "$target")" "$l"
#    ln -fs "$(realpath --relative-to="./V6.1/" "$target")" "$l"
done

chown ftpuser:ftpgroup $2 -R
