#!/bin/bash

source lib/function/config
# builder and profile versions
BUILDER="6.9"
PROFILE="1.2"

if [[ `command -v dialog` ]]; then :; else sudo apt install dialog; fi
ARCH=`uname -m`
MYNAME=`echo $USER | sed -e "s/\b\(.\)/\u\1/g"`

# userdata dot txt
NAME="$MYNAME"
USERNAME="$USER"
PASSWORD="board"
COMPILER="gcc"
CCACHE="1"
NETWORKMANAGER="1"
RTW88="0"
USERACCT="0"
USER_PKGS="htop libsensors-dev rsyslog"
LOCALE="en_US.UTF-8"
LOCALE_LC_ALL="C.UTF-8"
TIMEZONE="America/New_York"
NAMESERVER1="8.8.8.8"
NAMESERVER2="8.8.4.4"
# profile dot txt
if [[ -f "profile.txt" ]]; then
	. profile.txt
	if [[ -n "$USER_PROFILE" ]]; then :; else rm -f profile.txt; fi
	if [[ "$PROFILE" == "$USER_PROFILE" ]]; then :; else profile_txt; fi
else
	profile_txt
fi
ROOTPASSWD="0"
VERSION="6.6.y"
BUILD_VERSION="1"
MENUCONFIG="0"
DISTRO="debian"
DISTRO_VERSION="bookworm"
CUSTOM_DEFCONFIG="0"
MYCONFIG=""
VERBOSE="0"
DEVROOTFS="1"
COMPRESS_IMG="0"
USCRIPTS="0"

# open fd
exec 3>&1

# store data to $VALUES variable
VALUES=$(dialog --ok-label "Submit" --no-cancel \
	--backtitle "RASPBERRY PI IMAGE BUILDER: CONFIG MENU" \
	--title "" --form "" 0 36 0 \
	"Name:"			1 1	"$NAME" 		1 7 23 0 \
	"Username:"		2 1	"$USERNAME" 		2 11 19 0 \
	"Password:"		3 1	"$PASSWORD"  		3 11 19 0 \
	"Enable root:"		4 1	"$ROOTPASSWD"  		4 14 3 0 \
	""			5 1	""	 		5 14 0 0 \
	"Linux kernel"		6 1	""	 		6 14 0 0 \
        "Branch:"   		7 1     "$VERSION"        	7 9 8 0 \
        "Build:"   		8 1     "$BUILD_VERSION"        8 8 5 0 \
	"Menuconfig:"		9 1	"$MENUCONFIG"  		9 13 3 0 \
	"Compiler:"		10 1	"$COMPILER"	 	10 11 10 0 \
	"Ccache:"		11 1	"$CCACHE"	 	11 9 3 0 \
	""			12 1	""	 		12 14 0 0 \
	"Distribution"		13 1	""	 		13 14 0 0 \
	"Distro:"		14 1	"$DISTRO"	  	14 9 21 0 \
	"Release:"		15 1	"$DISTRO_VERSION"  	15 10 20 0 \
	"Network Manager:" 	16 1	"$NETWORKMANAGER" 	16 18 3 0 \
	""			17 1	""	 		17 14 0 0 \
	"Customize"		18 1	""	 		18 14 0 0 \
	"Defconfig:"		19 1	"$CUSTOM_DEFCONFIG" 	19 12 3 0 \
	"Name:"			20 1	"$MYCONFIG"	  	20 7 23 0 \
	""			21 1	""	 		21 14 0 0 \
	"User options"		22 1	""	 		22 14 0 0 \
	"Verbosity:"		23 1	"$VERBOSE"		23 12 3 0 \
	"Compress img:"		24 1	"$COMPRESS_IMG" 	24 15 3 0 \
	"User scripts:"		25 1	"$USCRIPTS"		25 15 3 0 \
	"User service:" 	26 1	"$USERACCT"		26 15 3 0 \
2>&1 1>&3)

# close fd
exec 3>&-

# convert into variables
echo "$VALUES" > tmp1
echo NAME='"' > tmp0
echo USERNAME='"' >> tmp0
echo PASSWORD='"' >> tmp0
echo ROOTPASSWD= >> tmp0
echo VERSION='"' >> tmp0
echo BUILD_VERSION='"' >> tmp0
echo MENUCONFIG= >> tmp0
echo COMPILER='"' >> tmp0
echo CCACHE= >> tmp0
echo DISTRO='"' >> tmp0
echo DISTRO_VERSION='"' >> tmp0
echo NETWORKMANAGER= >> tmp0
echo CUSTOM_DEFCONFIG= >> tmp0
echo MYCONFIG='"' >> tmp0
echo VERBOSE= >> tmp0
echo COMPRESS_IMG= >> tmp0
echo USCRIPTS= >> tmp0
echo USERACCT= >> tmp0
echo 'DEVROOTFS="1"' >> tmp0
echo USER_PKGS='"'${USER_PKGS}'"' >> tmp0
echo 'ENABLE_COMMIT="0"' >> tmp0
echo 'COMMIT=""' >> tmp0
echo CORES=\`nproc\` >> tmp0
echo 'CFLAGS=""' >> tmp0
echo 'KBUSER="playboy"' >> tmp0
echo 'KBHOST="penguin"' >> tmp0
echo LOCALE='"'${LOCALE}'"' >> tmp0
echo LOCALE_LC_ALL='"'${LOCALE_LC_ALL}'"' >> tmp0
echo TIMEZONE='"'${TIMEZONE}'"' >> tmp0
echo NAMESERVER1='"'${NAMESERVER1}'"' >> tmp0
echo NAMESERVER2='"'${NAMESERVER2}'"' >> tmp0
echo "RTW88=${RTW88}" >> tmp0
echo BUILDER=${BUILDER} >> tmp0
echo '"' > tmp2
echo '"' >> tmp2
echo '"' >> tmp2
echo >> tmp2
echo '"' >> tmp2
echo '"' >> tmp2
echo >> tmp2
echo '"' >> tmp2
echo >> tmp2
echo '"' >> tmp2
echo '"' >> tmp2
echo >> tmp2
echo >> tmp2
echo _defconfig'"' >> tmp2
paste -d '\0' tmp0 tmp1 tmp2  > userdata.txt
rm -f tmp0 tmp1 tmp2

# categories
sed -i '5i### LINUX KERNEL' userdata.txt
sed -i '11i### DISTRIBUTION' userdata.txt
sed -i '15i### CUSTOM' userdata.txt
sed -i '24i### CUSTOM LINUX KERNEL' userdata.txt
sed -i '27i### COMPILER TUNING' userdata.txt
sed -i '30i### WHOAMI AND HOST' userdata.txt
sed -i '33i### LOCALES TIMEZONE AND NAMESERVERS' userdata.txt
sed -i '39i### EXTRA WIRELESS' userdata.txt
sed -i '41i### DO NOT EDIT BELOW THIS LINE' userdata.txt

# custom dot txt
if [[ -f "custom.txt" ]]; then
	. custom.txt
	if [[ "$CUSTOM" == "$BUILDER" ]]; then :; else custom_txt; fi
else
	custom_txt
fi

# kernel preferences
if [[ -f "kernel.txt" ]]; then
	. kernel.txt
	if [[ "$KERNEL_PREFERENCES" == "$BUILDER" ]]; then :; else kernel_txt; fi
else
	kernel_txt
fi

# rootfs / image mirrors
if [[ -f "mirror.txt" ]]; then
	. mirror.txt
	if [[ "$MIRROR_LIST" == "$BUILDER" ]]; then :; else mirror_txt; fi
else
	mirror_txt
fi

# clear screen
clear -x

# userdata-txt
if [[ -f "userdata.txt" ]]; then
	sed -i 's/=0/="0"/g' userdata.txt
	sed -i 's/=1/="1"/g' userdata.txt
	sed -i '4G;10G;14G;23G;26G;29G;32G;38G;40G' userdata.txt
	echo -e "userdata file created."
	exit 0
fi

exit 0
