#!/bin/bash
clear



###
# Introduction -- This should look familliar to most users ;)
#                 Why change something that works?  :D
###

export test="empty"

while [ $test != "avocado" ]; do
   clear
   echo -e
   echo -e
   echo -e
   echo -e "     \033[01;34mWelcome to the \033[01;31mGentoox\033[01;34m firstboot script."
   echo -e "     \033[00m"
   echo -e "     This installer is designed to work with \033[01;34mSSH\033[00m, \033[01;34mUSB keyboards\033[00m and"
   echo -e "     \033[01;34mXpads\033[00m.  The installer will ask you a range of questions, you"
   echo -e "     can either type the answers or highlight them with the \033[01;34mXpad\033[00m and then"
   echo -e "     press the '\033[01;31mB\033[00m' button to paste your response."
   echo -e "     \033[01;31mPractice the \033[01;34mXpad\033[00m highlighting technique now\033[00m, if you have a \033[01;34mkeyboard"
   echo -e "     \033[00mand you wish to use it,  type 'avocado' and press enter to use your "
   echo -e "     keyboard throughout the installation."
   echo -e
   echo -e "     If you are using your \033[01;34mXpad\033[00m, then try to highlight the following word:"
   echo -e
   echo -e "     avocado"
   echo -e
   echo -e "     To do this, move your cursor to the first letter of the word, then"
   echo -e "     hold down the '\033[01;32mA\033[00m' button and drag right until the the whole line"
   echo -e "     from the beginning of the word is highlighted - \033[01;33mNOT JUST THE WORD, BUT"
   echo -e "     THE WHOLE LINE AFTER IT\033[00m it should look similar to this: \033[47m\033[30mavocado            \033[00m"
   echo -e "     Once you have it highlighted press the '\033[01;31mB\033[00m' button to paste it.  "
   echo -e "     If you get it wrong, this script will loop until you get it right."
   echo -e "     If it goes dreadfully wrong, the xbox \033[01;32mwill reboot.\033[00m"
   echo -e "     \033[00m"
   echo -n "     Enter word here: "
   read test
done
clear


###
# If successful, then execute the following
###
if [ $test == "avocado" ]; then
   clear
        echo ""
        echo ""
        echo ""
   echo "     Well done, you've got it."
   echo ""
        echo -e "\033[01;31m     NOTE: Read all the information on the following screens carefully"
        echo "           and write the details down - a large number of people ask"
   echo "           questions like 'Whats the default FTP login?' when its stated"
   echo -e "           in this setup wizard.\033[00m"
   sleep 10
   ###
   # If it goes very wrong, tell the system to exit and reboot
   ###
else
   clear
   reboot
   sleep 1000
fi

###
# Clear out window manager selection
###
rm -rf /etc/conf.d/magic-database.tmp
grep -vi onxfce1 /etc/conf.d/magic-database > /etc/conf.d/magic-database.tmp
grep -vi onkde1 /etc/conf.d/magic-database.tmp > /etc/conf.d/magic-database
rm -rf /etc/conf.d/magic-database.tmp

grep -vi startxfce4 /etc/skel/.xinitrc >> /etc/skel/.xinitrc.tmp
mv /etc/skel/.xinitrc.tmp /etc/skel/.xinitrc
chown root:root /etc/skel/.xinitrc
chmod 755 /etc/skel/.xinitrc

grep -vi startxfce4 /root/.xinitrc >> /root/.xinitrc.tmp
mv /root/.xinitrc.tmp /root/.xinitrc
chown root:root /root/.xinitrc
chmod 755 /root/.xinitrc

grep -vi startxfce4 /home/gentoox/.xinitrc >> /home/gentoox/.xinitrc.tmp
mv /home/gentoox/.xinitrc.tmp /home/gentoox/.xinitrc
chown gentoox:users /home/gentoox/.xinitrc
chmod 755 /home/gentoox/.xinitrc

grep -vi startkde /etc/skel/.xinitrc >> /etc/skel/.xinitrc.tmp
mv /etc/skel/.xinitrc.tmp /etc/skel/.xinitrc
chown root:root /etc/skel/.xinitrc
chmod 755 /etc/skel/.xinitrc

grep -vi startkde /root/.xinitrc >> /root/.xinitrc.tmp
mv /root/.xinitrc.tmp /root/.xinitrc
chown root:root /root/.xinitrc
chmod 755 /root/.xinitrc

grep -vi startkde /home/gentoox/.xinitrc >> /home/gentoox/.xinitrc.tmp
mv /home/gentoox/.xinitrc.tmp /home/gentoox/.xinitrc
chown gentoox:users /home/gentoox/.xinitrc
chmod 755 /home/gentoox/.xinitrc
clear

###
# Setup the Window Manager
###

clear
echo ""
echo ""
echo ""
echo "     Configuring XFCE..."
sleep 5

echo "onxfce1" >> /etc/conf.d/magic-database
chown root:root /etc/conf.d/magic-database
chmod 600 /etc/conf.d/magic-database

echo "startxfce4" >> /etc/skel/.xinitrc
echo "startxfce4" >> /root/.xinitrc
echo "startxfce4" >> /home/gentoox/.xinitrc
clear

###
# Setup XDM
###

echo ""
echo ""
echo ""
echo "     Would you like the GUI to start automatically?"
echo "     The GUI is a graphical user interface for Gentoox."
echo ""
echo "     You can enable this later as root by running: rc-update add xdm default"
echo "     or disable it as root by running            : rc-update del xdm default"
echo ""
echo "     yes"
echo "     no"
echo ""
echo -n "     Answer: "
read wantxdm
if [ $wantxdm == "yes" ]; then
   clear
        echo ""
        echo ""
        echo ""
   echo "     Enabling GUI..."
   sleep 5
   rc-update add xdm default 1> /dev/null 2> /dev/null
   rc-update add x11vnc default 1> /dev/null 2> /dev/null
else
   clear
        echo ""
        echo ""
        echo ""
   echo "     Disabling GUI..."
   sleep 5
   rc-update del xdm default 1> /dev/null 2> /dev/null
   rc-update del x11vnc default 1> /dev/null 2> /dev/null
fi
clear


###
# Setup webmin
###

echo ""
echo ""
echo ""
echo "     Would you like Webmin to start automatically?"
echo "     Webmin lets you administrate your computer remotely via a web browser"
echo "     Simply connect to http://xbox_ip:10000 and login with your root details"
echo "     to connect to Webmin."
echo ""
echo "     NOTE: You should always change the default password for security reasons."
echo "     You can enable this later as root by running: rc-update add webmin default"
echo "     or disable it as root by running            : rc-update del webmin default"
echo ""
echo "     yes"
echo "     no"
echo ""
echo -n "     Answer: "
read wantwebmin
if [ $wantwebmin == "yes" ]; then
   clear
        echo ""
        echo ""
        echo ""
   echo "     Enabling Webmin..."
   sleep 5
   rc-update add webmin default 1> /dev/null 2> /dev/null
else
   clear
        echo ""
        echo ""
        echo ""
   echo "     Disabling Webmin..."
   sleep 5
   rc-update del webmin default 1> /dev/null 2> /dev/null
fi
clear



###
# Setup Samba
###

echo ""
echo ""
echo ""
echo "     Would you like Samba to start automatically?"
echo "     Samba lets you share your Xbox files with a windows network."
echo "     To connect to Samba from a Windows PC, run \\xbox_ip\ when asked"
echo "     for a password, enter your root details."
echo ""
echo "     NOTE: You should always change the default password for security reasons."
echo "     You can enable this later as root by running: rc-update add samba default"
echo "     or disable it as root by running            : rc-update del samba default"
echo ""
echo "     yes"
echo "     no"
echo ""
echo -n "     Answer: "
read wantsamba
if [ $wantsamba == "yes" ]; then
   clear
        echo ""
        echo ""
        echo ""
   echo "     Enabling Samba..."
   sleep 5
   rc-update add samba default 1> /dev/null 2> /dev/null
else
   clear
        echo ""
        echo ""
        echo ""
   echo "     Disabling Samba..."
   sleep 5
   rc-update del samba default 1> /dev/null 2> /dev/null
fi
clear



###
# Setup ProFTPd
###

echo ""
echo ""
echo ""
echo "     Would you like ProFTPd to start automatically?"
echo "     ProFTPd is an FTP server."
echo "     To connect, enter your Xbox's IP in the ftp client, then connect with:"
echo ""
echo "     User: gentoox"
echo "     Pass: xbox"
echo ""
echo "     This will grant you access to the '/home/gentoox directory'"
echo "     You cannot access /mnt/fatx/* due to security reasons but you can add your"
echo "     own users to enable this at a later stage."
echo ""
echo "     NOTE: You should change gentoox's password when you first login by running"
echo "     'passwd' then following the on screen instructions."
echo "     You can enable this later as root by running: rc-update add proftpd default"
echo "     or disable it as root by running            : rc-update del proftpd default"
echo ""
echo "     yes"
echo "     no"
echo ""
echo -n "     Answer: "
read wantproftpd
if [ $wantproftpd == "yes" ]; then
   clear
        echo ""
        echo ""
        echo ""
   echo "     Enabling ProFTPd..."
   sleep 5
   rc-update add proftpd default 1> /dev/null 2> /dev/null
else
   clear
        echo ""
        echo ""
        echo ""
   echo "     Disabling ProFTPd..."
   sleep 5
   rc-update del proftpd default 1> /dev/null 2> /dev/null
fi
clear

###
# Delete self to stop running on subsequent reboots!
###

grep -v "firstboot" /etc/conf.d/local.start > /etc/conf.d/local.start.bah
mv /etc/conf.d/local.start.bah /etc/conf.d/local.start
chown root:root /etc/conf.d/local.start
chmod 644 /etc/conf.d/local.start
rm -rf /bin/firstboot
clear

###
# Call XBVSET
###
sh /bin/xbvset