Initial commit
[hashcat.git] / extra / tab_completion / install
1 #!/bin/bash
2 # Programmable bash completion for oclHashcat / cudaHashcat
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}/oclHashcat64.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    sudo ${BASH_SOURCE[0]} ${was_sourced}
98
99   ret=${?}
100
101   if [ "${ret}" -eq 0 ]; then
102
103     source_completion
104
105   fi
106
107   my_exit ${ret}
108   return ${?}
109
110 fi
111
112 if [ -n "${1}"  ]
113 then
114
115   parent_sourced=${1}
116   is_child=1
117
118 fi
119
120 if [ -f "${COMPGENFOLDER}" ]
121 then
122
123   echo "The bash completion script file (${COMPGENSCRIPT}) could not be found"
124   echo "Please make sure that the distro 'bash-completion' package is installed (apt-get install it otherwise). EXIT"
125   my_exit 1
126   return ${?}
127
128 fi
129
130 if [ -d "${COMPGENFOLDER}" ]; then
131
132   # copy the script to target folder
133
134   cp ${ROOT}/oclHashcat64.sh "${COMPGENTARGET}"
135
136   # adjust paths to the main binaries of oclHashcat
137
138   sed -ri "s!^(ROOT=).*!\1\"${ROOT_PARENT}\"!" "${COMPGENTARGET}"
139
140
141   # add the compgen to bashrc if not already there
142
143   if ! egrep -q "^[^#]*\. *${COMPGENSCRIPT}" "${BASHRC}"; then
144
145     cat >> "${BASHRC}" << EOF
146
147 if [ -f "${COMPGENSCRIPT}" ]; then
148
149   . ${COMPGENSCRIPT}
150
151 fi
152
153 EOF
154
155   fi
156
157   if source_completion; then
158
159     echo "Bash completion scripts for oclHashcat were successfully installed, but since you didn't 'source' this file, you need to run:"
160     echo "source ${COMPGENTARGET} # or source ${BASHRC}"
161     echo
162     echo "in order to be able to use the tab completion within the current shell."
163
164   fi
165
166 else
167
168   echo "The compgen script folder (${COMPGENFOLDER}) could NOT be found. EXIT"
169   my_exit 1
170   return ${?}
171
172 fi
173
174 my_exit 0
175 return ${?}