This repository has been archived by the owner on Mar 16, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathnew-repository
executable file
·228 lines (193 loc) · 7.16 KB
/
new-repository
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#!/bin/bash
# _______ _ _______ _______ ______ _ _______
# ( ____ \|\ /|( ( /|( ____ \( ____ \( ___ \ ( \ ( ____ \
# | ( \/| ) ( || \ ( || ( \/| ( \/| ( ) )| ( | ( \/
# | (__ | | | || \ | || | | (__ | (__/ / | | | (__
# | __) | | | || (\ \) || | | __) | __ ( | | | __)
# | ( | | | || | \ || | | ( | ( \ \ | | | (
# | ) | (___) || ) \ || (____/\| (____/\| )___) )| (____/\| (____/\
# |/ (_______)|/ )_)(_______/(_______/|/ \___/ (_______/(_______/
# Written by: @Funilrys, Nissar Chababy <contact at funilrys dot com>
# Github : https://github.com/funilrys/funceble
# Github : https://github.com/funilrys/dead-hosts
################################ Contributors ##################################
# Let's contribute !!
################################################################################
############################### Text Format ###################################
# Red Color
red=$(tput setaf 1)
# White Color
white=$(tput setaf 7)
# Bold
bold=$(tput bold)
# Disable formating
normal=$(tput sgr0)
################################################################################
############################## Default Values ##################################
# Default list name
listName=''
# Default link to download
linkToDownload=''
# Parrent dirr
parentDir=$(dirname ${PWD})/
# Default rawLink
rawLink=''
# Default README.md location
readmeDef="${PWD}/README"
# Default value of repo name
repoName=''
# Default tested-list Dir
testedDir='tested-list'
# Update.me filename
updateMe='update.me'
# Default update location
updateMeDef="${PWD}/${updateMe}"
################################################################################
################################## Check Variables #############################
# Used to check "integrity" of main variables
#
# @CalledBy createRepository
################################################################################
checkVariables()
{
important=("${listName}" "${rawLink}" "${repoName}")
commands=('n' 'l' 'd')
for i in ${!important[*]}
do
if [[ ${important[${i}]} == "" ]]
then
echo "${red}${bold}Please add ${white}-${commands[${i}]}${normal} ${red}${bold}to your statement${normal}"
echo ""
exit 1
fi
done
}
################################## Create Dir ##################################
# Used to apply DRY againt the creation of directory
#
# @CalledBy createRepository
################################################################################
createDir()
{
# We initiate the variable we need
local mkdirectory="${1}"
# We create directory
mkdir "${mkdirectory}"
# We create the file
touch "${mkdirectory}/.keep"
}
################################## Create Repository ###########################
# logic aroung the creation of the sub-repository
#
# @CalledBy Arguments Handle section
################################################################################
createRepository()
{
checkVariables
if [[ ${dirToCreate} != '' ]]
then
if [[ ! -d ${dirToCreate} ]]
then
# We create the main directory
mkdir ${dirToCreate}
# We create directory
createDir ${dirToCreate}${testedDir}
else
echo 'Repo Already exist'
exit 1
fi
# We copy the default files
cp ${readmeDef} ${dirToCreate}/README.md
cp ${updateMeDef} ${dirToCreate}
# We give execution permission
chmod +x ${dirToCreate}${updateMe}
# We update the content of ${dirToCreate}${repoToolFileName}
filesContent
fi
}
################################ Replace Content ###############################
# sed to apply DRY againt the replacement of the content
#
# @CalledBy filesContent
################################################################################
replaceContent()
{
# We initiate the variable we need
local regex="${1}"
local replaceBy="${2}"
local file="${3}"
# We replace the content of the given file with the given data
sed -i "s|${regex}|${replaceBy}|g" "${file}"
}
################################ Replace Content ###############################
# sed to apply DRY againt the replacement of the content
#
# @CalledBy filesContent
################################################################################
filesContent()
{
checkVariables
local updateLocation="${dirToCreate}${updateMe}"
local readmeLocation="${dirToCreate}README.md"
listName=${listName// /_}
regexListName="%%listName%%"
regexRepoName='%%repoName%%'
regexRawLink='%%rawLink%%'
# We replace/update the content of update script
replaceContent "${regexListName}" "${listName}" "${updateLocation}"
replaceContent "${regexRawLink}" "${rawLink}" "${updateLocation}"
# We replace/update the content of README.md
replaceContent "${regexRepoName}" "${repoName}" "${readmeLocation}"
replaceContent '%%DownloadLink%%' "${rawLink}" "${readmeLocation}"
}
################################## Usage #######################################
# Help function
#
# @CalledBy main, Arguments Handle Section
################################################################################
usage()
{
echo "Usage: ${0}"
echo ""
echo " {[ -l 'http://linktothelist.com' ]} && {[ -n 'fileNameOfTheList' ]} && {[ -d 'nameOfTheMainDirectory' ]}"
echo ""
echo " -d Name of the main directory"
echo " -n Name of the file that's gonna be downloaded (${red}${bold}Must be before ${white}-d${normal})"
echo " -l Link to the raw format of the list (${red}${bold}Must be before ${white}-d${normal})"
echo ""
echo "Examples:"
echo ""
echo " ${0} ${red}-n${normal} ${bold}'badreferers'${normal} ${red}-l${normal} ${bold}'https://raw.githubusercontent.com/mitchellkrogza/nginx-ultimate-bad-bot-blocker/master/Pull_Requests_Here_Please/badreferers.list'${normal} ${red}-d${normal} ${bold}'Badd-Boyz-Hosts@mitchellkrogza'${normal}"
echo " Generate the ${bold}'Badd-Boyz-Hosts@mitchellkrogza'${normal} directory with the list name ${bold}'badreferers.list'${normal}"
echo ""
}
############################### Arguments Handle ###############################
# We use this part to get arguments from command line.
#
# @Requiredby All
################################################################################
while [ "$#" -gt 0 ]; do
case "$1" in
# We catch the list name (whithout .list)
-n)
listName="${2}"
shift 2
;;
# We catch the repoName and create it's directory
-d) # Need to be the last parameter to pass
dirToCreate="${parentDir}${2}/"
repoName="${2}"
createRepository
shift 2
;;
# We catch the raw link
-l)
rawLink="${2}"
shift 2
;;
*)
usage
exit 1
;;
esac
done