-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymlndotf.sh
executable file
·80 lines (66 loc) · 2.49 KB
/
symlndotf.sh
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
#!/usr/bin/env bash
# ---
# SYMLiNk DOTFiles (symlndotf)
#
# This script will create symlinks of the specified file in the same relative location while backing up.
# $DotfileDir/path/to/new_dotfile --> symlndotf.sh
# ==> $HomeDir/path/to/old_dotfile --moved--> $BackupDir/path/to/old_dotfile_timestamp
# ==> $HomeDir/path/to/new_dotfile --symlink--> $DotfileDir/path/to/new_dotfile
#
# ...where by default:
# HomeDir=$HOME
# BackupDir="$HomeDir/.dotfiles.backups"
# CurrentDir=$(pwd -P)
# DotfileDir="$HomeDir/gitRepos/dotfiles2.0"
set -e # Stop script on any error
if [[ $SUDO_USER ]]; then
echo "Running scripts you find on the internet as root is dangerous. Try again without 'sudo'."
exit 1
fi
HomeDir=$(readlink -f $HOME)
BackupDir="$HomeDir/.dotfiles.backups"
CurrentDir=$(pwd -P)
DotfileDir="$HomeDir/gitRepos/dotfiles2.0"
backup_dotfile() {
if [[ ! -e $BackupDir ]]; then
echo "Creating backups folder $BackupDir..."
mkdir $BackupDir
fi
inputabs=$(readlink -f $1)
# get input relative to the dotfile directory via string subtraction
filepath="${inputabs#"$DotfileDir"}"
filedirpath=$(dirname $filepath)
if [[ -e "$HomeDir/$filepath" ]]; then
echo " $filepath already exists. Backing up to $BackupDir"
mkdir -p $BackupDir/$filedirpath
datesuffix=$(date '+%Y%m%d%H%M%S')
# Copy the file while removing symlinks if present
cp -L $HomeDir/$filepath $BackupDir/${filepath}_$datesuffix && rm $HomeDir/$filepath
echo " $(basename $filepath) backed up to $BackupDir/${filepath}_$datesuffix"
fi
}
symlink_dotfile() {
filepath=$(realpath -s --relative-to=$DotfileDir $1)
filedirpath=$(dirname $filepath)
filename=$(basename $filepath)
# Get actual file path (if symlink, otherwise it's the same)
filepath_expanded=$(realpath --relative-to=$DotfileDir $1)
mkdir -p $HomeDir/$filedirpath && ln -s $DotfileDir/$filepath_expanded $HomeDir/$filepath
[ -L "${HomeDir}/${filepath}" ] && echo "SUCCESS SYMLINKING $1"
}
declare -a dotfilepaths
# Loop to:
# A) Expand directories into individual files in them
# B) Only allow regular files and symlinks to be passed through
for input in $@;
do
[ -d $input ] && dotfilepaths+=($(find $input -type f))
[ -f $input ] && dotfilepaths+=($input)
[ -L $input ] && dotfilepaths+=($input)
done
for dotfilepath in "${dotfilepaths[@]}";
do
echo WORKING ON $dotfilepath '............'
backup_dotfile $dotfilepath
symlink_dotfile $dotfilepath
done