-
Notifications
You must be signed in to change notification settings - Fork 0
/
import
executable file
·75 lines (68 loc) · 2.25 KB
/
import
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
#!/bin/bash
## import settings files from the repo to this computer, without linking
# imported files will have '.' prepended to their filename
## Usage:
## ./import
# imports all files from "src"
## ./import list of files
# imports files "list", "of" and "files", if they exist in "src"
# cannot handle paths to files
# ignores leading '.'s in filenames, as no source files may be hidden
# destination for files
dest="$HOME"
# path to repo from ${dest}
repo="config"
# source directory for copied files, relative to ${repo}
src="src"
# files to be copied
files="$(
if [ "$#" -le 0 ] ; # no arguments
then
ls -1 "${src}" # all the files in source directory
else
IFS=$'\n'; echo "$*" # all the files listed in the command line args
fi
)"
# don't do anything if there's nothing to do
if [ -z "${files}" ];
then
echo "no files to import, exiting"
else
# files which would be overwritten
overwrites="$(
echo "${files}"| # for each file
while IFS='' read file;
do
ls -1a "${dest}" | grep "^.${file}$" # file exists in destination directory, as a hidden file
done
)"
if [ -n "${overwrites}" ];
then
echo "This action will overwrite the following files:"
echo "${overwrites}"|
sed "s:^:${dest}/:" # format it nicely with the path
fi
echo -n "Are you sure? (y/N): "
read confirm
if ! [ -t 0 ] ;
then # open in a pipe
# simulate user input
echo "${confirm}"
fi
if echo "${confirm}"|grep -Eq "[Yy]+([Ee]+[Ss]+)*" ;
then # they tried to say yes
echo "${files}"| # for each file
while IFS='' read file;
do
if [ -e "${src}/${file}" ];
then # source file exists
cp -frvT --remove-destination "${dest}/${repo}/${src}/${file}" "${dest}/.${file}" # copy, overwriting, recursive, verbose
else # file doesn't exist
echo "Warning: cannot copy nonexistent file \"${src}/${file}\"" # don't copy nothing
fi
done
else # they said something else; assume no
echo "aborting" # let the user know nothing happened
exit 1
fi
fi