-
Notifications
You must be signed in to change notification settings - Fork 1
/
symlink_userdirs
executable file
·99 lines (89 loc) · 3.03 KB
/
symlink_userdirs
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env bash
# Copyright (C) 2021 Rodrigo Silva (MestreLion) <[email protected]>
# License: GPLv3 or later. See <http://www.gnu.org/licenses/gpl.html>
# -----------------------------------------------------------------------------
DESCRIPTION='Move special user dirs, replacing with symlinks to data partition'
ARGS='[TARGET_BASEDIR]'
setuplib=${SETUP_LIB_PATH:-"$(dirname "$(readlink -f "$0")")"/../setuplib}
# shellcheck source=../setuplib
if [[ -r "$setuplib" ]]; then source "$setuplib"; else
echo "Setup library not found: $setuplib" >&2; exit 1;
fi
# -----------------------------------------------------------------------------
basedir=${1:-${SETUP_USERDIRS_TARGET:-}}
if ! [[ -v SETUP_USERDIRS_OVERRIDES[@] ]]; then declare -gA SETUP_USERDIRS_OVERRIDES=(); fi
if ! [[ -v SETUP_USERDIRS_HIDE ]]; then SETUP_USERDIRS_HIDE=(TEMPLATES PUBLICSHARE); fi
# -----------------------------------------------------------------------------
merge_dirs() {
local tag=$1
local source; source=$(xdg-user-dir "$tag")
local rsync_opts=()
# Skip if it does not exist, or already a symlink, or set to $HOME
if [[ ! -e "$source" || -h "$source" || "$source" == "$HOME" ]]; then
return
fi
local old=${source}.${SETUP_SLUG}.$(now).old # almost the same as $(bakfile)
local target; target=$(
cd "$HOME" &&
realpath --canonicalize-missing --relative-base "$HOME" -- \
"${SETUP_USERDIRS_OVERRIDES[$tag]:-$source}"
)
if ! [[ "${target:0:1}" == '/' ]]; then
target=${basedir%/}/${target}
fi
if [[ "$target" == "$source" ]]; then
return
fi
message "Move content and symlink: $source -> $target"
setup_run mkdir -p -- "$target"
if ! [[ "$(df --output=fstype "$target" | tail -n -1)" == ext4 ]]; then
# Disable permission setting for non-EXT4 partitions (FAT/NTFS/etc)
rsync_opts+=(--no-p --no-g --omit-dir-times)
fi
setup_run rsync -qaSh "${rsync_opts[@]}" --progress --remove-source-files -- \
"${source%/}/" "${target%/}/"
setup_run find "$source" -type d -empty -delete
if [[ -e "$source" ]]; then
mv -- "$source" "$old"
fi
setup_run ln -s -- "$target" "$source"
}
if [[ "$basedir" ]]; then
for tag in DOCUMENTS DOWNLOAD MUSIC PICTURES VIDEOS; do
merge_dirs "$tag"
done
fi
# Hide templates, publicshare, examples
declare -A created
hide_path() {
local path=$1
local path_file; path_file=$(basename -- "$path")
local path_dir; path_dir=$( dirname -- "$path")
local hidden=${path_dir}/.hidden
if [[ -e "$path" ]] && ! grep -qF "$path_file" "$hidden" 2>/dev/null; then
message "Hide $(basename "$path")"
if ! [[ -e "$hidden" ]]; then
created["$hidden"]=1
fi
if ! [[ "${created["$hidden"]:-}" ]]; then
backup_file "$hidden"
fi
echo "$path_file" | setup_run tee -a "$hidden"
fi
}
for tag in "${SETUP_USERDIRS_HIDE[@]}"; do
path=$(xdg-user-dir "$tag")
if [[ "$path" == "$HOME" ]]; then
continue
fi
hide_path "$path"
done
paths=(
"$HOME/examples.desktop" # Not in Ubuntu 20.04 anymore
"$HOME/snap"
"$basedir/\$RECYCLE.BIN"
"$basedir/System Volume Information"
)
for path in "${paths[@]}"; do
hide_path "$path"
done