Fix m 60 a 0 by making modified variable non-const
[hashcat.git] / extra / tab_completion / install
1 #!/bin/bash
2 # Programmable bash completion for hashcat
3 # this script was tested under ubuntu, please verify if on your
4 # distro /etc/bash_completion.d/ exists (otherwise it won't work)
5
6 COMPGENSCRIPT=/etc/bash_completion
7 COMPGENFOLDER=${COMPGENSCRIPT}.d
8 COMPGENTARGET=${COMPGENFOLDER}/hashcat.sh
9 BASHRC=~/.bashrc
10 ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
11
12 #############################################################################
13
14 is_sourced ()
15 {
16   if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
17
18     SOURCED=0
19
20   else
21
22     SOURCED=1
23
24   fi
25
26   return ${SOURCED}
27 }
28
29 source_completion ()
30 {
31   # load the completion into current shell
32
33   if [ "${is_child}" -eq 1 ]; then
34
35     if [ "${parent_sourced}" -eq 0 ]; then
36
37       return 1
38
39     fi
40
41   fi
42
43   if [ "${was_sourced}" -eq 0 ]; then
44
45     source "${COMPGENTARGET}"
46     # or
47     #source ${BASHRC}
48
49     return 1
50
51   fi
52
53   return 0
54 }
55
56 my_exit ()
57 {
58   if [ "${was_sourced}" -eq 0 ]; then
59
60     return $1
61
62   else
63
64     exit $1
65
66   fi
67 }
68
69 is_root ()
70 {
71   if [ "$(id -g)" -eq 0 ]; then
72
73     return 0
74
75   else
76
77     return 1
78
79   fi
80 }
81
82
83 #############################################################################
84
85 is_sourced
86 was_sourced=${?}
87
88 ROOT_PARENT="$(cd ${ROOT}/.. && pwd)"
89
90 # Check (install) permissions
91
92 parent_sourced=0
93 is_child=0
94
95 if ! is_root; then
96
97   echo "Warning: root permissions are required to install the tab completion script into the protected '${COMPGENFOLDER}' folder"
98
99   sudo ${BASH_SOURCE[0]} ${was_sourced}
100
101   ret=${?}
102
103   if [ "${ret}" -eq 0 ]; then
104
105     source_completion
106
107   fi
108
109   my_exit ${ret}
110   return ${?}
111
112 fi
113
114 if [ -n "${1}"  ]
115 then
116
117   parent_sourced=${1}
118   is_child=1
119
120 fi
121
122 if [ -f "${COMPGENFOLDER}" ]
123 then
124
125   echo "The bash completion script file (${COMPGENSCRIPT}) could not be found"
126   echo "Please make sure that the distro 'bash-completion' package is installed (apt-get install it otherwise). EXIT"
127   my_exit 1
128   return ${?}
129
130 fi
131
132 if [ -d "${COMPGENFOLDER}" ]; then
133
134   # remove the old version of hashcat64.sh (was renamed to just hashcat.sh)
135
136   rm -f "${COMPGENTARGET}"/hashcat64.sh
137
138   # copy the script to target folder
139
140   cp ${ROOT}/hashcat.sh "${COMPGENTARGET}"
141
142   # adjust paths to the main binaries of hashcat
143
144   sed -ri "s!^(ROOT=).*!\1\"${ROOT_PARENT}\"!" "${COMPGENTARGET}"
145
146
147   # add the compgen to bashrc if not already there
148
149   if ! egrep -q "^[^#]*\. *${COMPGENSCRIPT}" "${BASHRC}"; then
150
151     cat >> "${BASHRC}" << EOF
152
153 if [ -f "${COMPGENSCRIPT}" ]; then
154
155   . ${COMPGENSCRIPT}
156
157 fi
158
159 EOF
160
161   fi
162
163   if source_completion; then
164
165     echo "Bash completion scripts for hashcat were successfully installed, but since you didn't 'source' this file, you need to run:"
166     echo "source ${COMPGENTARGET} # or source ${BASHRC}"
167     echo
168     echo "in order to be able to use the tab completion within the current shell."
169
170   fi
171
172 else
173
174   echo "The compgen script folder (${COMPGENFOLDER}) could NOT be found. EXIT"
175   my_exit 1
176   return ${?}
177
178 fi
179
180 my_exit 0
181 return ${?}