#!/bin/sh

[ "$1" = "-v" ] && verbose=-v && shift

src=$1
dst=${2:-.}

usage()
{
	echo "Usage: $(basename $0) SRC_DIR [DST_DIR]"
	echo
	exit 1
}

linkdir()
{
	local odir="$PWD"
	[ -d "$2" ] || mkdir -p "$2"
	cd "$2"
	local d="$PWD"
	cd "$odir" && cd "$1"
	for f in * .*; do
		if [ "$f" != "." -a "$f" != ".." -a \( -e "$f" -o -L "$f" \) ]; then
			if [ -d "$f" -a ! -L "$f" ]; then
				linkdir "$f" "$d/$f"
			else
				ln $verbose "$f" "$d"
			fi
		fi
	done
	cd "$odir"
}

[ -z "$src" ] && usage

[ ! -d "$src" ] && echo "$src is not a directory" && exit 2

echo "$src" | grep -q /$ || dst=$dst/$(basename "$src")

linkdir "$src" "$dst"
