-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sht
138 lines (117 loc) · 2.47 KB
/
install.sht
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#%shebang
#%version 2
#
# Installation script
#
#%prolog
#%include sys/msg
#%include sys/cmd
#%include sys/checkdir
#%include sys/checkvar
#%include sys/copyfile
################
# Set variables
SRCDIR="$(cd -- "$(dirname -- "$0")" ; pwd)"
DSTDIR="${HOME}"
: "${XDG_DATA_HOME:=${HOME}/.local/share}"
BCKDIR="${XDG_DATA_HOME}/spxshell/backup"
BACKUP="no" # No backup by default
COPYFILES="
dot.bashrc
dot.exrc
dot.inputrc
dot.profile
dot.vimrc
"
LINKFILES="
dot.bashrc:dot.kshrc
"
REMOVEFILES="
dot.bash_profile
dot.bash_login
"
####
# Initialization subroutine
startup ()
{
# Check variables
{
check_var SRCDIR &&
check_var DSTDIR
} || return 1
# Check destination directory
check_dir "${DSTDIR}" || return 1
# Backup destination files only for the first time
# and if the backup directory is successfully created
if test -d "${BCKDIR}" ; then
BACKUP="no"
else
# Try to create directory
( umask 0077 ; cmd mkdir -p "${BCKDIR}" )
test $? -ne 0 && BACKUP="no" || BACKUP="yes"
fi
}
# Copy files
copy_files ()
{
local _f=""
local _src=""
local _dst=""
for _f in ${COPYFILES} ; do
_src="${SRCDIR}/${_f}"
_dst="${DSTDIR}/${_f#dot}"
# Try to backup destination file
# Ignore unsuccessful copying
test "${BACKUP}" = "yes" && test -f "${_dst}" && cmd cp "${_dst}" "${BCKDIR}"
copy_file "${_src}" "${_dst}"
done
}
# Link files
link_files ()
{
local _f=""
local _src=""
local _dst=""
for _f in ${LINKFILES} ; do
_src="${_f%:*}"
test -n "${_src}" && test "${_src}" != "${_f}" && _src="${_src#dot}" || continue
_dst="${_f#*:}"
test -n "${_dst}" && test "${_dst}" != "${_f}" && _dst="${DSTDIR}/${_dst#dot}" || continue
# Skip if the link exists
test -L "${_dst}" && continue
# Try to backup file
test "${BACKUP}" = "yes" && test -f "${_dst}" && cmd cp "${_dst}" "${BCKDIR}"
cmd ln -sf "${_src}" "${_dst}"
if test $? -ne 0 ; then
err "can't create symbolic link '${_src}' -> '${_dst}'"
fi
done
}
# Remove files
remove_files ()
{
local _f=""
local _dst=""
for _f in ${REMOVEFILES} ; do
_dst="${DSTDIR}/${_f#dot}"
if test -f "${_dst}" ; then
# Try to backup file
test "${BACKUP}" = "yes" && cmd cp "${_dst}" "${BCKDIR}"
# Try to remove file
cmd rm -f "${_dst}"
if test $? -ne 0 ; then
err "can't delete file '${_dst}'"
fi
fi
done
}
# Main subroutine
main ()
{
startup || return 1
copy_files
link_files
remove_files
}
# Call main subroutine
main "$@"