summaryrefslogtreecommitdiffstats
path: root/setup-box
blob: b01de5b67bc2f8096016da70dfe34339dc36060d (plain)
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#!/bin/sh

# Alpine Setup Box
# Copyright (C) 2015 Alan Lacerda
# See LICENSE file for license details 

# Static definition
##
### DO NOT TOUCH THIS VARIABLE
CONFIG_DIR=@CONFIG_DIR@
JSON_DIR=@JSON_DIR@
DEPS=@DEPS@
VERSION=@VERSION@

# Variable definition
##
### This variable is populated by the search_file() function
json_files=""
### After all research, what will we finally install
final_packs=""
final_serv=""
### File that contains all desktop-related groups
desktop_json="$JSON_DIR/desktop.json"

### End of Variables definition
##
#

usage () {
   cat <<__EOF__

$(basename $0) $VERSION
   
Usage: $(basename $0) [options]

A tool to setup a variety of Alpine Linux Environmet

Options:
 -d Specify one desktop environment
 -e Install a specific subgroup
 -g Install a full group
 -h Show this help
 -l List all avaliable groups

Desktop:
 gparted
 mate
 xfce
 
Subgroups:
 pentest.voip
 pentest.forensics

Groups:
 pentest
 
__EOF__
   exit 1
}

# Installing all dependencies for setup-box.
insdep() {
   apk add -q --virtual .setup-box $DEPS || exit 126
}

# Once setup-box has finished its job, we can get rid of all dependencies previously installed
deldep() {
   apk del -q .setup-box || exit 126
}

# We need to exclude $desktop_json from the list. This is due to the fact that
# desktop environment cannot be installed without "base". In order to install
# a desktop environment the user must use -d option
search_files() {
for i in $(ls $JSON_DIR/*.json); do
      if [ "$i" != "$desktop_json" ]; then
	     t="$t "$i
	  fi
   done
   json_files=$t
}

# Function to define in what json files perform the search for the
# group to be installed
## Called by -e option
search_sub_group() {
   for f in $json_files; do
   if [ "${1%.*}" != "desktop" ]; then
	   if [ $(cat $f | jq ".${1%.*} | length") ]; then
		  for j in $@; do
			 core=$(echo $j | cut -d. -f1)
			 sub=$(echo $j | cut -d. -f2)
			 
			 packages=$(cat $f \
				| jq ".$core[] \
				| select(.$sub)" \
				| egrep -Ev '\{|:|]|}' \
				| sed 's/\"//g' \
				| sed 's/,//g')
				
			 final_pack="$final_pack "$packages
		  done
		fi
	fi
   done
}

# Selecting a whole meta-group to be installed
## Called by -g option
search_meta_group() {
   for i in $json_files; do
      if [ $(cat $i | jq ".$1 | length") ]; then
 	     packages=$(cat $i \
			| jq ".$1[]" \
			| egrep -Ev '\{|:|]|}' \
			| sed 's/\"//g' \
			| sed 's/,//g')
			
		services=$(cat  $i \
			| jq ".$1[][]" \
			| jq '.[] \
			| select(.services)' \
			| sed 's/\"//g')
		
		final_pack="$final_pack "$packages
		final_serv="$final_serv "$services
      fi
   done
}

## Called by -d option
search_desktop() {
  if [ $(cat $desktop_json | jq -e ".desktop[] | select(.$1) | length")  ]; then
	 # first of all lets force some packages to be installed
	 for i in "base" "drivers"; do
	    packages=$(cat $desktop_json \
		   | jq ".desktop[] \
		   | select(.$i)" \
		   | egrep -Ev '\{|:|]|}' \
   		   | sed 's/\"//g' \
		   | sed 's/,//g')
		
		services=$(cat $desktop_json \
			| jq ".desktop[].$i[].services[]" \
			| sed 's/\"//g')
	    
	    final_pack="$final_pack "$packages
		final_serv="$final_serv "$services
	 done
	 # Now lets pick up the packages that we were requested to install
	 packages=$(cat $desktop_json \
	    | jq ".desktop[] \
		| select(.$1)" \
		| egrep -Ev '\{|:|]|}' \
		| sed 's/\"//g' \
		| sed 's/,//g')
	 
	 final_pack="$final_pack "$packages
  fi
}

# Called by -l option
list_groups() {
   cat <<__EOF__

$(basename $0) $VERSION

Avaliables groups are: 
__EOF__
 
	for i in $(echo $json_files | sed 's/.json//g' | tr ' ' \\n ); do
		basename $i
	done
}

# Function to remove duplicated packages from the list of packages
# to be installed - just a clean up
optimize_pack() {
	final_pack=$(echo ${1} \
		| tr ' ' \\n \
		| sort -u \
		| tr \\n ' ')
}

# Function to remove duplicated services from the list of services
optimize_serv() {
   final_serv=$(echo ${1} \
		| tr ' ' \\n \
		| sort -u \
		| tr \\n ' ')
}

# Function to execute the rc-update add against each services that were installed
add_services() {
   for i in $@; do 
      rc-update add $i
  done
}

##
# Main function starts here
main () {
   # first of all lets redirect any error to the limbo
   #exec 2&> /dev/null
   
   # Harvesting for all valid JSON files
   # Getting ready to be used...
   search_files

   # If there is no arguments... let's show how to use this application
   if [ $# -lt 1 ]; then
      usage
   fi
   
   # If there is any argument... let's work
	while getopts "d:e:g:hl" opt ; do
			case $opt in
			d) search_desktop "$OPTARG";;
			e) search_sub_group "$OPTARG";;
			g) search_meta_group "$OPTARG";;
			l) list_groups;;
			h) usage;;
			*) usage;;
			esac
	done
	shift `expr $OPTIND - 1`

	# installing dependencies
	# insdep
	
	# Packages to be installed (includes services)
	optimize_pack "$final_pack"
	echo $final_pack

	# Services to be added to startup (just services)
	optimize_serv "$final_serv"
	#add_services $final_serv
	echo Servicos: $final_serv
	
	# Removing dependencies
	# deldep
}

main $@