forked from SergKolo/sergrep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add-update.awk
executable file
·170 lines (146 loc) · 4.01 KB
/
add-update.awk
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
#!/usr/bin/awk -f
#
###########################################################
# Author: Serg Kolo
# Date: Nov 27,2015
# Purpose: A script that enables/disables 4 ubuntu sources
# (namely updates, backports, proposed, and security )
# much in a way like software-properties-gtk does
# Written for: http://paste.ubuntu.com/13434218/
###########################################################
#
# Permission to use, copy, modify, and distribute this software is hereby granted
# without fee, provided that the copyright notice above and this permission statement
# appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
function printUsage() {
print "Usage: sudo ./add-update.awk -v ACTION=[enable|disable|help] -v SOURCE=[updates|backports|security|proposed]";
exit
}
function checkSourceEnabled()
{
if ( $3 ~ SOURCE) {
print SOURCE" is enabled; exiting"
VAL = 1
}
else {
VAL = 0
}
return VAL
}
function disableSource()
{
if ( $0 ~ SOURCE ) $0="# removed";
j++;
newLines[j]=$0;
}
function listStuff () {
for(k=4; k<=NF; k++) if ( $k~/#/ ) {break} else {
COMPONENTS=COMPONENTS" "$k
};
gsub(/\-.*/,"",$3);
STRING=$1" "$2" "$3APPEND" "COMPONENTS;
COMPONENTS=""
return STRING;
}
function replaceFile()
{
command="mv /tmp/sources.list "ARGV[1]
system(command);
}
############
# MAIN
#############
BEGIN {
# argument checking sequence
# must remain written in if-else
# structure rather than case,
# to consider users who may not be able
# to install gawk due to broken sources.list
# which is what this script should be aimed at
# actions checked first so that
# help message can be printed
if ( ACTION == "enable" ||
ACTION == "disable" ||
ACTION == "default" ) {
actionArgs="0"
print "<<< ACTION ARG OK"
}
else if (ACTION == "help" ){
actionArgs="1"
printUsage()
exit
}
if ( SOURCE == "update" ||
SOURCE == "security" ||
SOURCE == "backports" ||
SOURCE == "proposed" ) {
sourceArgs="0"
print "<<< SOURCE ARG OK"
}
else if ( ACTION != "default" || ACTION != "help" ) {
sourceArgs="1"
print "<<< E: SOURCE ARG INCORRECT";
printUsage();
exit }
# static filename to operate on
ARGV[ARGC++]="/etc/apt/sources.list";
if (ACTION == "enable" ) {
APPEND="-"SOURCE;
} else{
APPEND="";
}
} # END OF BEGIN
$0~/^deb*/ && $0!~/partner/ && $0!~/extra/ {
if ( ACTION == "enable" ) {
j++;
ARRAY[j]=$0
ENABLED=checkSourceEnabled();
if ( ENABLED ) {
exit 1
}
else {
j++;
ARRAY[j]=listStuff();
}
}
else if ( ACTION == "disable" ){
disableSource() ;
}
else if ( ACTION == "default" && SOURCE == "default" ) {
j++;
defaultsArray[j]=$0;
j++;
defaultsArray[j]=listStuff();
}
}
END {
if ( sourceArgs == "1" || actionArgs == "1" )
{
print "<<< Post-processing skipped"
}
else {
print "<<< Script finished processing" ;
if ( ACTION =="enable" && ENABLED == 0 ){
for(i=1;i<=j;i++)
print ARRAY[i] | "sort -u > /tmp/sources.list ";
replaceFile();
}
else if ( ACTION == "disable" ) {
for ( i=1;i<=j;i++ ) print newLines[i] | "sort -u > /tmp/sources.list"
replaceFile();
}
else if (ACTION == "default" ){
for ( i=1;i<=j;i++ ) print defaultsArray[i] | "sort -i -u > /tmp/sources.list"
replaceFile();
}
}
}
# END OF MAIN