-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathefc_lib
executable file
·187 lines (152 loc) · 5.33 KB
/
efc_lib
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#!/bin/bash
# efc_lib [ encrypt / decrypt files.]
# MIT license (MIT)
# functions using the gpg lib to symmetrically encrypt / decrypt individual file.
shopt -s globstar
decrypt() {
# $1 = fq file name
# $2 = password
# $3 = delete flag (yes/no)
# $4 = output file name [optional]
echo -e ' \t' "decrypting the file: $1"
local return_status=0
if [ -z "${4}" ]; then
echo -e "\t decrypting the file : $1"
gpg --quiet --yes --batch --passphrase "$2" "$1" 2>>"$1_out"
else
echo -e "\t decrypting the file : $1 -> $4"
gpg --quiet --yes --batch --output "$4" --passphrase "$2" "$1" 2>>"$1_out"
fi
if [ -s "$1_out" ]; then
echo -e ' \t' "invalid key/file is provided!, file is ignored!"
else
if [ $3 == 'yes' ]; then
echo -e ' \t' "removing the original file $1"
rm -rf "$1"
fi
return_status=1
fi
rm -rf "$1_out"
return $return_status
}
encrypt() {
# $1 = fq file name
# $2 = password
# $3 = delete flag (yes/no)
# $4 = output file name [optional]
if [ -z "${4}" ]; then
echo -e "\t encrypting the file : $1"
gpg --quiet --yes --batch --passphrase "$2" -c "$1"
else
echo -e "\t encrypting the file : $1 -> $4"
gpg --quiet --yes --batch --output "$4" --passphrase "$2" -c "$1"
fi
if [ $3 == 'yes' ]; then
echo -e ' \t' "removing the original file: $1"
rm -rf "$1"
fi
return 1
}
remove_zip_after_verify() {
echo "$1"
if ! tar tf "$1" &>/dev/null; then
echo -e "\toriginal DIR won't be deleted, error in archive : $1"
else
echo -e "\toriginal DIR will be removed! : $2"
rm -rf "$2"
fi
}
zip_all_folders() {
echo
find "$1" -print |
while read file; do
if [[ -d "$file" ]] && [[ "$file" != "$1" ]]; then
tar_name=$(basename "$file")
tar_fq_name="$1/$tar_name.tar.gz"
echo -e "Found : DIR $file , archiving as : $tar_fq_name"
tar -cvf "$tar_fq_name" "$file"
remove_zip_after_verify "$tar_fq_name" "$file"
fi
done
}
read_version() {
execution_path=$(dirname "$0")
while IFS= read -r line; do
if [[ "$line" == ???EFC* ]]; then
echo -e "\n$line"
break
fi
done <"$execution_path/efc"
}
print_version_info() {
read_version
echo -e "bash version : %s $BASH_VERSION"
echo -e "gpg version : " $(gpg --version | sed -n 1p)
echo -e "libgcrypt version : " $(gpg --version | sed -n 2p)
echo -e "tar version : " $(tar --version | sed -n 1p)
echo -e "\nThere is NO WARRANTY, to the extent permitted by law; licensed under MIT license (MIT)"
echo -e "Written by compilable."
}
# extract password from url
function read_from_url() {
url=$1
output_file=$(mktemp)
curl "$url" --output "$output_file" 2>/dev/null
line=$(head -n 1 $output_file)
rm -f $output_file
echo $line
}
# extract password from file
function read_from_file() {
if [[ -f "$1" ]]; then
password=$(head -n 1 $1)
echo $password
fi
}
# extract password
function extract_password() {
regex='(https?|http|ftp|file)://[-[:alnum:]\+&@#/%?=~_|!:,.;]*[-[:alnum:]\+&@#/%=~_|]'
if [[ $1 =~ $regex ]]; then
echo "[info] read passprase from link: $1"
password=$(read_from_url $1)
elif [[ -f "$1" ]]; then
echo "[info] read passprase from file: $1"
password=$(read_from_file $1)
fi
if [ -z "$password" ]; then
echo "[error] password can not be empty! make sure password file is accessible and not empty."
fi
}
# upgrade the current version to latest relased version.
upgrade_to_latest_from_remote() {
wget -q --spider http://github.com
if [ $? -eq 0 ]; then
echo "[info] obtaining the latest build from github repo."
# download the latest version to temp location
temp_location=$(mktemp -d -t efc-XXXXXXXXXX)
eval wget -q $(curl -s https://api.github.com/repos/compilable/efc/releases/latest | grep browser_download_url | cut -d : -f 2,3) -P $temp_location
tar_file=$(find $temp_location -maxdepth 1 -type f -iname "efc_*.tar.gz")
output_path=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
echo "[info] : efc installed location : $output_path"
if [ -f "$tar_file" ]; then
echo "[info] file : $tar_file found, extracting to $output_path"
mkdir -p "$output_path/.new"
tar -xvf $tar_file --directory "$output_path/.new"
# override with the latest version
echo '' > "$output_path/efc"
cat "$output_path/.new/bin/efc" > "$output_path/efc"
cat "$output_path/.new/bin/License" > "$output_path/License"
cat "$output_path/.new/bin/README.md" > "$output_path/README.md"
# remove downloaded files
rm -rf "$output_path/.new/"
rm -rf "$temp_location"
echo -e "[info] efc is updated to the latet version: ▶ ▶ ▶ " '\n'
./efc --version
else
echo "[error] unable to download the latest file, exiting."
fi
else
echo "unable to access github.com, check the internet connection or proxy settings, exiting"
exit
fi
}