summaryrefslogtreecommitdiffstats
path: root/pilc/pilc-bootstrap.sh
blob: 5eb8eda0d08c5dce8a08cb5ff913811943ec99e2 (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
#!/bin/sh
#
# PiLC bootstrap
#
# Copyright 2016-2017 Michael Buesch <m@bues.ch>
#
# 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.
#

basedir="$(dirname "$0")"
[ "$(echo "$basedir" | cut -c1)" = '/' ] || basedir="$PWD/$basedir"

# The repository root is basedir.
basedir="$basedir/.."


MAIN_MIRROR="http://mirrordirector.raspbian.org/raspbian/"
RPIGPIO_VERSION="0.6.3"
RPIGPIO_MIRROR="https://netcologne.dl.sourceforge.net/project/raspberry-gpio-python/RPi.GPIO-$RPIGPIO_VERSION.tar.gz"
RPIGPIO_SHA256="9366ff36104a39368759929e71f0d8ad6a88553497b3064cbc40f4248806cc19"


die()
{
	echo "=== $*" >&2
	exit 1
}

info()
{
	echo "--- $*"
}

# print the first of its arguments.
first()
{
	echo "$1"
}

# print the last of its arguments.
last()
{
	while [ $# -gt 1 ]; do shift; done
	echo "$1"
}

# $1=program_name
have_program()
{
	which "$1" >/dev/null 2>&1
}

# $1=program_name, ($2=description)
assert_program()
{
	local bin="$1"
	local desc="$2"
	[ -n "$desc" ] || desc="$bin"
	have_program "$bin" || die "$bin not found. Please install $desc."
}

term_signal()
{
	die "Terminating signal received"
}

cleanup()
{
	info "Cleaning up..."
	for mp in "$mp_shm" "$mp_proc" "$mp_sys" "$mp_bootimgfile" "$mp_rootimgfile"; do
		[ -n "$mp" -a -d "$mp" ] &&\
			umount "$mp" >/dev/null 2>&1
	done
	for mp in "$mp_bootimgfile" "$mp_rootimgfile"; do
		[ -n "$mp" -a -d "$mp" ] &&\
			rmdir "$mp" >/dev/null 2>&1
	done
}

boot_config_file()
{
	cat <<EOF
# For more options and information see
# http://www.raspberrypi.org/documentation/configuration/config-txt.md
# Some settings may impact device functionality. See link above for details

# uncomment if you get no picture on HDMI for a default "safe" mode
#hdmi_safe=1

# uncomment this if your display has a black border of unused pixels visible
# and your display can output without overscan
#disable_overscan=1

# uncomment the following to adjust overscan. Use positive numbers if console
# goes off screen, and negative if there is too much border
#overscan_left=16
#overscan_right=16
#overscan_top=16
#overscan_bottom=16

# uncomment to force a console size. By default it will be display's size minus
# overscan.
#framebuffer_width=1280
#framebuffer_height=720

# uncomment if hdmi display is not detected and composite is being output
#hdmi_force_hotplug=1

# uncomment to force a specific HDMI mode (this will force VGA)
#hdmi_group=1
#hdmi_mode=1

# uncomment to force a HDMI mode rather than DVI. This can make audio work in
# DMT (computer monitor) modes
#hdmi_drive=2

# uncomment to increase signal to HDMI, if you have interference, blanking, or
# no display
#config_hdmi_boost=4

# uncomment for composite PAL
#sdtv_mode=2

# GPU memory
gpu_mem=16

#uncomment to overclock the arm. 700 MHz is the default.
#arm_freq=800

# Uncomment some or all of these to enable the optional hardware interfaces
dtparam=i2c_arm=on
dtparam=i2c_vc=on
#dtparam=i2s=on
#dtparam=spi=on

# Uncomment this to enable the lirc-rpi module
#dtoverlay=lirc-rpi

# Additional overlays and parameters are documented /boot/overlays/README

# Enable audio (loads snd_bcm2835)
dtparam=audio=on
EOF
}

policy_rcd_file()
{
	cat <<EOF
#!/bin/sh
exit 101
EOF
}

download()
{
	local target="$1"
	local mirror="$2"
	local sha256="$3"

	info "Downloading $mirror..."
	rm -f "$target"
	wget -O "$target" "$mirror" || die "Failed to fetch $mirror"
	[ "$(sha256sum -b "$target" | cut -f1 -d' ')" = "$sha256" ] ||\
		die "SHA256 verification of $target failed"
}

pilc_bootstrap_first_stage()
{
	echo "Running first stage..."

	[ "$(id -u)" = "0" ] || die "Permission denied. Must be root."

	# Check host tools (first/third stage).
	assert_program debootstrap
	assert_program git
	assert_program chroot
	assert_program rsync
	assert_program parted
	assert_program mkfs.vfat
	assert_program mkfs.ext4
	assert_program 7z
	assert_program install
	[ -x "$opt_qemu" ] ||\
		die "The qemu binary '$opt_qemu' is not executable."

	# debootstrap first stage.
	if [ $opt_skip_debootstrap1 -eq 0 ]; then
		info "Running debootstrap first stage..."
		debootstrap --arch="$opt_arch" --foreign --verbose \
			--keyring="$basedir/pilc/raspbian.public.key.gpg" \
			"$opt_suite" "$opt_target_dir" "$MAIN_MIRROR" \
			|| die "debootstrap failed"
		mkdir -p "$opt_target_dir/usr/share/keyrings" ||\
			die "Failed to create keyrings dir."
		cp "$basedir/pilc/raspbian.archive.public.key.gpg" \
		   "$opt_target_dir/usr/share/keyrings/" ||\
			die "Failed to copy raspbian.archive.public.key.gpg."
		cp /usr/share/keyrings/debian-archive-keyring.gpg \
		   "$opt_target_dir/usr/share/keyrings/debian-archive-keyring.gpg" ||\
			die "Failed to copy debian-archive-keyring.gpg."
	fi
	[ -d "$opt_target_dir" ] ||\
		die "Target directory '$opt_target_dir' does not exist."

	# Avoid the start of daemons during second stage.
	policy_rcd_file > "$opt_target_dir/usr/sbin/policy-rc.d" ||\
		die "Failed to create policy-rc.d"
	chmod 755 "$opt_target_dir/usr/sbin/policy-rc.d" ||\
		die "Failed to chmod policy-rc.d"

	# Copy qemu.
	local qemu_bin="$opt_target_dir/$opt_qemu"
	if ! [ -x "$qemu_bin" ]; then
		info "Copying qemu binary from '$opt_qemu' to '$qemu_bin'..."
		mkdir -p "$(dirname "$qemu_bin")" ||\
			die "Failed to make qemu base directory."
		cp "$opt_qemu" "$qemu_bin" ||\
			die "Failed to copy qemu binary."
	fi

	info "Copying PiLC bootstrap script..."
	cp "$basedir/pilc/pilc-bootstrap.sh" "$opt_target_dir/" ||\
		die "Failed to copy bootstrap script."

	info "Checking out awlsim..."
	local awlsim_dir="$opt_target_dir/tmp/awlsim"
	local checkout_dir="$awlsim_dir/src"
	rm -rf "$awlsim_dir"
	mkdir -p "$awlsim_dir" || die "mkdir failed"
	git clone --no-checkout "$basedir/.git" "$checkout_dir" ||\
		die "Failed to clone"
	(
		cd "$checkout_dir" ||\
			die "Failed to cd"
		git checkout "$opt_branch" ||\
			die "Failed to check out branch."
		git submodule update --init submodules/pyprofibus ||\
			die "Failed to pull pyprofibus submodule"
		rm -r .git submodules/pyprofibus/.git ||\
			die "Failed to remove .git directory."
		mv submodules/pyprofibus .. ||\
			die "Failed to move pyprofibus submodule."
	) || die

	# Fetch RPi.GPIO
	download "$opt_target_dir/tmp/RPi.GPIO-$RPIGPIO_VERSION.tar.gz" \
		"$RPIGPIO_MIRROR" "$RPIGPIO_SHA256"

	# Copy kernel tree
	if [ "$opt_kernel" = "pilc" ]; then
		info "Copying PiLC kernel tree..."
		rsync -rlt --inplace --exclude='.git*' \
			"$basedir/pilc/kernel/" "$opt_target_dir/tmp/kernel/" ||\
			die "Failed to copy PiLC kernel."
		cp "$basedir/pilc/kernel.config" "$opt_target_dir/tmp/kernel/.config" ||\
			die "Failed to copy PiLC kernel config."
	fi

	# Second stage will mount a few filesystems.
	# Keep track to umount them in cleanup.
	mp_proc="$opt_target_dir/proc"
	mp_sys="$opt_target_dir/sys"
	mp_shm="$opt_target_dir/dev/shm"
}

pilc_bootstrap_second_stage()
{
	info "Running second stage ($opt_arch)..."

	[ -x /pilc-bootstrap.sh ] ||\
		die "Second stage does not contain the bootstrap script."

	# Set up environment.
	export LC_ALL=C
	export LANGUAGE=C
	export LANG=C

	# debootstrap second stage.
	if [ $opt_skip_debootstrap2 -eq 0 ]; then
		info "Running debootstrap second stage..."
		/debootstrap/debootstrap --verbose --second-stage ||\
			die "Debootstrap second stage failed."
	fi

	info "Mounting /proc..."
	mkdir -p /proc ||\
		die "Failed to create /proc mountpoint."
	mount -t proc proc /proc ||\
		die "Mounting /proc failed."

	info "Mounting /sys..."
	mkdir -p /sys ||\
		die "Failed to create /sys mountpoint."
	mount -t sysfs sysfs /sys ||\
		die "Mounting /sys failed."

	info "Mounting /dev/shm..."
	mkdir -p /dev/shm ||\
		die "Failed to create /dev/shm mountpoint."
	mount -t tmpfs tmpfs /dev/shm ||\
		die "Mounting /dev/shm failed."

	info "Writing apt configuration..."
cat > /etc/apt/sources.list <<EOF
deb $MAIN_MIRROR $opt_suite main firmware rpi
deb http://archive.raspberrypi.org/debian/ $opt_suite main ui
EOF
	[ $? -eq 0 ] || die "Failed to set sources.list"
	echo 'Acquire { Languages "none"; };' > /etc/apt/apt.conf.d/99no-translations ||\
		die "Failed to set apt.conf.d"

	info "Creating /etc/fstab"
	mkdir -p /config ||\
		die "Failed to create /config"
	cat > /etc/fstab <<EOF
proc		/proc			proc		auto,defaults		0 0
debugfs		/sys/kernel/debug	debugfs		auto,defaults		0 0
configfs	/config			configfs	auto,defaults		0 0
tmpfs		/tmp			tmpfs		auto,mode=1777		0 0
/dev/mmcblk0p2	/			ext4		auto,noatime,errors=remount-ro	0 1
/dev/mmcblk0p1	/boot			vfat		auto,noatime		0 0
EOF

	info "Writing misc /etc stuff..."
	echo "PiLC" > /etc/hostname ||\
		die "Failed to set hostname"
	printf 'PiLC GNU/Linux (based on Raspbian) \\n \\l\n\n' > /etc/issue ||\
		die "Failed to create /etc/issue"
	printf 'PiLC GNU/Linux (based on Raspbian)\n' > /etc/issue.net ||\
		die "Failed to create /etc/issue.net"
	sed -i -e 's|PRETTY_NAME=.*|PRETTY_NAME="PiLC"|' \
		/etc/os-release ||\
		die "Failed to set os-release PRETTY_NAME."
	sed -i -e 's|NAME=.*|NAME="PiLC"|' \
		/etc/os-release ||\
		die "Failed to set os-release NAME."
	sed -i -e 's|ID=.*|ID=pilc|' \
		/etc/os-release ||\
		die "Failed to set os-release ID."
	sed -i -e 's|ID_LIKE=.*|ID_LIKE=raspbian|' \
		/etc/os-release ||\
		die "Failed to set os-release ID_LIKE."
	sed -i -e 's|HOME_URL=.*|HOME_URL="https://bues.ch/a/pilc"|' \
		/etc/os-release ||\
		die "Failed to set os-release HOME_URL."
	sed -i -e 's|SUPPORT_URL=.*|SUPPORT_URL="https://bues.ch/a/pilc"|' \
		/etc/os-release ||\
		die "Failed to set os-release SUPPORT_URL."
	sed -i -e 's|BUG_REPORT_URL=.*|BUG_REPORT_URL="https://bues.ch/a/pilc"|' \
		/etc/os-release ||\
		die "Failed to set os-release BUG_REPORT_URL."
	sed -i -e 's|#FSCKFIX=no|FSCKFIX=yes|' \
		/etc/default/rcS ||\
		die "Failed to set FSCKFIX=yes"

	info "Updating packages..."
cat <<EOF | debconf-set-selections
debconf	debconf/priority	select	high
debconf	debconf/frontend	select	Noninteractive
locales	locales/locales_to_be_generated	multiselect	en_US.UTF-8 UTF-8
locales	locales/default_environment_locale	select	None
EOF
	[ $? -eq 0 ] || die "Failed to configure debconf settings"
	apt-key add /usr/share/keyrings/raspbian.archive.public.key.gpg ||\
		die "apt-key add failed"
	apt-get -y update ||\
		die "apt-get update failed"
	apt-get -y dist-upgrade ||\
		die "apt-get dist-upgrade failed"

	info "Installing packages..."
	if [ "$opt_kernel" = "raspi" ]; then
		local bootloader=raspberrypi-bootloader
		apt-get -y purge raspberrypi-bootloader-nokernel ||\
			die "Failed to remove bootloader"
	elif [ "$opt_kernel" = "pilc" ]; then
		local bootloader=raspberrypi-bootloader-nokernel
		apt-get -y purge raspberrypi-bootloader ||\
			die "Failed to remove bootloader"
	else
		die "Invalid opt_kernel"
	fi
	apt-get -y install \
		"$bootloader" \
		aptitude \
		autoconf \
		automake \
		bc \
		build-essential \
		bwidget \
		console-setup \
		cython \
		cython3 \
		dbus \
		debconf-utils \
		devscripts \
		git \
		gnu-fdisk \
		htop \
		i2c-tools \
		irqbalance \
		libboost-python-dev \
		libgl1-mesa-dev \
		libglu1-mesa-dev \
		libglib2.0-dev \
		libgtk2.0-dev \
		libncurses5-dev \
		libmodbus-dev \
		libreadline-gplv2-dev \
		libtk-img \
		libudev-dev \
		libusb-1.0-0-dev \
		libxmu-dev \
		locales \
		nano \
		openssh-server \
		openssh-blacklist \
		openssh-blacklist-extra \
		parted \
		pkg-config \
		pypy \
		pypy-dev \
		python \
		python-all-dev \
		python-cairo \
		python-dev \
		python-gtk2 \
		python-rpi.gpio \
		python-serial \
		python-setuptools \
		python-smbus \
		python-tk \
		python3 \
		python3-all-dev \
		python3-cairo \
		python3-dev \
		python3-rpi.gpio \
		python3-serial \
		python3-setuptools \
		python3-smbus \
		python3-tk \
		schedtool \
		screen \
		sudo \
		systemd \
		tcl-dev \
		tclx \
		tk-dev \
		tmux \
		vim ||\
		die "apt-get install failed"
	apt-get install --reinstall libc6-dev ||\
		die "Failed to reinstall"
cat <<EOF | debconf-set-selections
debconf	debconf/frontend	select	Dialog
locales	locales/default_environment_locale	select	en_US.UTF-8
EOF
	[ $? -eq 0 ] || die "Failed to configure debconf settings"
	dpkg-reconfigure -u locales ||\
		die "Failed to reconfigure locales"
	apt-get -y clean ||\
		die "apt-get clean failed"

	# Build RPi.GPIO
	info "Building RPi.GPIO $RPIGPIO_VERSION for PyPy..."
	(
		cd /tmp || die "Failed to cd /tmp"
		tar xf "RPi.GPIO-$RPIGPIO_VERSION.tar.gz" ||\
			die "Failed to unpack RPi.GPIO-$RPIGPIO_VERSION.tar.gz"
		cd "RPi.GPIO-$RPIGPIO_VERSION" ||\
			die "Failed to cd RPi.GPIO-$RPIGPIO_VERSION"
		pypy ./setup.py install ||\
			die "Failed to install RPi.GPIO-$RPIGPIO_VERSION"
	) || die
	rm -r /tmp/RPi.GPIO* ||\
		die "Failed to remove RPi.GPIO."

	# Build and install kernel
	if [ "$opt_kernel" = "pilc" ]; then
		info "Building PiLC kernel..."
		(
			cd "/tmp/kernel" ||\
				die "Failed to cd to /tmp/kernel"
			make oldconfig ||\
				die "Kernel oldconfig failed"
			make -j "$_NPROCESSORS_ONLN" ||\
				die "Kernel build failed"
			#TODO
		) || die
		die "TODO: PiLC kernel"
	fi

	info "Removing ssh keys..."
	if [ -e "$(first /etc/ssh/ssh_host_*_key*)" ]; then
		rm /etc/ssh/ssh_host_*_key* ||\
			die "Failed to remove ssh keys."
	fi
	echo 1 > /etc/ssh/sshd_not_to_be_run ||\
		die "Failed to create /etc/ssh/sshd_not_to_be_run"
	echo 1 > /etc/ssh/ssh_create_keys ||\
		die "Failed to create /etc/ssh/ssh_create_keys"

	info "Creating /etc/rc.local..."
	cat > /etc/rc.local <<EOF
#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#

# Re-generate ssh keys, if requested.
if [ -e /etc/ssh/ssh_create_keys ]; then
	/bin/rm -f /etc/ssh/ssh_host_*_key*
	LC_ALL=C LANGUAGE=C LANG=C /usr/sbin/dpkg-reconfigure openssh-server
	/bin/rm /etc/ssh/sshd_not_to_be_run
	/bin/rm /etc/ssh/ssh_create_keys
	/etc/init.d/ssh start
fi

# Workaround firmware issue leaving i2c0 in an non-ALT0 state.
for i in 28 29; do
	/bin/echo \$i > /sys/class/gpio/export
	/bin/echo in > /sys/class/gpio/gpio\${i}/direction
done

# Add HAT eeprom device.
if ! [ -d "/sys/class/i2c-adapter/i2c-0/0-0050" ]; then
	/bin/echo "24c32 0x50" > /sys/class/i2c-adapter/i2c-0/new_device
fi

# Add /dev/ttyS0 link for convenience.
if ! [ -e /dev/ttyS0 ]; then
	/bin/ln -s /dev/ttyAMA0 /dev/ttyS0
fi

exit 0
EOF
	[ $? -eq 0 ] || die "Failed to create /etc/rc.local"

	info "Creating /etc/modules-load.d/i2c.conf..."
	cat > /etc/modules-load.d/i2c.conf <<EOF
i2c-dev
i2c-bcm2708
at24
rtc-rv3029c2
EOF
	[ $? -eq 0 ] || die "Failed to create /etc/modules-load.d/i2c.conf"

	info "Creating users/groups..."
	userdel -f pi
	groupdel pi
	rm -rf /home/pi
	groupadd -g 1000 pi ||\
		die "Failed to create group pi."
	useradd -u 1000 -d /home/pi -m -g pi\
		-G pi,lp,dialout,cdrom,floppy,audio,dip,src,video,plugdev,netdev,i2c\
		-s /bin/bash\
		pi ||\
		die "Failed to create user pi."
	printf 'raspberry\nraspberry\n' | passwd pi ||\
		die "Failed to set 'pi' password."
	echo 'pi ALL=(ALL:ALL) ALL' > "/etc/sudoers.d/00-pi" ||\
		die "Failed to create /etc/sudoers.d/00-pi"

	info "Initializing home directory..."
	mkdir -p /home/pi/.vim || die "Failed to mkdir /home/pi/.vim"
	cat > /home/pi/.vim/vimrc <<EOF
set nocompatible
set autoindent
syntax enable
set backspace=indent,start
set number
EOF
	[ $? -eq 0 ] || die "Failed to create /home/pi/.vim/vimrc"
	cat > /home/pi/.tmux.conf <<EOF
# Default new panes and windows to be opened in the current panes path
bind-key c new-window -c "#{pane_current_path}"
bind-key % split-window -h -c "#{pane_current_path}"
bind-key "\"" split-window -c "#{pane_current_path}"
EOF
	[ $? -eq 0 ] || die "Failed to create /home/pi/.tmux.conf"

	info "Building awlsim..."
	(
		cd /tmp/awlsim/src ||\
			die "Failed to cd"
		if [ $opt_cython -eq 0 ]; then
			# Disable cython
			sed -i -e '/Package: cython/,/^$/ d' \
				debian/control ||\
				die "Failed to patch control file"
			sed -i -e 's/export AWLSIM_CYTHON=1/export AWLSIM_CYTHON=0/' \
				debian/rules ||\
				die "Failed to patch rules file"
		fi
		debuild -uc -us -b -d || die "debuild failed"
		info "Built awlsim files:"
		ls .. || die "Failed to list results"

		info "Installing awlsim..."
		# Core
		dpkg -i ../python-awlsim_*.deb ||\
			die "Failed to install python-awlsim"
		dpkg -i ../python3-awlsim_*.deb ||\
			die "Failed to install python3-awlsim"
		dpkg -i ../pypy-awlsim_*.deb ||\
			die "Failed to install pypy-awlsim"
		if [ $opt_cython -ne 0 ]; then
			dpkg -i ../cython-awlsim_*.deb ||\
				die "Failed to install cython-awlsim"
			dpkg -i ../cython3-awlsim_*.deb ||\
				die "Failed to install cython3-awlsim"
		fi
		# hardware: dummy
		dpkg -i ../python-awlsimhw-dummy_*.deb ||\
			die "Failed to install python-awlsimhw-dummy"
		dpkg -i ../python3-awlsimhw-dummy_*.deb ||\
			die "Failed to install python3-awlsimhw-dummy"
		dpkg -i ../pypy-awlsimhw-dummy_*.deb ||\
			die "Failed to install pypy-awlsimhw-dummy"
		if [ $opt_cython -ne 0 ]; then
			dpkg -i ../cython-awlsimhw-dummy_*.deb ||\
				die "Failed to install cython-awlsimhw-dummy"
			dpkg -i ../cython3-awlsimhw-dummy_*.deb ||\
				die "Failed to install cython3-awlsimhw-dummy"
		fi
		# hardware: linuxcnc
		dpkg -i ../python-awlsimhw-linuxcnc_*.deb ||\
			die "Failed to install python-awlsimhw-linuxcnc"
		dpkg -i ../python3-awlsimhw-linuxcnc_*.deb ||\
			die "Failed to install python3-awlsimhw-linuxcnc"
		if [ $opt_cython -ne 0 ]; then
			dpkg -i ../cython-awlsimhw-linuxcnc_*.deb ||\
				die "Failed to install cython-awlsimhw-linuxcnc"
			dpkg -i ../cython3-awlsimhw-linuxcnc_*.deb ||\
				die "Failed to install cython3-awlsimhw-linuxcnc"
		fi
		# hardware: profibus
		dpkg -i ../python-awlsimhw-profibus_*.deb ||\
			die "Failed to install python-awlsimhw-profibus"
		dpkg -i ../python3-awlsimhw-profibus_*.deb ||\
			die "Failed to install python3-awlsimhw-profibus"
		dpkg -i ../pypy-awlsimhw-profibus_*.deb ||\
			die "Failed to install pypy-awlsimhw-profibus"
		if [ $opt_cython -ne 0 ]; then
			dpkg -i ../cython-awlsimhw-profibus_*.deb ||\
				die "Failed to install cython-awlsimhw-profibus"
			dpkg -i ../cython3-awlsimhw-profibus_*.deb ||\
				die "Failed to install cython3-awlsimhw-profibus"
		fi
		# hardware: RPi GPIO
		dpkg -i ../python-awlsimhw-rpigpio_*.deb ||\
			die "Failed to install python-awlsimhw-rpigpio"
		dpkg -i ../python3-awlsimhw-rpigpio_*.deb ||\
			die "Failed to install python3-awlsimhw-rpigpio"
		dpkg -i ../pypy-awlsimhw-rpigpio_*.deb ||\
			die "Failed to install pypy-awlsimhw-rpigpio"
		if [ $opt_cython -ne 0 ]; then
			dpkg -i ../cython-awlsimhw-rpigpio_*.deb ||\
				die "Failed to install cython-awlsimhw-rpigpio"
			dpkg -i ../cython3-awlsimhw-rpigpio_*.deb ||\
				die "Failed to install cython3-awlsimhw-rpigpio"
		fi
		# PiLC libraries
		dpkg -i ../python-libpilc_*.deb ||\
			die "Failed to install python-libpilc"
		dpkg -i ../python3-libpilc_*.deb ||\
			die "Failed to install python3-libpilc"
		# Executables
		dpkg -i ../awlsim-server_*.deb ||\
			die "Failed to install awlsim-server"
		dpkg -i ../awlsim-client_*.deb ||\
			die "Failed to install awlsim-client"
		dpkg -i ../awlsim-symtab_*.deb ||\
			die "Failed to install awlsim-symtab"
		dpkg -i ../awlsim-test_*.deb ||\
			die "Failed to install awlsim-test"
		dpkg -i ../awlsim-proupgrade_*.deb ||\
			die "Failed to install awlsim-proupgrade"
		dpkg -i ../awlsim-linuxcnc-hal_*.deb ||\
			die "Failed to install awlsim-linuxcnc-hal"
		dpkg -i ../pilc-hat-conf_*.deb ||\
			die "Failed to install pilc-hat-conf"
		# GUI and misc
		mkdir -p /home/pi/awlsim-gui ||\
			die "mkdir /home/pi/awlsim-gui failed"
		cp ../python*-awlsim-gui_*.deb ../awlsim-gui_*.deb \
			/home/pi/awlsim-gui/ ||\
			die "Failed to copy awlsim-gui"
		cp examples/EXAMPLE.awlpro /home/pi/generic-example.awlpro ||\
			die "Failed to copy EXAMPLE.awlpro."
		cp examples/raspberrypi-gpio.awlpro /home/pi/raspberrypi-gpio-example.awlpro ||\
			die "Failed to copy raspberrypi-gpio.awlpro."
		cp examples/raspberrypi-profibus.awlpro /home/pi/raspberrypi-profibus-example.awlpro ||\
			die "Failed to copy raspberrypi-profibus.awlpro."

		#TODO run the testsuite

		#TODO install unit via package
		info "Installing awlsim service unit..."
		local awlsim_prefix=/usr
		local site="$awlsim_prefix/lib/python2.7/dist-packages"
		cat awlsim-server.service.in |\
		sed -e 's|@USER@|root|g' \
		    -e 's|@GROUP@|root|g' \
		    -e "s|@PREFIX@|$awlsim_prefix|g" \
		    -e 's|@PROJECT@|/etc/awlsim-server.awlpro|g' \
		    -e 's|@PYTHON@|/usr/bin/python2.7|g' \
		    -e "s|@PYTHON_SITE@|$site|g" >\
		    /etc/systemd/system/awlsim-server.service ||\
		    die "Failed to create awlsim-server.service"
		systemctl enable awlsim-server.service ||\
			die "Failed to enable awlsim-server-service"
	) || die
	info "Building pyprofibus..."
	(
		cd /tmp/awlsim/pyprofibus ||\
			die "Failed to cd"
		debuild -uc -us -b -d || die "debuild failed"
		info "Built pyprofibus files:"
		ls .. || die "Failed to list results"

		info "Installing pyprofibus..."
		dpkg -i ../python-pyprofibus_*.deb ||\
			die "Failed to install python-pyprofibus"
		dpkg -i ../python3-pyprofibus_*.deb ||\
			die "Failed to install python3-pyprofibus"
		dpkg -i ../pypy-pyprofibus_*.deb ||\
			die "Failed to install pypy-pyprofibus"
		dpkg -i ../profisniff_*.deb ||\
			die "Failed to install profisniff"
		dpkg -i ../gsdparser_*.deb ||\
			die "Failed to install gsdparser"
		dpkg -i ../pyprofibus-linuxcnc-hal_*.deb ||\
			die "Failed to install pyprofibus-linuxcnc-hal"
	) || die
	rm -r /tmp/awlsim ||\
		die "Failed to remove awlsim checkout."

	info "Extending pi user environment..."
	cat >> /home/pi/.bashrc <<EOF

# PiLC
for __i in /opt/awlsim/lib/python*/site-packages/; do
	export PYTHONPATH="\$PYTHONPATH:\$__i"
done
export PATH="\$PATH:/opt/awlsim/bin"
EOF
	[ $? -eq 0 ] || die "Failed to extend /home/pi/.bashrc"

	info "Configuring network..."
	cat > /etc/network/interfaces.d/lo <<EOF
auto lo
iface lo inet loopback
EOF
	[ $? -eq 0 ] || die "Failed to create /etc/network/interfaces.d/lo"
	for i in $(seq 0 9); do
		cat > /etc/network/interfaces.d/eth$i <<EOF
allow-hotplug eth$i
iface eth$i inet dhcp
iface eth$i inet6 auto
EOF
		[ $? -eq 0 ] || die "Failed to create /etc/network/interfaces.d/eth$i"
	done

	info "Updating home directory permissions..."
	chown -R pi:pi /home/pi || die "Failed to change /home/pi permissions."

	# Remove rc.d policy file
	if [ -e /usr/sbin/policy-rc.d ]; then
		rm /usr/sbin/policy-rc.d ||\
			die "Failed to remove policy-rc.d"
	fi

	info "Stopping processes..."
	for i in dbus ssh atd irqbalance; do
		/etc/init.d/$i stop
	done

	info "Umounting /dev/shm..."
	umount /dev/shm || die "Failed to umount /dev/shm"
	info "Umounting /sys..."
	umount /sys || die "Failed to umount /sys"
	info "Umounting /proc..."
	umount /proc || die "Failed to umount /proc"
}

pilc_bootstrap_third_stage()
{
	info "Running third stage..."

	info "Removing PiLC bootstrap script..."
	rm "$opt_target_dir/pilc-bootstrap.sh" ||\
		die "Failed to remove bootstrap script."

	info "Configuring boot..."
	cat > "$opt_target_dir/boot/cmdline.txt" <<EOF
dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet
EOF
	[ $? -eq 0 ] || die "Failed to create /boot/cmdline.txt"
	boot_config_file > "$opt_target_dir/boot/config.txt" ||\
		die "Failed to create /boot/config.txt"
	local img="$(first "$opt_target_dir/boot/"vmlinuz-*-rpi)"
	if [ -e "$img" ]; then
		mv "$img" "$opt_target_dir/boot/kernel.img" ||\
			die "Failed to create kernel.img"
	fi
	local img="$(first "$opt_target_dir/boot/"initrd.img-*-rpi)"
	if [ -e "$img" ]; then
		mv "$img" "$opt_target_dir/boot/initrd.img" ||\
			die "Failed to create initrd.img"
	fi
	local img="$(first "$opt_target_dir/boot/"vmlinuz-*-rpi2)"
	if [ -e "$img" ]; then
		mv "$img" "$opt_target_dir/boot/kernel7.img" ||\
			die "Failed to create kernel7.img"
	fi
	local img="$(first "$opt_target_dir/boot/"initrd.img-*-rpi2)"
	if [ -e "$img" ]; then
		mv "$img" "$opt_target_dir/boot/initrd7.img" ||\
			die "Failed to create initrd7.img"
	fi

	# Prepare image paths.
	local target_dir="$(readlink -m "${opt_target_dir}")"
	[ -n "$target_dir" ] || die "Failed to resolve target dir."
	local imgfile="${target_dir}${opt_imgsuffix}.img"
	local imgfile_zip="${imgfile}.7z"
	local bootimgfile="${imgfile}.boot"
	mp_bootimgfile="${bootimgfile}.mp"
	local rootimgfile="${imgfile}.root"
	mp_rootimgfile="${rootimgfile}.mp"
	rm -f "$imgfile" "$imgfile_zip" "$rootimgfile" "$bootimgfile"
	rmdir "$mp_bootimgfile" "$mp_rootimgfile" 2>/dev/null

	# Create images.
	if [ "$opt_img" -ne 0 ]; then
		info "Creating boot image..."
		mkfs.vfat -F 32 -i 7771B0BB -n boot -C "$bootimgfile" \
			$(expr \( 64 \* 1024 \) - \( 4 \* 1024 \) ) ||\
			die "Failed to create boot partition file system."
		mkdir "$mp_bootimgfile" ||\
			die "Failed to make boot partition mount point."
		mount -o loop "$bootimgfile" "$mp_bootimgfile" ||\
			die "Failed to mount boot partition."
		rsync -aHAX --inplace \
			"$target_dir/boot/" "$mp_bootimgfile/" ||\
			die "Failed to copy boot files."
		umount "$mp_bootimgfile" ||\
			die "Failed to umount boot partition."
		rmdir "$mp_bootimgfile" ||\
			die "Failed to remove boot partition mount point."

		info "Creating root image..."
		mkfs.ext4 "$rootimgfile" $(expr \( 4000 - 64 \) \* 1024 ) ||\
			die "Failed to create root filesystem."
		mkdir "$mp_rootimgfile" ||\
			die "Failed to make root partition mount point."
		mount -o loop "$rootimgfile" "$mp_rootimgfile" ||\
			die "Failed to mount root partition."
		rsync -aHAX --inplace \
			--exclude='boot/*' \
			--exclude='proc/*' \
			--exclude='sys/*' \
			--exclude='dev/shm/*' \
			--exclude='tmp/*' \
			--exclude="$(basename "$opt_qemu")" \
			"$target_dir/" "$mp_rootimgfile/" ||\
			die "Failed to copy root files."
		umount "$mp_rootimgfile" ||\
			die "Failed to umount root partition."
		rmdir "$mp_rootimgfile" ||\
			die "Failed to remove root partition mount point."

		info "Creating image '$imgfile'..."
		dd if=/dev/zero of="$imgfile" bs=1M count=4000 conv=sparse ||\
			die "Failed to create image file."
		parted "$imgfile" <<EOF
		    unit b
		    mklabel msdos
		    mkpart primary fat32 $(expr 4 \* 1024 \* 1024) $(expr 64 \* 1024 \* 1024 - 1)
		    mkpart primary ext4 $(expr 64 \* 1024 \* 1024) 100%
EOF
		[ $? -eq 0 ] || die "Failed to create partitions."

		info "Integrating boot image..."
		dd if="$bootimgfile" of="$imgfile"\
			seek=4 bs=1M conv=notrunc,sparse ||\
			die "Failed to integrate boot partition."
		rm "$bootimgfile" ||\
			die "Failed to delete boot partition image."

		info "Integrating root image..."
		dd if="$rootimgfile" of="$imgfile"\
			seek=64 bs=1M conv=notrunc,sparse ||\
			die "Failed to integrate root partition."
		rm "$rootimgfile" ||\
			die "Failed to delete root partition image."

		# Create zipped image.
		if [ "$opt_zimg" -ne 0 ]; then
			info "Compressing image..."
			7z -mx=9 a "$imgfile_zip" "$imgfile" ||\
				die "Failed to compress partition image."
		fi
	fi
}

usage()
{
	echo "pilc-bootstrap.sh [OPTIONS] TARGET_DIR"
	echo
	echo "Options:"
	echo
	echo " --branch|-b BRANCH      Select the awlsim branch or tag."
	echo "                         Default: $default_branch"
	echo
	echo " --no-cython|-C          Do not build Cython modules."
	echo "                         Default: Build cython modules"
	echo
	echo " --suite|-s SUITE        Select the suite."
	echo "                         Default: $default_suite"
	echo
	echo " --arch|-a ARCH          Select the default arch."
	echo "                         Default: $default_arch"
	echo
	echo " --kernel|-k KERNEL      Kernel selection. Any of:"
	echo "                         raspi  -> raspberrypi-bootloader package (default)"
	echo "                         pilc   -> PiLC kernel"
	echo
	echo " --qemu-bin|-q PATH      Select qemu-user-static binary."
	echo "                         Default: $default_qemu"
	echo
	echo " --img-suffix|-s SUFFIX  Image file suffix."
	echo "                         Default: $default_imgsuffix"
	echo
	echo " --no-img|-I             Do not create an image."
	echo "                         Default: Create image."
	echo
	echo " --no-zimg|-Z            Do not create a 7zipped image."
	echo "                         Default: Create 7zipped image."
	echo
	echo " --skip-debootstrap1|-1  Skip debootstrap first stage."
	echo " --skip-debootstrap2|-2  Skip debootstrap second stage."
}

# canonicalize basedir
basedir="$(readlink -e "$basedir")"
[ -n "$basedir" ] || die "Failed to canonicalize base directory."

# Mountpoints. Will be umounted on cleanup.
mp_shm=
mp_proc=
mp_sys=
mp_bootimgfile=
mp_rootimgfile=

trap term_signal TERM INT

if [ -z "$__PILC_BOOTSTRAP_SECOND_STAGE__" ]; then
	# First stage

	trap cleanup EXIT

	export _NPROCESSORS_ONLN="$(getconf _NPROCESSORS_ONLN)"
	[ -n "$_NPROCESSORS_ONLN" ] || die "Failed to get # of online CPUs"

	default_branch="master"
	default_suite="jessie"
	default_arch="armhf"
	default_kernel="raspi"
	default_qemu="/usr/bin/qemu-arm-static"
	default_imgsuffix="-$(date '+%Y%m%d')"
	default_img=1
	default_zimg=1

	opt_target_dir=
	opt_branch="$default_branch"
	opt_cython=1
	opt_suite="$default_suite"
	opt_arch="$default_arch"
	opt_kernel="$default_kernel"
	opt_qemu="$default_qemu"
	opt_skip_debootstrap1=0
	opt_skip_debootstrap2=0
	opt_imgsuffix="$default_imgsuffix"
	opt_img="$default_img"
	opt_zimg="$default_zimg"

	while [ $# -ge 1 ]; do
		case "$1" in
		--help|-h)
			usage
			exit 0
			;;
		--branch|-b)
			shift
			opt_branch="$1"
			[ -n "$opt_branch" ] || die "No branch given"
			;;
		--no-cython|-C)
			opt_cython=0
			;;
		--suite|-s)
			shift
			opt_suite="$1"
			[ -n "$opt_suite" ] || die "No suite given"
			;;
		--arch|-a)
			shift
			opt_arch="$1"
			[ -n "$opt_arch" ] || die "No arch given"
			;;
		--kernel|-k)
			shift
			opt_kernel="$1"
			[ "$opt_kernel" = "raspi" -o \
			  "$opt_kernel" = "pilc" ] || die "Invalid kernel"
			;;
		--qemu-bin|-q)
			shift
			opt_qemu="$1"
			[ -x "$opt_qemu" ] || die "No valid qemu binary given"
			;;
		--skip-debootstrap1|-1)
			opt_skip_debootstrap1=1
			;;
		--skip-debootstrap2|-2)
			opt_skip_debootstrap2=1
			;;
		--img-suffix|-s)
			shift
			opt_imgsuffix="$1"
			;;
		--no-zimg|-Z)
			opt_zimg=0
			;;
		--no-img|-I)
			opt_img=0
			;;
		*)
			opt_target_dir="$*"
			break
			;;
		esac
		shift
	done
	[ -n "$opt_target_dir" ] ||\
		die "No TARGET_DIR"
	[ -d "$opt_target_dir" -o ! -e "$opt_target_dir" ] ||\
		die "$opt_target_dir is not a directory"

	# Run first stage.
	pilc_bootstrap_first_stage

	info "Starting second stage."
	# Export options for use by second stage.
	export opt_target_dir
	export opt_branch
	export opt_cython
	export opt_suite
	export opt_arch
	export opt_kernel
	export opt_qemu
	export opt_skip_debootstrap1
	export opt_skip_debootstrap2
	export opt_imgsuffix
	export opt_zimg
	export opt_img
	export __PILC_BOOTSTRAP_SECOND_STAGE__=1
	chroot "$opt_target_dir" "/pilc-bootstrap.sh" ||\
		die "Chroot failed."

	# Run third stage.
	pilc_bootstrap_third_stage

	info ""
	info "Successfully bootstrapped PiLC."

	exit 0
else
	# Run second stage
	pilc_bootstrap_second_stage

	exit 0
fi
bues.ch cgit interface