summaryrefslogtreecommitdiff
path: root/arch2parabola
blob: a5c909cbe387794dc0985065c883e0e2dcacf0c8 (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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#!/bin/bash

# arch2parabola - converts an Arch Gnu/Linux system to Parabola Gnu/Linux-libre

#  Copyright © 2010 Joshua Ismael Haase Hernández
#  Copyright © 2011 Joseph Graham

#  ---------- GNU General Public License 3 ----------
                                                                             
#  This file is part of Parabola.                                          
                                                                             
#  Parabola is free software: you can redistribute it and/or modify        
#  it under the terms of the GNU General Public License as published by    
#  the Free Software Foundation, either version 3 of the License, or       
#  (at your option) any later version.                                     
                                                                             
#  Parabola is distributed in the hope that it will be useful,             
#  but WITHOUT ANY WARRANTY; without even the implied warranty of          
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           
#  GNU General Public License for more details.                            
                                                                             
#  You should have received a copy of the GNU General Public License       
#  along with Parabola.  If not, see <http://www.gnu.org/licenses/>.       

# Set this to the URL of the blacklist.
blacklist_url="http://repo.parabolagnulinux.org/docs/blacklist.txt"

# Set this to the url of the index page which will be parsed to find out the
# url of the mirrorlist.
repo_index_page="http://repo.parabolagnulinux.org/core/os/any/"

# Set this to the package name of Parabola's mirrorlist (excluding the version
# number).
parabola_mirrorlist_package_name="pacman-mirrorlist-libre"

# Set this to the package name of Arch's default kernel (excluding the version
# number).
arch_default_kernel_package_name="kernel26"

# Make a temporary directory.
tempdir=$(mktemp -d)

# This will be the name of the log file.
logname="$(pwd)/arch2parabola$(date +%Y%m%d).log"

# These are the dependencies of the script.
dependancies=(which date pacman wget seq sed wget test)

# ---------- Function Declarations ----------

# Define the abort procedure.
function abort
{
    cat << EOF
Something failed in ${section}.

A debug log has been saved to ${logname}.

Please fix the error and re-run the script to finnish converting your system to
Parabola Gnu/Linux.
EOF
    rm -rf ${tempdir}
    exit 1
}

# Ask yes or no (y/n), if given, stop, otherwise keep asking.
# uses variable "q"
function askyn
{
    case "$1" in 
	y | n ) ;;
	*) echo Answer "y" or "n"
	   read q
	   askyn ${q}
	   ;;
    esac
}

# This function logs, and echos the section.
function log_section
{
    cat >> ${logname} << EOF

${section}

EOF
    echo
    echo ${section}
}

# This function checks if the `askyn' function was cancelled, and if so, logs
# it and aborts.
function if_cancelled
{
    if [[ ${q} == "n" ]]
    then
	    echo "The user canceled the procedure" >> ${logname}
	    abort
    else
	    unset q
    fi
}

# This is an SGML parser function. It reads tags in the same way that `read'
# reads lines. The element is assigned to var `element' and the content is
# assigned to var `content'.
function rdom
{
    local IFS=\>
    read -d \< element content
}

section=" ---------- Sanity Check ----------"

declare issues=0

# If the user is not root then error and exit.
(( EUID )) && { echo "This script should be run as root user." ; exit 1 ; }

# Check if the dependencies are available. This won't be needed if this
# script is packaged.
for dep in ${dependancies[@]}
do
    if ! which ${dep} > /dev/null 2> /dev/null
    then
	issues="1"
	"${dep} is missing, please install it." >> ${logname}
    fi
done

# Check if the system is running a custom kernel.
if ! pacman -Q ${arch_default_kernel_package_name} >/dev/null 2>/dev/null
then
    issues="1"
    cat >> ${logname} << EOF
The system is running a custom kernel.
Please uninstall it before running this script.

You can find a PKGBUILD for a linux-libre kernel at

http://repo.parabolagnulinux.org/pkgbuilds/

EOF
fi

# If anything went wrong then abort.
(( issues )) && abort

unset issues

section="---------- Non free packages revision ----------"
log_section

cd ${tempdir}

echo "Downloading the blacklist of proprietary software packages."

if ! wget ${blacklist_url} >> ${logname} \
    2>> ${logname}
then
    echo "Download failed, exiting" >> ${logname}
    abort
fi

declare -a exists

echo "Searching for proprietary software on the system."
echo
exists[0]="These proprietary software packages have been found on the system:"

for package in $(cut -d: -f1 blacklist.txt)
do
    # Check if the package is in pacman's database.
    if pacman -Q ${package} >/dev/null 2>/dev/null
    then
        # Add this package to the array of blacklited packages that have been
	# found in the system.
	exists[${#exists[@]}]=${package}

	# Echo and log the package.
	echo ${package} | tee -a ${logname}
    fi
done

unset exists[0]

section="---------- Pacman mirrorlist replacement ----------"
log_section
cat << EOF
* Pacman will be synced to avoid any errors.
* Pacman mirror list will be replaced for parabola mirror list.

Do you wish to continue? [y/n]
EOF
askyn
if_cancelled

# Update pacman.
if ! pacman -S --noconfirm pacman
then
    echo "Syncing pacman failed." >> ${logname}
    abort
fi

# This piece of code automatically detects the url of the mirrorlist and puts
# it in the var `mirror'.
while rdom
do
    if [[ ${element%%[[:space:]]*} == "a" ]]
    then mirror="$(echo ${element##*[[:space:]]} | cut -d\" -f 2 | grep \
	"${parabola_mirrorlist_package_name}-")"
    fi
done <<< "$(curl "${repo_index_page}")"

# Install the mirrorlist.
if ! pacman -U --noconfirm ${mirror} >> ${logname} 2>> ${logname}
then
    cat >> ${logname} << EOF 
Installing the libre mirror list failed.
Maybe there's a new mirrorlist and we need to update the script.

If that's the case send a mail to dev@list.parabolagnulinux.org

Otherwise, try again.
EOF
    abort
fi

# Rename `mirrorlist.pacnew' to `mirrorlist'.
mv /etc/pacman.d/mirrorlist.pacnew /etc/pacman.d/mirrorlist

section="---------- Package Replacement ----------"
log_section
cat << EOF

* Pacman cache will be erased
* Pacman database will be updated for parabola
* Non free packages that have free replacement will be Synced

Do you wish to continue? [y/n] 
EOF
askyn
if_cancelled

pacman -Scc --noconfirm >> ${logname} 2>> ${logname} || abort
pacman -Syy --noconfirm >> ${logname} 2>> ${logname} || abort

declare replacement

# We search the blacklist to find packages that have libre replacements and
# install them.
for package in ${exists[@]}
do
    if [[ ${package} ]]
    then
	replacement=$(grep -e ${package}[:space:] blacklist.txt | cut -d: f2)
	if [[ ${replacement} ]]
	then
	    pacman -S --noconfirm ${replacement} >> ${logname} 2>> ${logname} ||
	    abort
	fi
    fi
done

unset replacement

section="---------- Non-free packages removal ----------"
log_section
cat <<EOF

* Non free packages that don't have free replacements will be Removed

Do you wish to continue? [y/n] 
EOF
askyn
if_cancelled

# Remove the packages that don't have free replacements.
for package in ${exists[@]}
do
    pacman -Rc --noconfirm ${package} >> ${logname} 2>> ${logname} || abort
done

rm -rf ${tempdir}

section="---------- You are now on Parabola GNU/Linux ----------"
log_section
cat << EOF
Welcome to Freedom.

* You have to manually remove any non-free packages you installed from the AUR.

Do you wish to keep the log? [y/n] 
EOF

askyn

# Ask the user if they want to keep the log and if not then delete it.
if [[ ${q} == "y" ]]
then
    echo "The log has been saved in ${logname}"
else
    rm -f ${logname}
fi

exit 0