forked from mrshannon/makedeb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makedeb
executable file
·1041 lines (882 loc) · 25.3 KB
/
makedeb
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# makedeb - build Debain packages from PKGBUILD files
# Copyright (C) 2018 Michael R. Shannon <[email protected]>
#
# This program 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 2 of the License, or
# (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
# used for passing function outputs around
declare retval
declare missing_depends=()
declare installed_depends=()
declare source_files=()
declare pkgname=""
declare pkgver=""
declare pkgrel=""
declare depends=()
declare makedepends=()
declare checkdepends=()
declare source=()
declare sources=()
declare noextract=()
declare section=""
declare priority="optional"
declare essential="no"
declare maintainer=""
declare pkgdesc=""
declare pkglong=""
declare url=""
declare predepends=()
declare suggests=()
declare enhances=()
declare replaces=()
declare conflicts=()
declare provides=()
declare arch=""
declare copyright=""
declare changelog=""
declare backup=()
declare conffiles=()
declare install=""
# GLOBALS
# ansi codes
declare -r ANSI_RESET='\033[0m'
declare -r ANSI_BOLD='\033[1m'
declare -r ANSI_RED='\033[0;31m'
declare -r ANSI_GREEN='\033[0;32m'
declare -r ANSI_YELLOW='\033[0;33m'
declare -r ANSI_BLUE='\033[0;34m'
declare -r EXIT_ON_FAIL=1
# OPTIONS
INSTALL_DEPENDS=
REMOVE_DEPENDS=0
# FUNCTIONS
msg1() {
echo -e "${ANSI_GREEN}${ANSI_BOLD}==>${ANSI_RESET}${ANSI_BOLD} ${1}${ANSI_RESET}"
}
msg2() {
echo -e " ${ANSI_BLUE}${ANSI_BOLD}->${ANSI_RESET}${ANSI_BOLD} ${1}${ANSI_RESET}"
}
msg3() {
echo " $1"
}
warning() {
echo -e "${ANSI_YELLOW}${ANSI_BOLD}==> WARNING:${ANSI_RESET}${ANSI_BOLD} ${1}${ANSI_RESET}"
}
error() {
echo -e "${ANSI_RED}${ANSI_BOLD}==> ERROR:${ANSI_RESET}${ANSI_BOLD} ${1}${ANSI_RESET}"
if [[ "$EXIT_ON_FAIL" -ne 0 ]]; then
exit 1
fi
}
# https://stackoverflow.com/a/17841619
join() {
local d=$1
shift
echo -n "$1"
shift
printf "%s" "${@/#/$d}"
}
stripstr() {
sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//'
}
date_string() {
date -u '+%a %d %b %Y %r UTC'
# date -u '+%F %T UTC'
}
ctrl_c() {
echo -e "\nExiting..."
exit 1
}
# https://stackoverflow.com/a/24067243
version_gt() {
test "$(printf '%s\n' "$@" | sort -V | head -n 1)" != "$1"
}
version_compare() {
local first=$1
local op=$2
local second=$3
case "$op" in
"<")
version_gt "$second" "$first"
;;
"<=")
test "$first" == "$second" || version_gt "$second" "$first"
;;
"==")
test "$first" == "$second"
;;
">=")
test "$first" == "$second" || version_gt "$first" "$second"
;;
">")
version_gt "$first" "$second"
;;
*)
error "Invalid operator $op"
exit 1
;;
esac
return $?
}
# reads the maintainer from the comment at the top of the PKGBUILD file, this
# is here for compatibility with Arch Linux makepkg, you should explicitly set
# the maintainer field in PKGBUILD for makedeb
read_maintainer() {
maintainer=$(grep -Po -m 1 '(?<=Maintainer:).*$' "${startdir}/PKGBUILD")
maintainer=$(echo "$maintainer" | stripstr)
}
print_start_message() {
msg1 "Making package: ${pkgname} ${pkgver}-${pkgrel} ($(date_string))"
}
# validates entries in the sourced PKGBUILD file, which must be sourced before
# calling this function.
validate_pkgbuild() {
local tmp=()
# combine source and sources and save them back
tmp=("${source[@]}" "${sources[@]}")
source=("${tmp[@]}")
sources=("${tmp[@]}")
# combine optdepends and recommends and save them back
tmp=("${optdepends[@]}" "${recommends[@]}")
optdepends=("${tmp[@]}")
recommends=("${tmp[@]}")
# combine backup and conffiles and save them back
tmp=("${backup[@]}" "${conffiles[@]}")
backup=("${tmp[@]}")
conffiles=("${tmp[@]}")
}
check_depends() {
local package
local packages
local name
local op
local required_version
local installed_version
local found
retval=()
for package_string in "$@"; do
# convert an alternative list with | into an array
IFS='|' read -r -a packages <<< "${package_string}"
found=""
for package in "${packages[@]}"; do
name=$(echo "$package" | stripstr | grep -o '^[^<=>]*')
op=$(echo "$package" | stripstr | grep -Po "(?<=^$name)"'[<=>]*')
required_version=$(echo "$package" | stripstr | \
grep -Po "(?<=^$name$op)"'[0-9\.]*')
installed_version=$(dpkg -s "${name}" 2>/dev/null | \
grep -Po '(?<=Version: )[0-9\.]*')
if [[ -n "$installed_version" ]]; then
if [[ -n "$required_version" ]]; then
if ! version_compare "$installed_version" "$op" \
"$required_version"; then
local msg
msg="${name}'s installed version "
msg="${msg}(${installed_version}) is not ${op} "
msg="${msg}${required_version}"
error "$msg"
fi
fi
msg2 "Found ${name}"
found=1
fi
done
if [[ -z "$found" ]]; then
retval+=("${package_string}")
fi
done
}
check_runtime_depends() {
if [[ ${#depends[@]} -gt 0 ]]; then
msg1 "Checking runtime dependencies..."
check_depends "${depends[@]}"
missing_depends+=("${retval[@]}")
fi
}
check_buildtime_depends() {
if [[ ${#makedepends[@]} -gt 0 ]]; then
msg1 "Checking buildtime dependencies..."
check_depends "${makedepends[@]}"
missing_depends+=("${retval[@]}")
fi
}
check_checktime_depends() {
if [[ ${#checkdepends[@]} -gt 0 ]]; then
msg1 "Checking checktime dependencies..."
check_depends "${checkdepends[@]}"
missing_depends+=("${retval[@]}")
fi
}
install_missing_depends() {
local packages
local name
if [[ $(( ${#missing_depends[@]})) -gt 0 ]]; then
msg1 "Missing dependencies:"
for package in "${missing_depends[@]}"; do
msg2 "$package"
done
if [[ -z "$INSTALL_DEPENDS" ]]; then
local msg
msg="Would you like to install the required dependencies? (y/N): "
read -p "$msg" -n 1 -r
echo ""
if [[ "$REPLY" == "y" || "$REPLY" == "Y" ]]; then
INSTALL_DEPENDS=1
fi
fi
if [[ "$INSTALL_DEPENDS" -ne 0 ]]; then
msg1 "Installing dependencies..."
sudo -k
for package in "${missing_depends[@]}"; do
packages=(${package//|/})
name=$(echo "${packages[0]}" | grep -o '^[^<=>]*')
msg2 "${name}"
if sudo apt-get install --yes "${name}"; then
sudo apt-mark auto "${name}"
installed_depends+=("${name}")
else
error "Failed to install ${name}."
fi
done
else
error "Could not resolve all dependencies."
fi
fi
}
retrieve_sources() {
msg1 "Retrieving sources..."
local source_file
local name
local url
# loop through each source file
for source_file in "${sources[@]}"; do
url=$(echo "$source_file" | awk -F'::' '{print $2}')
name=$(echo "$source_file" | awk -F'::' '{print $1}')
if [[ "$(basename "$name")" != "$name" ]]; then
url=$name
name=$(basename "$name")
fi
# check if file is already downloaded
if [[ -f "${startdir}/${name}" ]]; then
msg2 "Found ${name}"
elif [[ -n "${name}" && -n "${url}" ]]; then
msg2 "Downloading ${name}"
if ! curl --location --output "${startdir}/${name}" "$url"; then
error "Failed to download '${name}' from ${url}"
fi
else
error "File ${name} does not exist."
fi
source_files+=("$name")
done
}
validate_sources() {
local failed=0
if ! _validate_sources md5sum; then
failed=1
fi
if ! _validate_sources sha1sum; then
failed=1
fi
if ! _validate_sources sha256sum; then
failed=1
fi
if ! _validate_sources sha384sum; then
failed=1
fi
if ! _validate_sources sha512sum; then
failed=1
fi
if [[ $failed -gt 0 ]]; then
error "One or more files did not pass the validity check!"
fi
}
_validate_sources() {
local -n sums="${1}s"
if [[ ${#sums[@]} -gt 0 ]]; then
msg1 "Validating source files with ${1}s..."
if [[ ${#sums[@]} -ne ${#source_files[@]} ]]; then
error "'${1}s' array not the same length as the 'source' array."
fi
local sum
local failed=0
for ((i=0;i<${#source_files[@]};++i)); do
if [[ ${sums[i]^^} != "SKIP" ]]; then
echo -en " ${ANSI_BLUE}${ANSI_BOLD}->${ANSI_RESET} "
echo -en "${ANSI_BOLD}${source_files[i]} ... ${ANSI_RESET}"
sum=$(${1} "${startdir}/${source_files[i]}" | cut -d' ' -f1)
if [[ "$sum" == "${sums[i]}" ]]; then
echo -e "${ANSI_BOLD}Passed${ANSI_RESET}"
else
echo -e "${ANSI_RED}${ANSI_BOLD}FAILED${ANSI_RESET}"
failed=1
fi
fi
done
return $failed
fi
}
extract_sources() {
mkdir -p "${srcdir}"
msg1 "Extracting sources..."
for file in "${source_files[@]}"; do
local extract=1
for exception in "${noextract[@]}"; do
if [[ $(basename "$exception") == "$(basename "$file")" ]]; then
extract=0
fi
done
if [[ $extract -gt 0 ]]; then
case ${file,,} in
*.tar|*.zip|*.tar.bz2|*.tar.gz|*.tar.xz|*.tbz2|*.tgz|*.txz|*.7z|*.iso|*.tpz)
msg2 "Extracting ${file} with bsdtar"
if ! bsdtar -C "${srcdir}" -xf "${startdir}/${file}"; then
error "Failed to extract $file"
fi
;;
*)
extract=0
;;
esac
fi
if [[ $extract -eq 0 ]]; then
cp "${startdir}/${file}" "${srcdir}/${file}"
fi
done
}
run_prepare() {
cd "${startdir}"
if [[ "$(type -t prepare)" == "function" ]]; then
msg1 "Starting prepare()"
prepare
fi
}
run_build() {
cd "${startdir}"
if [[ "$(type -t build)" == "function" ]]; then
msg1 "Starting build()"
build
fi
}
run_check() {
cd "${startdir}"
if [[ "$(type -t check)" == "function" ]]; then
msg1 "Starting check()"
check
fi
}
run_package() {
cd "${startdir}"
if [[ "$(type -t package)" != "function" ]]; then
error "No package() function in PKGBUILD."
fi
mkdir -p "${pkgdir}"
msg1 "Starting package()"
package
}
install_docs() {
msg1 "Installing docs..."
install_copyright
install_changelog
}
install_copyright() {
if [[ -n "${copyright}" ]]; then
if [[ -f "${startdir}/${changelog}" ]]; then
msg2 "copyright"
mkdir -p "${pkgdir}/usr/share/doc/${pkgname}"
cat "${startdir}/${copyright}" >\
"${pkgdir}/usr/share/doc/${pkgname}/copyright"
else
error "Cannot find copyright file: ${copyright}"
fi
fi
}
install_changelog() {
if [[ -n "${changelog}" ]]; then
if [[ -f "${startdir}/${changelog}" ]]; then
msg1 "changelog.Debian.gz"
mkdir -p "${pkgdir}/usr/share/doc/${pkgname}"
gzip -9 < "${startdir}/${changelog}" >\
"${pkgdir}/usr/share/doc/${pkgname}/changelog.gz"
else
error "Cannot find changelog file: ${changelog}"
fi
fi
}
make_debian() {
msg1 "Creating DEBIAN directory..."
mkdir -p "${pkgdir}/DEBIAN"
make_debian_control
make_debian_conffiles
make_debian_preinst
make_debian_postinst
make_debian_prerm
make_debian_postrm
}
make_debian_control() {
msg2 "DEBIAN/control"
local control="${pkgdir}/DEBIAN/control"
true >"${control}"
# Package
echo "Package: ${pkgname}" >>"${control}"
# Version
if [[ -n "$pkgrel" ]]; then
echo "Version: ${pkgver}-${pkgrel}" >>"${control}"
else
echo "Version: ${pkgver}" >>"${control}"
fi
# Section
if [[ -n "${section}" ]]; then
echo "Section: ${section}" >>"${control}"
fi
# Priority
if [[ -n "${priority}" ]]; then
echo "Priority: ${priority}" >>"${control}"
fi
# Architecture
local debarch=""
case "${arch}" in
any)
debarch=all
;;
i686)
debarch=i386
;;
x86_64)
debarch=amd64
;;
*)
debarch=$arch
esac
echo "Architecture: ${debarch}" >>"${control}"
# Essential
if [[ -n "$essential" && "$essential" != "no" ]]; then
echo "Essential: ${essential}" >>"${control}"
fi
# Depends
if [[ ${#depends[@]} -gt 0 ]]; then
echo "Depends: $(dependency_string "${depends[@]}")" >>"${control}"
else
echo "Depends: " >>"${control}"
fi
# Pre-Depends
if [[ ${#predepends[@]} -gt 0 ]]; then
echo "Pre-Depends: $(dependency_string "${predepends[@]}")" >>"${control}"
fi
# Recommends
if [[ ${#recommends[@]} -gt 0 ]]; then
echo "Recommends: $(dependency_string "${recommends[@]}")" >>"${control}"
fi
# Suggests
if [[ ${#suggests[@]} -gt 0 ]]; then
echo "Suggests: $(dependency_string "${suggests[@]}")" >>"${control}"
fi
# Enhances
if [[ ${#enhances[@]} -gt 0 ]]; then
echo "Enhances: $(dependency_string "${enhances[@]}")" >>"${control}"
fi
# Breaks and Replaces
if [[ ${#replaces[@]} -gt 0 ]]; then
echo "Breaks: $(dependency_string "${replaces[@]}")" >>"${control}"
echo "Replaces: $(dependency_string "${replaces[@]}")" >>"${control}"
fi
# Conflicts
if [[ ${#conflicts[@]} -gt 0 ]]; then
echo "Conflicts: $(dependency_string "${conflicts[@]}")" >>"${control}"
fi
# Provides
if [[ ${#provides[@]} -gt 0 ]]; then
echo "Provides: $(dependency_string "${provides[@]}")" >>"${control}"
fi
# Installed-Size
# local size
# size=$(du -ks --apparent-size "${pkgdir}" | cut -f1)
# echo "Installed-Size: ${size}" >>"${control}"
echo "Installed-Size: __INSTALLED_SIZE__" >>"${control}"
# Maintainer
echo "Maintainer: ${maintainer}" >>"${control}"
# Homepage
if [[ -n "${url}" ]]; then
echo "Homepage: ${url}" >>"${control}"
fi
# Description
echo "Description: ${pkgdesc}" >>"${control}"
if [[ -n "$pkglong" ]]; then
echo "${pkglong}" | sed -e 's/^/ /g' >>"${control}"
fi
}
dependency_string() {
local name
local op
local version
local packages
local tmp
local dependencies=()
for package_string in "$@"; do
# convert an alternative list with | into an array
IFS='|' read -r -a packages <<< "${package_string}"
tmp=()
for package in "${packages[@]}"; do
name=$(echo "$package" | stripstr | grep -o '^[^<=>]*')
op=$(echo "$package" | stripstr | grep -Po "(?<=^${name//+/\\+})"'[<=>]*')
version=$(echo "$package" | stripstr | \
grep -Po "(?<=^${name//+/\\+}${op})"'[0-9\.]*')
if [[ -n ${version} ]]; then
tmp+=("${name} (${op} ${version})")
else
tmp+=("$name")
fi
done
dependencies+=("$(join ' | ' "${tmp[@]}")")
done
join ', ' "${dependencies[@]}"
}
make_debian_conffiles() {
if [[ ${#conffiles[@]} -gt 0 ]]; then
msg2 "DEBIAN/conffiles"
local outfile="${pkgdir}/DEBIAN/conffiles"
true >"${outfile}"
for file in "${conffiles[@]}"; do
echo "/${file}" >>"${outfile}"
done
fi
}
# returns true (0) if the file ($1) contains the function $($2)
has_function() {
grep -q 'function\s*'"$1" "$2" || grep -q "$1"'\s*(\s*)' "$2"
}
make_debian_preinst() {
if [[ -n "$install" && -f "${startdir}/${install}" ]] && \
(has_function pre_install "${startdir}/${install}" || \
has_function pre_upgrade "${startdir}/${install}")
then
msg2 "DEBIAN/preinst"
local file="${pkgdir}/DEBIAN/preinst"
cat >"$file" <<EOF
#!/bin/bash -e
pkgname=${pkgname}
pkgver=${pkgver}
pkgrel=${pkgrel}
EOF
cat "${startdir}/${install}" >>"$file"
cat >>"$file" <<'EOF'
case "$1" in
install)
if [[ $# -ge 2 ]]; then
if [[ $(type -t pre_upgrade) == "function" ]]; then
pre_upgrade "$2" "${pkgver}-${pkgrel}"
fi
else
if [[ $(type -t pre_install) == "function" ]]; then
pre_install "${pkgver}-${pkgrel}"
fi
fi
;;
upgrade)
if [[ $(type -t pre_install) == "function" ]]; then
pre_upgrade "$2" "${pkgver}-${pkgrel}"
fi
;;
esac
EOF
chmod 755 "$file"
fi
}
make_debian_postinst() {
if [[ -n "$install" && -f "${startdir}/${install}" ]] && \
(has_function post_install "${startdir}/${install}" || \
has_function post_upgrade "${startdir}/${install}")
then
msg2 "DEBIAN/postinst"
local file="${pkgdir}/DEBIAN/postinst"
cat >"$file" <<EOF
#!/bin/bash -e
pkgname=${pkgname}
pkgver=${pkgver}
pkgrel=${pkgrel}
EOF
cat "${startdir}/${install}" >>"$file"
cat >>"$file" <<'EOF'
case "$1" in
configure)
if [[ $# -ge 2 ]]; then
if [[ $(type -t post_upgrade) == "function" ]]; then
post_upgrade "$2" "${pkgver}-${pkgrel}"
fi
else
if [[ $(type -t post_install) == "function" ]]; then
post_install "${pkgver}-${pkgrel}"
fi
fi
;;
abort-remove)
if [[ $(type -t post_install) == "function" ]]; then
post_install "${pkgver}-${pkgrel}"
fi
;;
abort-upgrade)
if [[ $# -ge 2 ]]; then
if [[ $(type -t post_install) == "function" ]]; then
post_install "${pkgver}-${pkgrel}"
fi
fi
;;
esac
EOF
chmod 755 "$file"
fi
}
make_debian_prerm() {
if [[ -n "$install" && -f "${startdir}/${install}" ]] && \
has_function pre_remove "${startdir}/${install}"
then
msg2 "DEBIAN/prerm"
local file="${pkgdir}/DEBIAN/prerm"
cat >"$file" <<EOF
#!/bin/bash -e
pkgname=${pkgname}
pkgver=${pkgver}
pkgrel=${pkgrel}
EOF
cat "${startdir}/${install}" >>"$file"
cat >>"$file" <<'EOF'
case "$1" in
remove)
if [[ $(type -t pre_remove) == "function" ]]; then
pre_remove "${pkgver}-${pkgrel}"
fi
;;
esac
EOF
chmod 755 "$file"
fi
}
make_debian_postrm() {
if [[ -n "$install" && -f "${startdir}/${install}" ]] && \
(has_function post_remove "${startdir}/${install}" || \
has_function post_purge "${startdir}/${install}")
then
msg2 "DEBIAN/postrm"
local file="${pkgdir}/DEBIAN/postrm"
cat >"$file" <<EOF
#!/bin/bash -e
pkgname=${pkgname}
pkgver=${pkgver}
pkgrel=${pkgrel}
EOF
cat "${startdir}/${install}" >>"$file"
cat >>"$file" <<'EOF'
case "$1" in
remove)
if [[ $(type -t post_remove) == "function" ]]; then
post_remove "${pkgver}-${pkgrel}"
fi
;;
purge)
if [[ $(type -t purge) == "function" ]]; then
purge "${pkgver}-${pkgrel}"
fi
;;
abort_install)
if [[ $# -ge 2 ]]; then
if [[ $(type -t post_remove) == "function" ]]; then
post_remove "${pkgver}-${pkgrel}"
# force Half-Installed state since we can't restore the old
# configuration files
return 1
fi
else
if [[ $(type -t post_remove) == "function" ]]; then
post_remove "${pkgver}-${pkgrel}"
fi
fi
;;
abort-upgrade)
if [[ $# -ge 2 ]]; then
if [[ $(type -t post_remove) == "function" ]]; then
post_remove "${pkgver}-${pkgrel}"
fi
fi
;;
esac
EOF
chmod 755 "$file"
fi
}
update_size() {
msg1 "Updating Installed-Size..."
local size
size=$(du -ks --apparent-size "${pkgdir}" | cut -f1)
sed -i "s/__INSTALLED_SIZE__/${size}/g" "${pkgdir}/DEBIAN/control"
}
gen_sums() {
cd "${pkgdir}" || exit 1
find -type f \
! -regex '.*?debian-binary.*' \
! -regex '.*?DEBIAN.*' -printf '%P\n' | \
xargs -d '\n' md5sum > "${pkgdir}/DEBIAN/md5sums"
cd "${startdir}"
}
build_deb() {
msg1 "Creating package '${pkgname}'..."
dpkg-deb --build "${startdir}/${pkgname}-${pkgver}-${pkgrel}"
}
check_deb() {
if [[ "$nolint" -gt 0 ]]; then
msg1 "Skipping linting '${pkgname}-${pkgver}-${pkgrel}.deb'..."
else
msg1 "Linting '${pkgname}-${pkgver}-${pkgrel}.deb'..."
lintian "${startdir}/${pkgname}-${pkgver}-${pkgrel}.deb"
fi
}
print_completion_message() {
msg1 "Finished making: ${pkgname} ${pkgver}-${pkgrel} ($(date_string))"
}
cleanup() {
if [[ ${#installed_depends[@]} -gt 0 ]]; then
if [[ -z "$REMOVE_DEPENDS" ]]; then
local msg
msg="Would you like to remove the build dependencies installed "
msg="${msg} earlier? (y/N): "
read -p "$msg" -n 1 -r
echo ""
if [[ "$REPLY" == "y" || "$REPLY" == "Y" ]]; then
REMOVE_DEPENDS=1
fi
fi
if [[ "$REMOVE_DEPENDS" -ne 0 ]]; then
msg1 "Removing dependencies installed earlier..."
for package in "${installed_depends[@]}"; do
msg2 "${package}"
if ! sudo apt-get purge --yes "${package}"; then
warning "Failed to remove ${package}."
fi
done
fi
installed_depends=()
fi
}
# BEGIN SCRIPT
trap ctrl_c INT
trap cleanup EXIT
declare startdir="$PWD"
if [[ ! -f "${startdir}/PKGBUILD" ]]; then
error "No PKGBUILD file found."
fi
# source the build script
read_maintainer
. "${startdir}/PKGBUILD"
validate_pkgbuild
declare srcdir="${startdir}/src"
declare pkgdir="${startdir}/${pkgname}-${pkgver}-${pkgrel}"
declare docdir="${pkgdir}/usr/share/doc/${pkgname}"
# clean
if [[ "$1" == "clean" ]]; then
rm -R "$srcdir"
rm -R "$pkgdir"
rm -R "${pkgdir}.deb"
exit 0
fi
# pre fakeroot
print_start_message
# check_runtime_depends
check_buildtime_depends
check_checktime_depends
install_missing_depends
retrieve_sources
validate_sources
extract_sources
run_prepare
run_build
run_check
# fakeroot
msg1 "Entering fakeroot environment..."
fakeroot bash <<EOF
declare pkgname=""
declare pkgver=""
declare pkgrel=""
declare depends=()
declare makedepends=()
declare checkdepends=()
declare source=()
declare sources=()
declare noextract=()
declare section=""
declare priority="optional"
declare essential="no"
declare maintainer=""
declare pkgdesc=""
declare pkglong=""
declare url=""
declare predepends=()
declare suggests=()
declare enhances=()
declare replaces=()
declare conflicts=()
declare provides=()
declare arch=""
declare copyright=""
declare changelog=""
declare backup=()
declare conffiles=()
declare install=""
declare -r ANSI_RESET='\033[0m'
declare -r ANSI_BOLD='\033[1m'
declare -r ANSI_RED='\033[0;31m'
declare -r ANSI_GREEN='\033[0;32m'
declare -r ANSI_YELLOW='\033[0;33m'
declare -r ANSI_BLUE='\033[0;34m'
$(declare -f msg1)
$(declare -f msg2)
$(declare -f msg3)
$(declare -f warning)
$(declare -f error)
$(declare -f join)
$(declare -f stripstr)
$(declare -f read_maintainer)
$(declare -f validate_pkgbuild)
$(declare -f install_docs)