#! /bin/bash

####################################
#### Variables
####################################
NEW_HOSTNAME="test"
NEW_DOMAINNAME="sigma.sbrf.ru"
DNS_SERVERS="10.21.7.212 10.21.7.214"
DNS_STATIC_SEARCHLIST="Sigma.sbrf.ru sberbank.ru"

####################################
#### Setting Software Repos
####################################
zypper repos | grep Yes | cut -f3 -d '|' | sed -e "s/ //" | awk '{print "zypper mr -dRK " $1}' | sudo bash
sudo zypper ar -G -c -n "SB-OpenSUSE-oss" -f http://10.23.48.12/opensuse/distribution/leap/42.1/oss/suse sb-opensuse-oss
sudo zypper mr -erk -p 5 SB-OpenSUSE-oss
sudo zypper ar -G -c -n "SB-OpenSUSE-update-oss" -f http://10.23.48.12/opensuse/update/leap/42.1/oss sb-opensuse-update-oss
sudo zypper mr -erk -p 5 SB-OpenSUSE-update-oss
sudo zypper ar -G -c -n "SB-OpenSUSE-packman" -f http://10.23.48.12/opensuse/packman/openSUSE_Leap_42.1 sb-opensuse-packman
sudo zypper mr -erk -p 5 SB-OpenSUSE-packman
sudo zypper ar -G -c -n "SB-OpenSUSE-Sky" http://10.23.48.12/opensuse/tel.red/repos/opensuse/42.1/ sb-opensuse-sky
sudo zypper mr -erk -p 5 SB-OpenSUSE-Sky
sudo zypper ar -G -c -n "SB-OpenSUSE-YandexBrowser-beta" -f http://10.23.48.12/opensuse/repo.yandex.ru/yandex-browser/rpm/beta/x86_64 sb-opensuse-yandexbrowser-beta
sudo zypper mr -erk -p 5  SB-OpenSUSE-YandexBrowser-beta
sudo zypper ar -G -c -n "nVidia Graphics Drivers" http://download.nvidia.com/opensuse/leap/42.1 nVidia-Graphics-Drivers
sudo zypper mr -erk -p 5 nVidia-Graphics-Drivers
sudo zypper ar -G -c -n "AMD/ATI Graphics Drivers" -f http://geeko.ioda.net/mirror/amd-fglrx/openSUSE_Leap_42.1/  AMD-Graphics-Drivers
sudo zypper mr -erk -p 5 AMD-Graphics-Drivers

sudo zypper -n up
sudo zypper -n dup
sudo zypper -n in nano yast2-online-update krb5-client

#sudo zypper -n in xrdp mono-complete


###############################################
### Setup Services
###############################################
sudo systemctl enable xrdp 
sudo systemctl enable xrdp-sesman 
sudo systemctl enable sshd 
sudo systemctl enable ntpd 

#sudo systemctl enable SuSEfirewall2 
#sudo SuSEfirewall2 start  
sudo systemctl disable SuSEfirewall2
sudo systemctl stop SuSEfirewall2

sudo systemctl start sshd
sudo systemctl start xrdp 
sudo systemctl start xrdp-sesman
sudo systemctl start ntpd 

###############################################
### Setting HOSTNAME, DOMAINNAME
###############################################
sudo hostname $NEW_HOSTNAME
sudo domainname $NEW_DOMAINNAME
echo $NEW_HOSTNAME.$NEW_DOMAINNAME | sudo tee /etc/HOSTNAME
echo $NEW_HOSTNAME.$NEW_DOMAINNAME | sudo tee /etc/hostname
sudo sed -i '/^127.0.0./D' /etc/hosts
echo "127.0.0.1 `hostname`.`domainname` `hostname` localhost" | sudo tee -a /etc/hosts
echo "127.0.0.2 `hostname`.`domainname` `hostname`" | sudo tee -a /etc/hosts

##############################################
### Setting up NameServers
##############################################
sudo sed -i '/NETCONFIG_DNS_STATIC_SEARCHLIST/D' /etc/sysconfig/network/config 
echo "NETCONFIG_DNS_STATIC_SEARCHLIST=\"$DNS_STATIC_SEARCHLIST\"" | sudo tee -a /etc/sysconfig/network/config
sudo sed -i '/NETCONFIG_DNS_STATIC_SERVERS/D' /etc/sysconfig/network/config
echo "NETCONFIG_DNS_STATIC_SERVERS=\"$DNS_SERVERS\"" | sudo tee -a /etc/sysconfig/network/config
sudo sed -i '/NETCONFIG_DNS_POLICY/D' /etc/sysconfig/network/config
echo "NETCONFIG_DNS_POLICY=\"STATIC NetworkManager\"" | sudo tee -a /etc/sysconfig/network/config

#############################################
#### Setting sudo
#############################################
sudo sed -i 's/Defaults\ targetpw/\#Defaults\ targetpw/g' /etc/sudoers
sudo sed -i 's/^ALL.*ALL=(ALL)\ ALL/\#ALL\ ALL=(ALL)\ ALL/g' /etc/sudoers

sudo cat <<EOF > /etc/sudoers.d/domain_users
localuser       ALL=(ALL) ALL 
%SIGMA\\Domain\ Users          ALL=(ALL) ALL 
%\\Domain\ Users          ALL=(ALL) ALL
%SIGMA\\Domain\ Admins      ALL=(ALL) NOPASSWD: ALL 
%\\Domain\ Admins      ALL=(ALL) NOPASSWD: ALL
EOF

#########################################
### Setup NTP servers
#########################################
sudo sed -i '/^server/D' /etc/ntp.conf
cat <<EOF | sudo tee -a /etc/ntp.conf 
server cab-vsp-dc00001.sigma.sbrf.ru iburst 
server cab-vsp-dc00002.sigma.sbrf.ru iburst 
server cab-vsp-dc00003.sigma.sbrf.ru iburst 
server cab-vsp-dc00004.sigma.sbrf.ru iburst 
server cab-vsp-dc00005.sigma.sbrf.ru iburst 
server cab-vsp-dc00006.sigma.sbrf.ru iburst 
server cab-vsp-dc00007.sigma.sbrf.ru iburst 
server cab-vsp-dc00008.sigma.sbrf.ru iburst
EOF

########################################
### Join AD Domain
########################################
sudo yast2 samba-client longhelp