aboutsummaryrefslogtreecommitdiff
blob: 66514196e8b091aa2ded645909a9468881a1e36e (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
#!/bin/bash

# SUBSHELL
_subshell() {
	echo -e "\e[33mCalling bash subshell\e[0m"
	sleep 2
	echo 'echo -e "\e[33mInside Subshell\e[0m"' >> /root/.bashrc
	echo 'echo -e "\e[33mExit to return back to parent\e[0m"' >> /root/.bashrc
	(clear; exec /bin/bash;)
	sed -i "/Inside Subshell/d" "/root/.bashrc"
	sed -i "/Exit to return back to parent/d" "/root/.bashrc"
	echo -e "\e[33mYou are back to parent\e[0m"
}

die() {
	echo "$@" 1>&2 ; exit 1
}
	
pass() {
	echo -e "[\e[34mDone\e[0m]"
}

inspector() {
	[[ -z "$@" ]] && print_inf_f
}

write_fail() {
	echo "FAILED" >> "${CHDIR}/emergeresume"	
}

delete_fail() {
	sed -i "/FAILED/d" "${CHDIR}/emergeresume"
}

_e_report_back() {
	echo -e "\e[33m$*\e[0m"
}

_o_report_back() {
	echo -e "\e[34m$*\e[0m"
}

# ALL ERROR MESSAGES BUT FIRST
_print_info() {
	case $1 in
		1)
			echo -e "[\e[32m*\e[0m] \e[34mDone\e[0m";;
		2)
			echo -e "[\e[31m*\e[0m] \e[31mFAILED\e[0m";;
		3)
			clear
			echo "If this message is printed while using the Maim Menu"
			echo "That means essential files are altered or something bad is happening."
			echo
			echo "Please run a health-check from the ~Main Menu~ and a Version check first."
			echo "If you see this again after the health/version check, please submit a bug report"
			echo "and stop using the program, or data loss may occur."
			echo
			echo "Exiting..."
			sleep 2
			exit 1;;
		4)
			echo -e "[\e[31m*\e[0m] [\e[31mERROR\e[0m: Wrong parameters"
			exit 1;;
		5)
			echo -e "[\e[31m*\e[0m] Failed getting version";;
		6)
			echo -e "[\e[31m*\e[0m] FATAL";;
	esac
}

# PROCESS FLOW TWEAKER. DO NOT EDIT THIS.
_monitor_chroot_flow() {
	case "$1" in
		UPDATE)
			sed -i -e 's/UPDATE=0/UPDATE=1/g' "${CHDIR}/chsinprog";;
		2)
			sed -i -e 's/BOOTSTRAP=0/BOOTSTRAP=1/g' "${CHDIR}/chsinprog";;
		REBUILD)
			sed -i -e 's/REBUILD=0/REBUILD=1/g' "${CHDIR}/chsinprog";;
		CONFIGURE)
			sed -i -e 's/CONFIGURE=0/CONFIGURE=1/g' "${CHDIR}/chsinprog";;
		INSTALL)
			sed -i -e 's/INSTALL=0/INSTALL=1/g' "${CHDIR}/chsinprog";;
		RUNLEVEL)
			sed -i -e 's/RUNLEVEL=0/RUNLEVEL=1/g' "${CHDIR}/chsinprog";;
		KERNEL)
			sed -i -e 's/KERNEL=0/KERNEL=1/g' "${CHDIR}/chsinprog";;
		INITRAMFS)
			sed -i -e 's/INITRAMFS=0/INITRAMFS=1/g' "${CHDIR}/chsinprog";;
		PORTAGE)
			sed -i -e 's/PORTAGE=0/PORTAGE=1/g' "${CHDIR}/chsinprog";;
		CLEAN)
			sed -i -e 's/CLEAN=0/CLEAN=1/g' "${CHDIR}/chsinprog";;
		CHROOTPART)
			sed -i -e 's/CHROOTPART=0/CHROOTPART=1/g' "${CHDIR}/chsinprog";;
		dn) ;;
	esac
}

_emerge_depclear() {
	# DEPCLEAN BEFORE APPLYING NEW PROFILE CHANGES
	echo -e "\e[34mRunning depclean before proceeding...\e[0m"
	if emerge --depclean >LAST_ACTION 2>&1; then
		_e_report_back "Depclean completed"
	else
		echo "emerge --depclean" >LAST_FAILED_COMMAND
		combat_medic
	fi
}

dist_ccashe_f() {
	# ADD OR REMOVE DISTCC FROM THE SYSTEM
	if [[ -n "$(echo ${CHFEATURES} | grep distcc)" ]]; then
		if ! eix --installed sys-devel/distcc >LAST_ACTION 2>&1; then
			echo -e "Configuring [\e[32mdistcc\e[0m]"
			if emerge -q sys-devel/distcc >LAST_ACTION 2>&1; then
				echo -e "[\e[34mConfigured\e[0m]"
			else
				echo "emerge -q sys-devel/distcc" >LAST_FAILED_COMMAND
				combat_medic
			fi

			mkdir -p /etc/distcc \
			&& cp "${CHDIR}/hosts" /etc/distcc/hosts \
			&& echo -e "Configuring [\e[32mdistcc hosts\e[0m]" \
			|| echo -e "Faild configuring [\e[33mdistcc hosts\e[0m]"
		fi
	else
		if eix --installed sys-devel/distcc >LAST_ACTION 2>&1; then
			echo -e "Removing [\e[32mdistcc\e[0m]" \
			&& emerge --deselect sys-devel/distcc >LAST_ACTION 2>&1
			echo -e "[\e[34mRemoved\e[0m]"
		fi
	fi
	
	if [[ -n "${CCCACHESIZE}" ]]; then
		if ! eix --installed dev-util/ccache >LAST_ACTION 2>&1; then
			echo -e "Configuring [\e[32mccache\e[0m]"
			if emerge -q dev-util/ccache >LAST_ACTION 2>&1; then
				echo -e "[\e[34mConfigured\e[0m]"
			else
				echo "emerge -q dev-util/ccache" >LAST_FAILED_COMMAND
				combat_medic
			fi
		fi
	else
		if eix --installed dev-util/ccache >LAST_ACTION 2>&1; then
			echo -e "Removing [\e[33mccache\e[0m]" \
			&& emerge --deselect sys-devel/distcc >LAST_ACTION 2>&1
			echo -e "[\e[34mRemoved\e[0m]"
		fi
	fi
}

port_prof_f() {
	# GSE PROFILE DIRECTORIES AND FILES
	if mkdir -p /usr/local/portage/{metadata,profiles}; then
		chown -R portage:portage /usr/local/portage || return 1
		echo 'gse' > /usr/local/portage/profiles/repo_name || return 1
		cp "${CHDIR}/localrepo/layout.conf" "/usr/local/portage/metadata/layout.conf" || return 1
		cp "${CHDIR}/localrepo/localrepo.conf" "/etc/portage/repos.conf/localrepo.conf" || return 1
		
		mkdir -p "/usr/local/portage/profiles/gse" \
		&& mkdir -p "/usr/local/portage/profiles/default/linux/${ARCH}/13.0/gse" \
		&& cp "${CHDIR}/profiles/parent-gse/eapi" \
		"/usr/local/portage/profiles/gse/eapi" \
		&& cp "${CHDIR}/profiles/parent-gse/make.defaults" \
		"/usr/local/portage/profiles/gse/make.defaults" \
		&& cp "${CHDIR}/profiles/parent-gse/package.use" \
		"/usr/local/portage/profiles/gse/package.use" \
		&& cp "${CHDIR}/profiles/parent-gse/package.use.force" \
		"/usr/local/portage/profiles/gse/package.use.force" \
		&& cp "${CHDIR}/profiles/child-gse/eapi" \
		"/usr/local/portage/profiles/default/linux/${ARCH}/13.0/gse/eapi" \
		&& cp "${CHDIR}/profiles/child-gse/parent" \
		"/usr/local/portage/profiles/default/linux/${ARCH}/13.0/gse/parent" \
		&& echo "# Local profile" > "/usr/local/portage/profiles/profiles.desc" \
		&& echo "amd64           default/linux/amd64/13.0/gse                            exp" \
		>> "/usr/local/portage/profiles/profiles.desc" \
		&& echo -e "[\e[32m*\e[0m] Creating [\e[34mprofile\e[0m]" \
		|| { echo -e "[\e[31m*\e[0m] Creating [\e[34profile\e[0m]"; return 1; }
	else
		echo -e "[\e[31m*\e[0m] Creating [\e[34profile\e[0m]"
		return 1
	fi
	# END OF GSE PROFILE
}

part_portage() {
	# MAKE.CONF, PACKAGE.USE, FEATURES AND CCACHE
	if rm -f /etc/portage/make.conf; then
		rm -r /etc/portage/package.use/sysbuild >LAST_ACTION 2>&1
		mkdir -p "/etc/portage/package.use" || exit 1
		cp "${CHDIR}/make.conf" /etc/portage/make.conf || exit 1
		echo -e "\e[34mConfigured\e[0m [\e[32mmake.conf\e[0m]"
		if cp "${CHDIR}/package.use/sysbuild" "/etc/portage/package.use"; then
			echo -e "\e[34mConfigured\e[0m [\e[32mpackage.use\e[0m]"
			echo "FEATURES=\"\${FEATURES} ${CHFEATURES}\"" >> /etc/portage/make.conf
			echo -e "\e[34mConfigured\e[0m [\e[32mFEATURES\e[0m]"
			if [[ -n "${CCCACHESIZE}" ]]; then
				echo "CCACHE=\"${CCCACHESIZE}\"" >> /etc/portage/make.conf
				echo -e "\e[34mConfigured\e[0m [\e[32mCCACHE=${CCCACHESIZE}\e[0m]"
			else
				ask_for_shell "Could not configure CCACHE"
			fi

			dist_ccashe_f || return 1
			_emerge_depclear
			port_prof_f || return 1
		else
			echo -e "\e[31mFaild\e[0m Configuring [\e[32mpackage.use\e[0m]"
			ask_for_shell "Could not configure package.use files"
		fi
	else
		echo -e "\e[31mFaild\e[0m Configuring [\e[32mmake.conf\e[0m]"
		exit 1
	fi

	if echo "$@" | grep -q 'cforce' || ! echo "$@" | grep -q '_flag_auto'; then
		echo -e "\e[34mEnabling basic profile\e[0m]"
		eselect profile set 1 && echo -e "\e[34mEnabled\e[0m"  \
		|| ask_for_shell "Could not enable basic profile"
	else
		# SELECTING PROFILE
		echo -e "Do you wish to enable \e[33mGSE\e[0m \e[34mprofile\e[0m ::: (\e[31mexperimental\e[0m)?"
		echo -e "Say no here, to select a basic \e[35mGentoo\e[0m \e[34mprofile\e[0m manually"
		echo "Answer Y/N: "
		read -rp "Input :: <= " YN
		while true; do
			case "${YN}" in
				[yY])
					GSE_PROF=$(eselect profile list | grep gse | awk -F '[' '{print $2}' | awk -F ']' '{print $1}') \
					&& eselect profile set "${GSE_PROF}" && echo -e "\e[34mEnabled\e[0m" && BREAKVAR=0 \
					|| { _print_info 2 && YN=n; };;
				[nN])
					eselect profile list
					echo -e "Please select a \e[34mprofile\e[0m listed from above:"
					echo -e "Answer is only related \e[34mprofile\e[0m numbers"
					read -rp "Input :: <= " ANS
					[[ -n "${ANS}" ]] && eselect profile set "${ANS}" && echo -e "\e[34mEnabled\e[0m" && BREAKVAR=0 \
					|| { _print_info 2 && BREAKVAR=1; };;
			esac
			if [[ "${BREAKVAR}" == 0 ]]; then
				break 
			else
				echo "Could not set a profile"
				YN=n
			fi
		done
	fi
	_apply_new "$@"
	unset YN
	unset ANS
	unset BREAKVAR
}

_apply_new() {
	# APPLYING PROFILE CHANGES
	echo -e "[\e[32m*\e[0m] \e[34mApplying new profile changes\e[0m"
	if emerge -q --update --deep --newuse --with-bdeps=y @world; then
		_e_report_back "Changes successfully applied"
	else
		echo "emerge -q --update --deep --newuse --with-bdeps=y @world" > LAST_FAILED_COMMAND
		combat_medic
	fi
}

build_the_world() {
	# EMERGIN WORLD
	if ! echo "$@" | grep -q '_flag_auto'; then
		while true; do
			echo "----------------------------------------------------------------------------"
			echo "Do you wish to rebuild the system?"
			echo "If catalyst built this system, simply say no"
			echo "Answer Yes or No: "
			read -rp "Input :: <= " YN
			case "${YN}" in
				[yY][eE][sS]|[yY])
					if emerge -eq @world; then
						_e_report_back "World rebuild has been completed"
						_monitor_chroot_flow "REBUILD"; break
					else
						echo "emerge -eq @world" > LAST_FAILED_COMMAND
						combat_medic
					fi;;
				[nN][oO]|[nN])
					echo "Exiting Rebuilding Part"
					sleep 1
					_monitor_chroot_flow "REBUILD"; break;;
			esac
		done
		unset YN
	fi
}


_update_portage(){
	if emerge -q --deep --update portage >LAST_ACTION 2>&1; then
		_e_report_back "Portage update was successful"
	else
		echo "emerge -q --deep --update portage" > LAST_FAILED_COMMAND
		combat_medic
	fi
}

_emerge_eix() {
	if emerge -q app-portage/eix >LAST_ACTION 2>&1; then
		_e_report_back "Emerge of eix was successful"
	else
		echo "emerge -q app-portage/eix" > LAST_FAILED_COMMAND
		combat_medic
	fi
}

_update_eix() {
	if eix-update >LAST_ACTION 2>&1; then
		_e_report_back "Update of eix was successful"
		_monitor_chroot_flow "UPDATE"
	else
		echo "eix-update" > LAST_FAILED_COMMAND
		ask_for_shell "Updating eix failed"
	fi
}

_emerge_gentoolkit() {
	if emerge -q app-portage/gentoolkit >LAST_ACTION 2>&1; then
		_e_report_back "Emerge of gentoolkit was successful"
	else
		echo "emerge -q app-portage/gentoolkit" > LAST_FAILED_COMMAND
		combat_medic
	fi
}

test_stage_f() {
	echo -e "\e[31m$1\e[0m"
}

_portage_subpart() {
	mkdir -p "/etc/portage/repos.conf"
	cp /usr/share/portage/config/repos.conf /etc/portage/repos.conf/gentoo.conf

	if [[ -n $(echo "$@" | grep 'catalyst') ]]; then
		#emerge --sync >LAST_ACTION 2>&1 && pass || { echo "emerge --sync" > LAST_FAILED_COMMAND; combat_medic; }
		rsync -aAXhrv root@192.168.2.2:/usr/portage/ /usr/portage/ >LAST_ACTION 2>&1 && pass || { echo "emerge--webrsync" > LAST_FAILED_COMMAND; combat_medic; }
		env-update >LAST_ACTION 2>&1
	elif [[ -n $(echo "$@" | grep 'precomp') ]]; then
		#emerge-webrsync >LAST_ACTION 2>&1 && pass || { echo "emerge--webrsync" > LAST_FAILED_COMMAND; combat_medic; }
		rsync -aAXhrv root@192.168.2.2:/usr/portage/ /usr/portage/ >LAST_ACTION 2>&1 && pass || { echo "emerge--webrsync" > LAST_FAILED_COMMAND; combat_medic; }
		env-update >LAST_ACTION 2>&1
	fi

	echo -e "\e[34mUpdating Portage\e[0m"
	_update_portage
	echo -e "\e[34mSearching for eix\e[0m"
	if [[ -z $(command -v eix) ]]; then
		echo -e "\e[33mCould not locate eix\e[0m"
		echo -e "\e[34mEmerging eix\e[0m"
		_emerge_eix
		echo -e "\e[34mUpdating eix database\e[0m"
		if _update_eix; then
			:
		else
			combat_medic
		fi
	else
		echo -e "\e[33meix located\e[0m"
		echo -e "\e[34mUpdating eix database\e[0m"
		if _update_eix; then
			:
		else
			combat_medic
		fi	
	fi
	if ! eix --installed app-portage/gentoolkit >LAST_ACTION 2>&1; then
		echo -e "\e[34mEmerging app-portage/gentoolkit\e[0m"
		_emerge_gentoolkit
		echo -e "\e[34mRunning\e[0m \e[35mGentoo's\e[0m \e[34mReverse Dependency rebuilder\e[0m"
		if revdep-rebuild; then
			_e_report_back "Reverse Dependency Completed"
		else
			combat_medic
		fi
	else
		echo -e "\e[34mRunning\e[0m \e[35mGentoo's\e[0m \e[34mReverse Dependency rebuilder\e[0m"
		if revdep-rebuild; then
			_e_report_back "Reverse Dependency Completed"
		else
			combat_medic
		fi
	fi
}

_part_c() {
	echo "============================================================================"
	echo -e "\e[35m 			Part C: Syncing Portage\e[0m"
	echo "============================================================================"
	echo -e "\e[34mUpdating the database\e[0m"
	_portage_subpart "$@"
}

_configure_timezone() {
	if [[ "${TIMEZONE}" != TMZ ]]; then
		echo "----------------------------------------------------------------------------"
		echo "${TIMEZONE}" > /etc/timezone && echo -e "[\e[32m*\e[0m] Configuring \e[34mTimezone\e[0m"
	else
		echo "----------------------------------------------------------------------------"
		echo "UTC" > /etc/timezone && echo -e "[\e[32m*\e[0m] \e[34mConfiguring Timezone\e[0m"
	fi

	if emerge --config sys-libs/timezone-data | grep "invalid" >LAST_ACTION 2>&1; then
		echo "UTC" >/etc/timezone; emerge --config sys-libs/timezone-data \
		&& echo -e "[\e[32m*\e[0m] Resetting to UTC" \
		|| { echo -e "[\e[31m*\e[0m] Resetting to UTC"; combat_medic; }
	fi
}

_configure_locale() {
	if [[ -z $(cat "${CHDIR}/locale.gen" | sed '/^#/ d' | sed '/^\s*$/d') ]]; then
		sed -i '/en_US.UTF-8/d' /etc/locale.gen
		echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen && echo -e "[\e[32m*\e[0m] Configuring [\e[34mlocale\e[0m]" \
		|| echo -e "[\e[31m*\e[0m] Configuring [\e[34mlocale\e[0m]"
	else
		echo "$(cat "${CHDIR}/locale.gen")" > /etc/locale.gen
		sed -i '/en_US.UTF-8/d' /etc/locale.gen
		echo "en_US.UTF-8 UTF-8" >> /etc/locale.gen && echo -e "[\e[32m*\e[0m] Configuring \e[34mlocale\e[0m" \
		|| echo -e "[\e[31m*\e[0m] Configuring [\e[34mlocale\e[0m]"
	fi

	locale-gen
	export LC_ALL="en_US.UTF-8"
	SETLOC=$(eselect locale list | grep en_US | awk -F ' ' '{ print $1 }' \
	| awk -F '[' '{ print $2 }' | awk -F ']' '{ print $1 }')

	eselect locale set "${SETLOC}" && echo -e "[\e[32m*\e[0m] Setting locale to [\e[34men_US\e[0m]" \
	|| echo -e "[\e[31m*\e[0m] Failed setting locale to [\e[34men_US\e[0m]"
	unset SETLOC
}

_configure_fstab() {
	if cat "${CHDIR}/fstab" > /etc/fstab; then
		echo -e "[\e[32m*\e[0m] Creating [\e[34mfstab\e[0m] entries"
		if [[ -n $(cat "${CHDIR}/system_links" | sed '/^#/ d' | sed '/^\s*$/d') ]]; then
			BREAKVAR=0
			 while read -r i; do
				case $(echo "$i" | awk -F ' ' '{ print $1 }') in
					tmpfs)
						echo >> /etc/fstab || { BREAKVAR=1; break; }
						echo "# $(echo "$i" | awk -F ' ' '{ print $2 }')" >> /etc/fstab || { BREAKVAR=1; break; }
						echo "tmpfs $(echo "$i" | awk -F ' ' '{ print $2 }') tmpfs nodev,nosuid,size=$(echo "$i" \
						| awk -F ' ' '{ print $3 }')" >> /etc/fstab || { BREAKVAR=1; break; }
						echo >> /etc/fstab || { BREAKVAR=1; break; }
						;;
					symlink)
						if [[ $(echo "$i" | awk -F ' ' '{ print $2 }') == 'f=n' ]]; then
							echo "ln -s $(echo "$i" | awk -F ' ' '{ print $3 }') $(echo "$i" \
							| awk -F ' ' '{ print $4 }')" || { BREAKVAR=1; break; }
						elif [[ $(echo "$i" | awk -F ' ' '{ print $2 }') == 'f=y' ]]; then
							mkdir -p "$(echo "$i" | awk -F ' ' '{ print $3 }')" || { BREAKVAR=1; break; }
							echo "ln -s $(echo "$i" | awk -F ' ' '{ print $4 }') $(echo "$i" \
							| awk -F ' ' '{ print $5 }')" || { BREAKVAR=1; break; }
						fi
						;;
					bindmount)
						if [[ $(echo "$i" | awk -F ' ' '{ print $2 }') == 'f=n' ]]; then
							echo >> /etc/fstab || { BREAKVAR=1; break; }
							echo "# bind mount: $(echo "$i" | awk -F ' ' '{ print $3 }')" >> /etc/fstab || { BREAKVAR=1; break; }
							echo "$(echo "$i" | awk -F ' ' '{ print $3 }') $(echo "$i" \
							| awk -F ' ' '{ print $4 }') none rw,bind 0 0" >> /etc/fstab || { BREAKVAR=1; break; }
							echo >> /etc/fstab || { BREAKVAR=1; break; }
						elif [[ $(echo "$i" | awk -F ' ' '{ print $2 }') == 'f=y' ]]; then
							echo >> /etc/fstab || { BREAKVAR=1; break; }
							echo "# bind mount: $(echo "$i" | awk -F ' ' '{ print $4 }')" >> /etc/fstab || { BREAKVAR=1; break; }
							mkdir -p "$(echo "$i" | awk -F ' ' '{ print $3 }')" || { BREAKVAR=1; break; }
							echo "$(echo "$i" | awk -F ' ' '{ print $4 }') $(echo "$i" \
							| awk -F ' ' '{ print $5 }') none rw,bind 0 0" >> /etc/fstab || { BREAKVAR=1; break; }
							echo >> /etc/fstab || { BREAKVAR=1; break; }
						fi
						;;
					overlay)
						echo >> /etc/fstab || { BREAKVAR=1; break; }
						echo "# overlay: $(echo "$i" | awk -F ' ' '{ print $2 }')" >> /etc/fstab || { BREAKVAR=1; break; }
						OVLFSLD=$(echo "$i" | awk -F ' ' '{ print $2 }')
						OVLFSUD=$(echo "$i" | awk -F ' ' '{ print $3 }')
						OVLFSWD=$(echo "$i" | awk -F ' ' '{ print $4 }')
						echo "overlay /merged overlay noauto,lowerdir=${OVLFSLD},uperdir=${OVLFSUD},workdir=${OVLFSWD} 0 0" >> /etc/fstab \
						|| { BREAKVAR=1; break; }
						echo >> /etc/fstab || { BREAKVAR=1; break; }
						unset OVLFSLD
						unset OVLFSUD
						unset OVLFSWD
						;;
				esac
			done < <(cat "${CHDIR}/system_links" | sed '/^#/ d' | sed '/^\s*$/d') && pass || die "Failed"
			if [[ "${BREAKVAR}" == 0 ]]; then
				echo -e "[\e[32m*\e[0m] Creating \e[34msystem links\e[0m and requested \e[34mfstab\e[0m entries"
			else
				echo -e "[\e[31m*\e[0m] Creating \e[34msystem links\e[0m and requested \e[34mfstab\e[0m entries"
				echo "###################################" >> issues.info
				echo "Issue: System links $(date)" >> issues.info
				echo "Command: Could not get issued command" >> issues.info
				echo "Items --->" >> issues.info
				cat "${CHDIR}/system_links" >> issues.info
				echo "###################################" >> issues.info
				ask_for_shell "Failed configuring system links"
			fi
		fi
	else
		echo -e "[\e[31m*\e[0m] Creating [\e[34mfstab\e[0m] entries"
		echo "###################################" >> issues.info
		echo "Issue: fstab $(date)" >> issues.info
		echo "Command: cat ${CHDIR}/fstab > /etc/fstab" >> issues.info
		echo "Items --->" >> issues.info
		cat "${CHDIR}/fstab" >> issues.info
		echo "###################################" >> issues.info
		ask_for_shell "Failed creating fstab entries"
	fi
}

_copy_function() {
	[[ -n $(cat "${CHDIR}/$1" | sed '/^#/ d' | sed '/^\s*$/d') ]] \
	&& if cp "${CHDIR}/$1" /etc/conf.d/"$2"; then
		echo -e "[\e[32m*\e[0m] Configuring [\e[34m$3\e[0m]"
	else
		echo -e "[\e[31m*\e[0m] Configuring [\e[34m$3\e[0m]"
		echo "###################################" >> issues.info
		echo "Issue: $3 $(date)" >> issues.info
		echo "Command: ${CHDIR}/$1 /etc/conf.d/$2" >> issues.info
		echo "Items --->" >> issues.info
		cat /etc/conf.d/"$2" >> issues.info
		echo "###################################" >> issues.info
		ask_for_shell "Failed configuring $3"
	fi
}

configure_system_f() {
	echo "============================================================================"
	echo -e "\e[35mPart E: Configuring system\e[0m"
	echo "============================================================================"
	env-update > /dev/null 2>&1 && source /etc/profile && export PS1="( 'Part E: Configuring system' ) $PS1"
	export PATH=${PATH}:${CHDIR}

	# TIMEZONE CONFIGURATION
	_configure_timezone

	# LOCALE CONFIGURATION
	_configure_locale

	# GENERATING FSTAB
	_configure_fstab

	# CONFIGURE HOSTNAME
	_copy_function "hostname" "hostname" "hostname"

	# CONFIGURE /ETC/CONF.D/NET
	_copy_function "net" "net" "/etc/conf.d/net"

	# CONFIGURE /ETC/DEFAULT/GRUB
	_copy_function  "grub" "grub" "/etc/default/grub"

	### CUSTOM SCRIPTS ENTRIES WILL BE INCLUDED HERE

	### INSCRIPT ENTRIES WILL BE INCLUDED HERE

	# CONFIGURE SSHD
	_copy_function "sshd" "sshd" "/etc/ssh/sshd_config"

	# CONFIGURE SSH.PUB
	[[ -n $(cat "${CHDIR}/ssh.pub" | sed '/^#/ d' | sed '/^\s*$/d') ]] && mkdir -p /root/.ssh \
	&& if cat "${CHDIR}/ssh.pub" | sed '/^#/ d' | sed '/^\s*$/d' > /root/.ssh/authorized_keys; then
		echo -e "\e[33m----------------------------------------------------------------------------\e[0m"
		echo -e "[\e[32m*\e[0m] Adding ssh.pub key to [\e[34m/root/.ssh/authorized_keys\e[0m]"
		echo -e "\e[33m----------------------------------------------------------------------------\e[0m"
	else
		echo -e "\e[31m----------------------------------------------------------------------------\e[0m"
		echo -e "[\e[31m*\e[0m] Adding ssh.pub key to [\e[34m/root/.ssh/authorized_keys\e[0m]"
		echo -e "\e[31m----------------------------------------------------------------------------\e[0m"
		echo "###################################" >> issues.info
		echo "Issue: ssh.pub.key $(date)" >> issues.info
		echo "Command: cat ${CHDIR}/ssh.pub | sed '/^#/ d' | sed '/^\s*$/d' > /root/.ssh/authorized_keys" >> issues.info
		echo "Items --->" >> issues.info
		cat /etc/conf.d/"$2" >> issues.info
		echo "###################################" >> issues.info
		ask_for_shell "Failed configuring ssh.pub key"
	fi
	_monitor_chroot_flow "CONFIGURE"
}

_emerge_default() {
	echo -e "\e[34mEmerging\e[0m..."
	if emerge -q --update --deep --with-bdeps=y ${INS_PARTF}; then
		_e_report_back "Emerged"
	else
		echo "emerge -q --update --deep --with-bdeps=y ${INS_PARTF}" > LAST_FAILED_COMMAND
		echo "###################################" >> issues.info
		echo "Issue: emerge request packages $(date)" >> issues.info
		echo "Command: emerge -q --update --deep --with-bdeps=y" >> issues.info
		echo "Items --->" >> issues.info
		echo "${INS_PARTF}" >> issues.info
		echo "###################################" >> issues.info
		combat_medic
	fi
}

_emerge_custom_requested() {
	mapfile -t CMPL < <(cat "${CHDIR}/custom_pacl" | sed '/^#/ d' | sed '/^\s*$/d')

	if emerge -q --deep --update "${CMPL[@]}"; then
		_e_report_back "Emerged"
	else
		echo "emerge -q --deep --update ${CMPL[@]}" > LAST_FAILED_COMMAND
		echo "###################################" >> issues.info
		echo "Issue: emerge request packages $(date)" >> issues.info
		echo "Command: emerge -q --deep --update " >> issues.info
		echo "Items --->" >> issues.info
		echo "${CMPL[@]}" >> issues.info
		echo "###################################" >> issues.info
		combat_medic
	fi
}

# EMERGE REQUESTED PACKAGES FUNCTION
emerge_requested() {
	echo
	echo "----------------------------------------------------------------------------"
	echo -e "\e[35mPart F: Installing required packages\e[0m"
	echo "----------------------------------------------------------------------------"
	env-update > /dev/null 2>&1 && source /etc/profile && export PS1="( 'Part F: Installing required packages' ) $PS1"
	export PATH=${PATH}:${CHDIR}

	# EMERGE CHINIT.CONF DEFINED PACKAGES
	_emerge_default

	# EMERGE REQUESTED PACKAGES
	_emerge_custom_requested

	# DEPCLEAN
	_emerge_depclear

	_monitor_chroot_flow "INSTALL"
}

# KERNEL BUILDER FUNCTION
_kernel_build() {
	echo
	echo "----------------------------------------------------------------------------"
	echo -e "\e[35mPart H: Building the Kernel\e[0m"
	echo "----------------------------------------------------------------------------"
	env-update > /dev/null 2>&1 && source /etc/profile && export PS1="( 'Part H: Building the Kernel' ) $PS1"
	export PATH=${PATH}:${CHDIR}
	MKPS=$(grep MAKEOPTS "/etc/portage/make.conf" | sed '/^#/ d' | sed  -e "s_\"__g" | awk -F '-j' '{print $2}' | sed '/^\s*$/d')
	if echo "$@" | grep -q 'cforce' || ! echo "$@" | grep -q '_flag_auto'; then
		echo -e "The \e[34mBuilding kernel\e[0m"
		if genkernel --install kernel --kernel-config="${CHDIR}/kernel-conf" \
			--makeopts="-j${MKPS}" --btrfs --postclear --e2fsprogs; then
			echo -e "The \e[34mkernel\e[0m built was \e[32msuccessful\e[0m"
			_monitor_chroot_flow "KERNEL"
			return 0
		else
			ask_for_shell "Failed Building the kernel"
		fi
	fi
	while true; do
		echo -e "Do you wish to configure the \e[34mkernel's\e[0m config file?"
		echo -e "Please keep in mind that main function of the controller are related with \e[34mkernel builtins\e[0m"
		echo -e "Select an option Y/N: "
		read -rp "Input :: <= " SELCT
			case ${SELCT} in
				[Yy][eE][sS]|[yY])
					if genkernel --install kernel --menuconfig --kernel-config="${CHDIR}/kernel-conf" \
						--makeopts="-j${MKPS}" --btrfs --postclear --e2fsprogs; then
						echo -e "The \e[34mkernel\e[0m built was \e[32msuccessful\e[0m"
						_monitor_chroot_flow "KERNEL"
					else
						echo "###################################" >> issues.info
						echo "Issue: Kernel $(date)" >> issues.info
						echo "Command: genkernel --install kernel --menuconfig --kernel-config=${CHDIR}/kernel-conf \
						--makeopts=-j${MKPS} --btrfs --postclear --e2fsprogs" >> issues.info
						echo "###################################" >> issues.info
						ask_for_shell "Failed building the kernel"
					fi
					break;;
				[nN][oO]|[nN])
					if genkernel --install kernel --kernel-config="${CHDIR}/kernel-conf" \
						--makeopts="-j${MKPS}" --btrfs --postclear --e2fsprogs; then
						echo -e "The \e[34mkernel\e[0m built was \e[32msuccessful\e[0m"
						_monitor_chroot_flow "KERNEL"
					else
						echo "###################################" >> issues.info
						echo "Issue: Kernel $(date)" >> issues.info
						echo "Command: genkernel --install kernel --kernel-config=${CHDIR}/kernel-conf \
						--makeopts=-j${MKPS} --btrfs --postclear --e2fsprogs" >> issues.info
						echo "###################################" >> issues.info
						ask_for_shell "Failed building the kernel"
					fi
					break;;
				*) echo "Wrong option";;
			esac
	done
}

# INITRAMFS BUILDER FUNCTION
_initramfs_builder() {
	echo
	echo "----------------------------------------------------------------------------"
	echo -e "\e[35mPart I: Building the Initramfs\e[0m"
	echo "----------------------------------------------------------------------------"
	env-update > /dev/null 2>&1 && source /etc/profile && export PS1="( 'Part H: Building the Initramfs' ) $PS1"
	export PATH=${PATH}:${CHDIR}
	if dracut -f "initramfs-controller-${ARCH}-${GSEVER}"; then
		echo -e "The \e[34minitramfs\e[0m built was successful"
		_monitor_chroot_flow "INITRAMFS"
	else
		echo "###################################" >> issues.info
		echo "Issue: Initramfs $(date)" >> issues.info
		echo "Command: dracut -f initramfs-controller-${ARCH}-${GSEVER}" >> issues.info
		echo "###################################" >> issues.info
		ask_for_shell "Failed building initramfs"
	fi
}

_des_c() {
	eclean-dist -d
	eclean-pkg -d

	if [[ -n "${_flag_minimal}" ]]; then
		rm -f /var/lib/portage/world
		touch /var/lib/portage/world
	else
		emerge --deselect sys-kernel/gentoo-sources
		emerge --deselect sys-kernel/genkernel
		emerge --deselect sys-boot/grub:2
		emerge --deselect sys-kernel/dracut
		emerge --deselect dev-util/ccache
		emerge --deselect sys-devel/distcc
		emerge --deselect app-portage/eix
		emerge --deselect app-portage/gentoolkit
	fi

	_emerge_depclear
}

# RUNLEVEL UPDATE FUNCTION
_runlevel_configuration() {
	echo
	echo "----------------------------------------------------------------------------"
	echo -e "\e[35mPart G: Updating Runlevel Entries\e[0m"
	echo "----------------------------------------------------------------------------"
	env-update > /dev/null 2>&1 && source /etc/profile && export PS1="( 'Part G: Updating Runlevel Entries' ) $PS1"
	export PATH=${PATH}:${CHDIR}
	{ while read -r i; do
		rc-update "$(echo "$i" | awk -F ' ' '{ print $2 }')" "$(echo $i | awk -F ' ' '{ print $1 }')" \
		"$(echo "$i" | awk -F ' ' '{ print $3 }')"
		sleep 0.5
	done < <(cat "${CHDIR}/runlevels" | sed '/^#/ d' | sed '/^\s*$/d'); } \
	&& { echo -e "[\e[32m*\e[0m] Updated successfully" && _monitor_chroot_flow "RUNLEVEL"; } \
	|| { echo "###################################" >> issues.info
	echo "Issue: Runlevels $(date)" >> issue.info
	echo "Command: rc-update \$(echo \$i | awk -F ' ' '{ print \$2 }') \$(echo \$i | awk -F ' ' '{ print \$1 }') \
		\$(echo \$i | awk -F ' ' '{ print \$3 }')" >> issues.info
	echo "Items --->" >> issues.info
	cat "${CHDIR}/runlevels" >> issues.info
	echo "###################################" >> issues.info
	ask_for_shell "Failed updating runlevels"; }
}

# SIMPLE EMERGE RESUME
_emerge_resume() {
	echo -e "\e[33mResuming\e[0m"
	emerge -qr \
	&& return 0 || return 1
}

# RECALL LAST FAILED ACTION
recal_action() {
	echo -e "\e[33mRecalling last process...\e[0m"
	eval "$(cat LAST_FAILED_COMMAND)"
}

# CALL FOR EVALUATION LAST FAILD ACTION
_recal_action() {
	if recal_action; then
		_e_report_back "Recalling action has been completed" \
		&& delete_fail \
		&& return 0
	else
		return 1
	fi
}

# CALL FOR EVALUATION EMERGE RESUME
_resume(){
	if _emerge_resume; then
		_e_report_back "Resume action was successful" \
		&& delete_fail \
		&& return 0
	else
		echo "emerge -qr" > LAST_FAILED_COMMAND
		return 1
	fi
}

# BRINGS SHELL
ask_for_shell() {
	while true; do
		_print_info 2
		echo "$@"
		echo "Do you wish to call shell function and fix the issues manually?"
		echo "An issues.info file will be created in the current directory"
		echo "The above file will contain the instruction that gse attempted to execute"
		echo "Answer Y/N "
		read -rp "Input :: <= " YN
		case "$YN" in
			[yY])
				chroot_master_loop "SHELL"
				break;;
			[nN])
				break;;
		esac
	done		
}

# EMERGE MASTER LOOP FUNCTION. IT OFFERS A RESUME, SHELL & EXIT OPTION.
emerge_master_loop() {
	if [[ -n "$(grep "FAILED" <"${CHDIR}/emergeresume")" ]]; then
		while true; do
			echo -e "\e[33mA previous emerge action could not be completed or stopped manually\e[0m"
			echo -e "Do you wish to \e[35mresume\e[0m that action?"
			echo -e "Answer: \e[35mRESUME\e[0m/\e[33mSHELL\e[0m/\e[34mCONTINUE\e[0m/\e[31mEXIT\e[0m"
			read -rp "Input :: <= " YN
			case "${YN}" in
				RESUME)
					if [[ -n "$(grep resume "/var/cache/edb/mtimedb")" ]]; then
						if _resume; then
							LOOPVAR="PASS"
						else
							LOOPVAR="LOOPEMERGE"
						fi
					else
						if _recal_action; then
							LOOPVAR="PASS"
						else
							LOOPVAR="LOOPEMERGE"
						fi	
					fi
					break;;
				SHELL)
					while true; do
						_subshell
						echo "If you fixed the issue, say RESUME to attempt an emerge resume"
						echo "You can answer SHELL to open shell again, or call die with EXIT"
						echo -e "Answer: \e[35mRESUME\e[0m/\e[33mSHELL\e[0m/\e[31mEXIT\e[0m"
						read -rp "Input :: <= " AANS
						case "${AANS}" in
							RESUME)
								break;;
							SHELL)
								;;
							EXIT)
								die "Exit Requested";;
						esac
					done
					if [[ -n "$(grep resume "/var/cache/edb/mtimedb")" ]]; then
						if _resume; then
							LOOPVAR="PASS"
						else
							LOOPVAR="LOOPEMERGE"
						fi
					else
						if _recal_action; then
							LOOPVAR="PASS"
						else
							LOOPVAR="LOOPEMERGE"
						fi	
					fi
					break;;
				CONTINUE)
						LOOPVAR="PASS"
						break;;
				EXIT)
					die "Exit Requested";;
			esac
		done
	fi
	unset YN
	unset AANS
	unset ANS
}

# CALL CHROOT MASTER LOOP FUNCTION WITH EMERGE MASTER LOOP FUNCTION AS TARGET
combat_medic() {
	_print_info 2
	write_fail
	chroot_master_loop "LOOPEMERGE"
}

# SUBSHELL LOOP FUNCTION, IT OFFERS
subshell_loop() {
	while true; do
		_subshell
		echo "If you fixed the issue, say CONTINUE proceed"
		echo "You can answer SHELL to open shell again, or call die with EXIT"
		echo "Answer? CONTINUE/SHELL/EXIT: "
		read -rp "Input :: <= " AANS
		case "${AANS}" in
			CONTINUE)
				LOOPVAR="EXITSHELL"
				break;;
			SHELL)
				LOOPVAR="SHELL"
				;;
			EXIT)
				die "Exit Requested";;
		esac
	done
}

# CHROOT MASTER LOOP FUNCTION. CONTROLLS SUBSHELL LOOP AND EMERGE MASTER LOOP
chroot_master_loop() {
	[[ -z $(echo "$@") ]] && _print_info 3
	LOOPVAR="$1"
	while true; do
		case "${LOOPVAR}" in
			PASS)
				delete_fail
				break;;
			LOOPEMERGE)
				emerge_master_loop;;
			SHELL)
				subshell_loop;;
			EXITSHELL)
				break;;
				
		esac
	done
}

# REPEAT PART X.
_repeat_part() {
	while true; do
		echo -e "\e[33mPart $1: Has been completed\e[0m"
		echo -e "\e[33mRun again? Y/N: \e[0m"
		read -rp "Input :: <= " YN
		case "${YN}" in
			[yN])
				return 0
				break;;
			[nN])
				return 1
				break;;
		esac
	done
}

_do_rs() {
	while read -r s;do
		source "${CHDIR}/cscripts/$s"
	done < <(grep "$1" "${CHDIR}/cscripts/doscripts" | sed '/^#/ d' | sed '/^\s*$/d' | awk -F ':' '{ print $2 }')
}

_do_hs() {
	if grep -q "$1" "${CHDIR}/cscripts/doscripts" && [[ -n "${_flag_sdir}" ]]; then
		_e_report_back "Sourcing scripts $2 $3"
		_do_rs "$1"
	fi
}

_call_hook_points() {
	if ! echo "$@" | grep -q '_flag_auto'; then
		if echo "${_lawful_args[@]}" | grep -q "$2"; then
			_e_report_back "Lawful entry detected on $2, proceeding..."
		else	
			if [[ "$(grep "$1" "${CHDIR}/chsinprog" | awk -F '=' '{ print $2 }')" == 0 || -n "${_flag_force_new}" ]] || echo "${_enforce_args[@]}" | grep -q "$2"; then
				if 	[[ -n "${_flag_force_new}" ]] || echo "${_enforce_args[@]}" | grep -q "$2"; then
					_e_report_back "Force entry detected on $3, forcing..."
					eval "$4" "${@:5}" "cforce" || die
				else
					eval "$4" "${@:5}" || die
				fi
			else
				if _repeat_part "$3"; then
					eval "$4" "${@:5}"
				else
					_e_report_back "Proceeding"
				fi
			fi
		fi
	else
		eval "$4" "${@:5}"
	fi
}