#!/bin/bash

source /sources/colors.sh
source /sources/spinner.sh

echo -e "${GRN}Successfully entered ${RED}CHROOT${RST}!\n"

echo -e "${GRN}Creating directories...${RST}"
if [ -f /sources/createdirs ]; then
	echo -e "${GRN}  Directories already created.${RST}"
else
	mkdir -p /boot
	mkdir -p /home
	mkdir -p /mnt
	mkdir -p /opt
	mkdir -p /srv

	mkdir -p /etc/opt
	mkdir -p /etc/opt/sysconfig
	mkdir -p /lib/firmware
	mkdir -p /media/floppy
	mkdir -p /media/cdrom
	mkdir -p /usr/bin
	mkdir -p /usr/include
	mkdir -p /usr/lib
	mkdir -p /usr/sbin
	mkdir -p /usr/src
	mkdir -p /usr/local/bin
	mkdir -p /usr/local/include
	mkdir -p /usr/local/lib
	mkdir -p /usr/local/sbin
	mkdir -p /usr/local/src
	mkdir -p /usr/share/color
	mkdir -p /usr/share/dict
	mkdir -p /usr/share/doc
	mkdir -p /usr/share/info
	mkdir -p /usr/share/locale
	mkdir -p /usr/share/man
	mkdir -p /usr/share/man/man1
	mkdir -p /usr/share/man/man2
	mkdir -p /usr/share/man/man3
	mkdir -p /usr/share/man/man4
	mkdir -p /usr/share/man/man5
	mkdir -p /usr/share/man/man6
	mkdir -p /usr/share/man/man7
	mkdir -p /usr/share/man/man8
	mkdir -p /usr/share/misc
	mkdir -p /usr/share/terminfo
	mkdir -p /usr/share/zoneinfo
	mkdir -p /usr/local/share/color
	mkdir -p /usr/local/share/dict
	mkdir -p /usr/local/share/doc
	mkdir -p /usr/local/share/info
	mkdir -p /usr/local/share/locale
	mkdir -p /usr/local/share/man
	mkdir -p /usr/local/share/man/man1
	mkdir -p /usr/local/share/man/man2
	mkdir -p /usr/local/share/man/man3
	mkdir -p /usr/local/share/man/man4
	mkdir -p /usr/local/share/man/man5
	mkdir -p /usr/local/share/man/man6
	mkdir -p /usr/local/share/man/man7
	mkdir -p /usr/local/share/man/man8
	mkdir -p /usr/local/share/misc
	mkdir -p /usr/local/share/terminfo
	mkdir -p /usr/local/share/zoneinfo
	mkdir -p /var/cache
	mkdir -p /var/local
	mkdir -p /var/log
	mkdir -p /var/mail
	mkdir -p /var/opt
	mkdir -p /var/spool
	mkdir -p /var/lib/color
	mkdir -p /var/lib/misc
	mkdir -p /var/lib/locate

	ln -sf /run /var/run
	ln -sf /run/lock /var/lock

	install -d -m 0750 /root
	install -d -m 1777 /tmp /var/tmp

	echo -e "  ${GRN}Directories created${RST}"
	touch /sources/createdirs
fi

echo -e "${GRN}Creating essential files and symbolic links... ${RST}"
if [ -f "/sources/symfiles" ]; then
	echo -e "  ${GRN}Files and symbolic links already created.${RST}"
else
	ln -sf /proc/self/mounts /etc/mtab
	echo "127.0.0.1 localhost $(hostname)" > /etc/hosts

	cat > /etc/passwd << "EOF"
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/dev/null:/bin/false
daemon:x:6:6:Daemon User:/dev/null:/bin/false
messagebus:x:18:18:D-Bus Message Daemon User:/run/dbus:/bin/false
uuidd:x:80:80:UUID Generation Daemon User:/dev/null:/bin/false
nobody:x:99:99:Unprivileged User:/dev/null:/bin/false
EOF

	cat > /etc/group << "EOF"
root:x:0:
bin:x:1:daemon
sys:x:2:
kmem:x:3:
tape:x:4:
tty:x:5:
daemon:x:6:
floppy:x:7:
disk:x:8:
lp:x:9:
dialout:x:10:
audio:x:11:
video:x:12:
utmp:x:13:
usb:x:14:
cdrom:x:15:
adm:x:16:
messagebus:x:18:
input:x:24:
mail:x:34:
kvm:x:61:
uuidd:x:80:
wheel:x:97:
nogroup:x:99:
users:x:999:
EOF

	echo "tester:x:$(ls -n $(tty) | cut -d" " -f3):101::/home/tester:/bin/bash" >> /etc/passwd
	echo "tester:x:101:" >> /etc/group
	install -o tester -d /home/tester

	touch /var/log/btmp
	touch /var/log/lastlog
	touch /var/log/faillog
	touch /var/log/tmp
	chgrp utmp /var/log/lastlog
	chmod 664  /var/log/lastlog
	chmod 600  /var/log/btmp

	echo -e "  ${GRN}Files and symlinks created.${RST}"
	touch /sources/symfiles
fi

cd /sources

# Chapter 7
for package in libstdc++ gettext bison perl python texinfo util-linux; do
	source packageinstall.sh 7 $package
    retval=$?
    if [ "$retval" -ne 0 ]; then
        exit 1
    fi
done