# Configure Host (chmod +x configure.sh)
# curl -O -J -L https://drive.google.com/uc?id=
#!/bin/bash
# (c) by Andreas Schmid-Klocke
# Version 0.98
#
color_green='\033[0;32m'
color_no='\033[0m'
clear
echo
echo -e "${color_green}   EXPERT- Let's Install & Configure${color_no}"
echo "   Version: 0.98"
echo "   Date: Nov. 2017"
echo
echo -e "${color_green}This script install samba, webmin and create a ldap and file service.${color_no}"
echo -e "${color_green}You can manage this services with Windows Remote Server Admin Tools (RSAT) or webmin about https://[hostname]/10000.${color_no}"
echo
echo -e "${color_green}Important, this script needs root 'sudo' rights. More details, see https://wiki.ubuntuusers.de/Howto/Samba4_als_Domaincontroller/${color_no}"
echo
echo -e "${color_green}Please have a look on my Youtube cloning tutorial: https://www.youtube.com/watch?v=mO${color_no}"
echo
echo

partone="/mnt/.part.one"
parttwo="/mnt/.part.two"

read -p "   Do you want start this script? (example, y or n): " var_startscript

start_production() {
  if [ -f $parttwo ]; then			#This run part 3
    clear
    echo "deb http://download.webmin.com/download/repository sarge contrib" >> /etc/apt/sources.list
    echo -e "${color_green} Webmin is a modern, web control panel for any Linux machine. Manage your host about https://hostname/10000${color_no}"
    sleep 10
    wget http://www.webmin.com/jcameron-key.asc   #Sometimes they need twice or more trys
    pause 3
    wget http://www.webmin.com/jcameron-key.asc
    pause 3
    wget http://www.webmin.com/jcameron-key.asc
    sudo apt-key add jcameron-key.asc
    sleep 2
    sudo apt-get update 
    sleep 2
    sudo apt-get install webmin  -y
    sudo netstat -tlpn
    sed -i '2i* - nofile 16384' /etc/security/limits.conf
    echo -e "${color_green} Done... ${color_no}" 
  elif [ -f $partone ]; then 			#This run part 2
    clear
    echo -e "${color_green}Install samba package (winbind is used for analysis purposes only but is valuable)${color_no}"
    sudo netstat -tlpn |grep ":53 "
    cat /etc/fstab |grep -i noacl
    sleep 5

    mount |grep -i noacl
    sudo apt-get install samba -y
    sudo apt-get install winbind -y
    sudo mv /etc/samba/smb.conf /etc/samba/smb.conf.org
    echo -e "${color_green}Domain name, DNS server of the provider / DSL router and user password for the Samba administrator are queried.${color_no}"
    sleep 5

    echo -e "${color_green}Please fill out the blanks.${color_no}"
    samba-tool domain provision

    echo -e "${color_green}Create /mnt/data  /mnt/home folder and shares.${color_no}"
    sleep 5
    mkdir /mnt/data
    mkdir /mnt/home
    echo "" >> /etc/samba/smb.conf
    echo "[data]" >> /etc/samba/smb.conf
    echo "       path = /mnt/data" >> /etc/samba/smb.conf
    echo "       writeable = yes" >> /etc/samba/smb.conf
    echo "" >> /etc/samba/smb.conf
    echo "[home]" >> /etc/samba/smb.conf
    echo "       path = /mnt/home" >> /etc/samba/smb.conf
    echo "       writeable = yes" >> /etc/samba/smb.conf

    cat /etc/samba/smb.conf
    sleep 5
    echo "Part one was done" > $parttwo		#Crate hidden file
    echo -e "${color_green} The host system is restarted, after the restart the script with sudo rights must be restarted!${color_no}"
    sleep 3 &&  reboot
  else 						#This run part 1
    # sleep 15 && clear
    echo -e "${color_green}Install the NTP daemon, this is required so that all clients can synchronize their time with the domain controller. This is a prerequisite for the smooth running of Kerberos authentication.${color_no}"
    sleep 5
    sudo apt-get install ntp -y
    sudo service ntp start
    netstat -tulpn |grep ":123 "
    sudo apt-get remove ntpdate
    sudo service ntp restart
    ntpq --peers
    sleep 3 
    sudo systemctl disable dnsmasq
    echo "Part one was done" > $partone		#Crate hidden file
    echo -e "${color_green} The host system is restarted, after the restart the script with sudo rights must be restarted!${color_no}"
    sleep 3 &&  reboot
  fi
}

start_lab() {

    echo -e "${color_green}Install the NTP daemon, this is required so that all clients can synchronize their time with the domain controller. This is a prerequisite for the smooth running of Kerberos authentication.${color_no}"
    sleep 5
    sudo apt-get update
    sudo apt-get install ntp -y
    sudo service ntp start
    netstat -tulpn |grep ":123 "
    sudo apt-get remove ntpdate
    sudo service ntp restart
    ntpq --peers
    sleep 3
    sudo systemctl disable dnsmasq

    clear
    echo -e "${color_green}Install samba package (winbind is used for analysis purposes only but is valuable)${color_no}"
    sudo netstat -tlpn |grep ":53 "
    cat /etc/fstab |grep -i noacl
    sleep 5

    mount |grep -i noacl
    sudo apt-get install samba -y
    sudo apt-get install winbind -y
    sudo mv /etc/samba/smb.conf /etc/samba/smb.conf.org
    echo -e "${color_green}Domain name, DNS server of the provider / DSL router and user password for the Samba administrator are queried.${color_no}"
    sleep 5

    echo -e "${color_green}Please fill out the blanks.${color_no}"
    samba-tool domain provision

    echo -e "${color_green}Create /mnt/data  /mnt/home folder and shares.${color_no}"
    sleep 5
    mkdir /mnt/data
    mkdir /mnt/home
    echo "" >> /etc/samba/smb.conf
    echo "[data]" >> /etc/samba/smb.conf
    echo "       path = /mnt/data" >> /etc/samba/smb.conf
    echo "       writeable = yes" >> /etc/samba/smb.conf
    echo "" >> /etc/samba/smb.conf
    echo "[home]" >> /etc/samba/smb.conf
    echo "       path = /mnt/home" >> /etc/samba/smb.conf
    echo "       writeable = yes" >> /etc/samba/smb.conf

    cat /etc/samba/smb.conf
    sleep 5

    clear
    echo "deb http://download.webmin.com/download/repository sarge contrib" >> /etc/apt/sources.list
    echo -e "${color_green}Webmin is a modern, web control panel for any Linux machine. Manage your host about https://hostname/10000${color_no}"
    sudo apt-get update
    sleep 10
    wget http://www.webmin.com/jcameron-key.asc   #Sometimes they need twice or more trys
    pause 3
    wget http://www.webmin.com/jcameron-key.asc
    pause 3
    wget http://www.webmin.com/jcameron-key.asc
    sudo apt-key add jcameron-key.asc
    sleep 2

    sudo apt-get install webmin -y
    sudo netstat -tlpn
    sed -i '2i* - nofile 16384' /etc/security/limits.conf
    echo -e "${color_green} Done... ${color_no}"
}

if [[ "$var_startscript" == "y" || "$var_startscript" == "Y" ]]; then
  clear
  echo
  echo
  echo -e "${color_green}Production installation needs twice reboots and is the favorite of this site https://wiki.ubuntuusers.de/Howto/Samba4_als_Domaincontroller/. Lab installation need only one reboot when it is finished.${color_no}"
  echo
  read -p "   Production or Lab installation? (example, p or l): " var_instart

  if [[ "$var_instart" == "p" || "$var_instart" == "P" ]]; then
      echo "You have chosen the production installation."
      start_production
  elif [[ "$var_instart" == "l" || "$var_instart" == "L" ]]; then
      echo "You have chosen the lab installation."
      start_lab
  else
      echo "Exit ..."
      exit 0
  fi
else
  exit 0
fi