]> CyberLeo.Net >> Repos - CDN/bash-config.git/blob - share/fixskel
Add fixskel script to handle bash-config symlinks in /usr/share/skel
[CDN/bash-config.git] / share / fixskel
1 #!/bin/sh
2
3 pebkac() {
4   [ "${*}" ] && printf "%s\n\n" "${*}"
5   cat <<EOF
6 Usage: fixskel [-c|-i|-r]
7
8  -c    Check symlinks for sanity
9  -i    Install symlinks
10  -r    Remove symlinks
11
12 This script will adjust the bash-config symlinks in /usr/share/skel
13 so that new users will be set up automatically. This is a companion
14 script to 'fixusers', which will set up existing users.
15
16 This script will avoid tampering with files that it thinks are
17 manually added. Use -c to check, and verify the files manually
18 afterwards.
19
20 EOF
21   exit "${#}"
22 }
23
24 files="bash_profile bashrc"
25
26 mine() {
27   [ "${#}" -eq 0 ] && set - ${files}
28   while [ "${1}" ]
29   do
30     file="${1}"
31     skel="/usr/share/skel/dot.${file}"
32     real="/usr/local/etc/bash-config/${file}"
33     if [ ! -L "${skel}" ] || [ "$(realpath "$(readlink "${skel}")")" != "${real}" ]
34     then
35       return 1
36     fi
37     shift
38   done
39   return 0
40 }
41
42 gone() {
43   [ "${#}" -eq 0 ] && set - ${files}
44   while [ "${1}" ]
45   do
46     file="${1}"
47     skel="/usr/share/skel/dot.${file}"
48     [ -f "${skel}" ] && return 1
49     shift
50   done
51   return 0
52 }
53
54 check() {
55   [ "${#}" -eq 0 ] && set - ${files}
56   while [ "${1}" ]
57   do
58     file="${1}"
59     if gone "${file}"
60     then
61       echo "File ${file} does not exist."
62     else
63       if mine "${file}"
64       then
65         echo "File ${file} is mine."
66       else
67         echo "File ${file} exists, but is not mine."
68       fi
69     fi
70     shift
71   done
72 }
73
74 install() {
75   if mine
76   then
77     echo "Skel symlinks appear to be correct"
78     return 0
79   fi
80   
81   if ! gone
82   then
83     echo "Skel files (${files}) appear to exist, and are not mine"
84     echo "Refusing to molest them"
85     return 1
86   fi
87   
88   for file in ${files}
89   do
90     skel="/usr/share/skel/dot.${file}"
91     real="/usr/local/etc/bash-config/${file}"
92     ln -s "${real}" "${skel}" || return $?
93   done
94   return 0
95 }
96
97 remove() {
98   if gone
99   then
100     echo "Skel symlinks already removed"
101     return 0
102   fi
103   
104   if ! mine
105   then
106     echo "Skel files (${files}) appear to exist, and are not mine"
107     echo "Refusing to molest them"
108     return 1
109   fi
110   
111   for file in ${files}
112   do
113     skel="/usr/share/skel/dot.${file}"
114     rm -f "${skel}" || return $?
115   done
116   return 0
117 }
118
119 while getopts "cir" opt
120 do
121   case "${opt}" in
122   c)  check; exit $? ;;
123   i)  install; exit $? ;;
124   r)  remove; exit $? ;;
125   [?]) pebkac "Unrecognized option ${OPTARG}" ;;
126   esac
127 done
128
129 pebkac