summaryrefslogtreecommitdiff
path: root/bin/lt
blob: 21afaf051d79b8c6c52df2f86a37a8528bff4aa6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/bin/sh

# link tree

lt_version='lt-0.1'
unset CDPATH
export LC_ALL=C IFS='   
'

# ca returns the common ancestor between 2 paths
# ca /h/u/a/b /h/u/c/d => /h/u
ca() {
	ca_p1=${1#/} ca_p2=${2#/} ca_r1= ca_r2= R1=/
	while true; do
		[ "$ca_p1" ] || [ "$ca_p2" ] || break
		ca_r1=$ca_r1/${ca_p1%%/*} ca_p1=${ca_p1#*/}
		ca_r2=$ca_r2/${ca_p2%%/*} ca_p2=${ca_p2#*/}
		[ "$ca_r1" != "$ca_r2" ] && break
		R1=$ca_r1
	done
}

#echo ca $1 $2 && ca $1 $2 && echo "$R1"

# bp returns the relative path from source to base
bp() {
	bp_s=$1 bp_b=$2 R1=
	while true; do
		[ "$bp_b" = "$bp_s" ] && break
		bp_s=${bp_s%/*}
		[ "$R1" ] && R1=$R1/.. || R1=..
	done
}

#echo bp /h/u/a /h/u && bp /h/u/a /h/u && echo "$R1"

# rp returns the relative path between src and base
# rp /h/u/a/b/f /h/u/b/f [/h/u] ../a/b/f
rp() {
	rp_s=$1 rp_d=$2 rp_c=$3
	! [ "$rp_c" ] && ca "$1" "$2" && rp_c=$R1
	rp_bd=${rp_d#$rp_c/}
	rp_bs=${rp_s#$rp_c/}
	bp "$rp_d" "$rp_c" && R1=$R1/$rp_bs && R1=${R1#../}
}

#rp /h/u/a/b /h/u/b && echo $R1

skip=".git .gitignore .*.swp Makefile"

# link tree src dest
lt() {
	d=$(mkdir -p "$2" && cd "$2" && pwd)
	cd "$1" && s=$PWD
	find . ! -type d -print | {
		cd "$d"
		while read f; do
			for k in $skip; do
				case $f in (./$k|./$k/*) continue 2;; esac
			done
			f=${f#./}
			fd=$d/$f; fd=${fd%/*}
			test -d "$fd" || $trace mkdir -p "$fd"
			rp "$s/$f" "$d/$f" && $trace ln -sf "$R1" "$d/$f"
		done
	}
}

#trace=echo
lt "$1" "$2"

#echo $1 $2 $3 && $1 $2 $3 && echo $R1