]> git.xonotic.org Git - xonotic/xonotic.git/blob - misc/tools/symlink-deduplicate.sh
Fix macOS SDL2 framework permissions
[xonotic/xonotic.git] / misc / tools / symlink-deduplicate.sh
1 #!/bin/sh
2
3 use_magnet_to_acquire_checksum_faster()
4 #             ___________________
5 #        ,--'' ~~~~~~~^^^~._     '.
6 #    ,.-' ~~~~~~~~~~^^^^^~~~._._   \
7 #    |   /^^^^^|    /^^^^^^^^\\ \   \
8 #  ,/___  <  o>      <  (OO) > _     \
9 # /'/,         |-         .       ----.\
10 # |(|-'^^;,-  ,|     __    ^~~^^^^^^^; |\
11 # \\`  |    <;_    __ |`---  ..-^^/- | ||
12 #  \`-|Oq-.____`________~='^^|__,/  ' //
13 #   \ || | |   |  |    \ ..-;|  /    '/
14 #   | ||#|#|the|==|game!|'^` |/'    /'
15 #   | \\\\^\***|***|    \ ,,;'     /
16 #   |  `-=\_\__\___\__..-' ,.- - ,/
17 #   | . `-_  ------   _,-'^-'^,-'
18 #   | `-._________..--''^,-''^
19 #   \             ,...-'^
20 #    `----------'^              PROBLEM?
21 {
22         magnet=`GIT_DIR="$git_src_repo/.git" git ls-files -s "$1"`
23         if [ -n "$magnet" ]; then
24                 magnet=${magnet#* }
25                 magnet=${magnet%% *}
26                 echo "$magnet"
27         else
28                 git hash-object "$1"
29         fi
30 }
31
32 lastinfiles=
33 lastinfileshash=
34 acquire_checksum()
35 {
36         if [ x"$1/../$2" = x"$lastinfiles" ]; then
37                 _a_s=$lastinfileshash
38         else
39                 _a_e=false
40                 for _a_f in "$1" "$2"; do
41                         case "$_a_f" in
42                                 */background_l2.tga|*/background_ingame_l2.tga)
43                                         _a_e=true
44                                         ;;
45                         esac
46                 done
47                 if [ -n "$git_src_repo" ] && ! $_a_e; then
48                         _a_s=`use_magnet_to_acquire_checksum_faster "${1#./}"`
49                         if [ -n "$2" ]; then
50                                 _a_s=$_a_s`use_magnet_to_acquire_checksum_faster "${2#./}"`
51                         fi
52                 else
53                         _a_s=`git hash-object "$1"`
54                         if [ -n "$2" ]; then
55                                 _a_s=$_a_s`git hash-object "$2"`
56                         fi
57                 fi
58                 lastinfileshash=$_a_s
59                 lastinfiles="$1/../$2"
60         fi
61         echo "$_a_s"
62 }
63
64 make_relative_path()
65 {
66         from=$1
67         to=$2
68         pre=
69         post=
70         while :; do
71                 case "$from" in
72                         */*)
73                                 case "$to" in
74                                         */*)
75                                                 fromfirst=${from%%/*}
76                                                 fromrest=${from#*/}
77                                                 tofirst=${to%%/*}
78                                                 torest=${to#*/}
79                                                 if [ x"$fromfirst" = x"$tofirst" ]; then
80                                                         from=$fromrest
81                                                         to=$torest
82                                                 else
83                                                         to=$tofirst
84                                                         post=/$torest
85                                                         pre=../$pre
86                                                         from=$fromrest # now we can only hit the ../ path or the bottom one
87                                                 fi
88                                                 ;;
89                                         *)
90                                                 # from has path, to does not
91                                                 # we need a ../ component then try again
92                                                 pre=../$pre
93                                                 from=${from#*/}
94                                                 ;;
95                                 esac
96                                 ;;
97                         *)
98                                 echo "$pre$to$post"
99                                 break
100                                 ;;
101                 esac
102         done
103 }
104
105 killed=0
106 while IFS= read -r L; do
107         s=`acquire_checksum "$L"`
108         eval first=\$first_$s
109         if [ -n "$first" ]; then
110                 first_r=`make_relative_path "$L" "$first"`
111                 killed=$((`stat -c %s "$L"` + $killed))
112                 ln -vsnf "$first_r" "$L"
113         else
114                 eval first_$s=\$L
115         fi
116 done
117 echo "Killed $(($killed / 1048576)) MiB"