#!/bin/bash


disable_services(){
  cmd="genie -i"
  eval "${cmd}" &
  p=$!;
  sleep 30;
  kill $p;

  echo "systemd-modules-load ..."
  genie -c systemctl disable systemd-modules-load

  echo "ssh ..."
  genie -c systemctl disable ssh

  echo "multipathd ..."
  genie -c systemctl disable multipathd

  echo "auditd ..."
  genie -c systemctl disable auditd

  echo "openvpn ..."
  genie -c systemctl disable openvpn

  echo "ufw ..."
  genie -c systemctl disable ufw
  
  echo "lightdm ..."
  genie -c systemctl disable lightdm.service
  
  echo "gpu-manager ..."
  genie -c systemctl disable gpu-manager.service
}

fix_binfmt_misc(){
  echo "" > /etc/fstab
}

if [[ "$1" == "set_username" ]]
then
  sed -i 's/USER_WIN/'"$(wslvar USERNAME)"'/g' /mnt/c/users/$(wslvar USERNAME)/.ubuntu/03_start_desktop.vbs
fi


########
#   1
########
if [[ "$1" == "update_pkgs" ]]
then
  /usr/bin/curl "http://apt.ceibal.edu.uy/wsl2/apt-ceibal.gpg.key" | apt-key add -
  
  cat << EOF > /etc/apt/sources.list.d/ceibal.list
deb http://apt.ceibal.edu.uy/ubuntu/ ub20 main
deb-src http://apt.ceibal.edu.uy/ubuntu/ ub20 main
EOF
  
  export DEBIAN_FRONTEND=noninteractive && dpkg --configure -a && apt --fix-broken install && apt-get update && apt upgrade -y
fi


########
#   2
########
if [[ "$1" == "install_utils" ]]
then
  export DEBIAN_FRONTEND=noninteractive &&  dpkg --configure -a && apt --fix-broken install && apt install -y ceibal-repo-update-wsl2 axel unzip apt-transport-https libpulse0 wget 
fi


########
#   3
########
if [[ "$1" == "sound" ]]
then
  username=$(wslvar username)

  # download pulseaudio to the downloads directory
  axel --num-connections 10 --output "/mnt/c/users/${username}/downloads/pulseaudio.zip" https://code.x2go.org/releases/binary-win32/3rd-party/pulse/pulseaudio-5.0-rev18.zip

  # unzip pulseaudio in the downloads directory
  unzip "/mnt/c/users/${username}/downloads/pulseaudio.zip" -d "/mnt/c/users/${username}/downloads/pulse/"

  # move the pulse directory to the root directory of the hard drive
  mv "/mnt/c/users/${username}/downloads/pulse/pulse/" /mnt/c/pulse/

  # add the environment variables to the bash configuration file
  echo -e "\nexport HOST_IP=\"\$(ip route |awk '/^default/{print \$3}')\"\nexport PULSE_SERVER=\"tcp:\$HOST_IP\"\nexport DISPLAY=\"\$(cat /etc/resolv.conf | grep nameserver | awk '{ print \$2 }'):0.0\"\n"

  # reload the bash configuration file
  source ~/.bashrc

  # add the authorized ip address to the pulse configuration file
  echo -e 'load-module module-native-protocol-tcp auth-ip-acl=0.0.0.0/0\nload-module module-esound-protocol-tcp auth-ip-acl=0.0.0.0/0\nload-module module-waveout sink_name=output source_name=input record=0\n' >> /mnt/c/pulse/config.pa

  # disable the automatic shutdown setting
  sed "s/; exit-idle-time = 20/; exit-idle-time = -1/" -i "/mnt/c/pulse/default config files/daemon.conf"

  # download the nssm service manager
  wget -O "/mnt/c/users/${username}/downloads/nssm.zip" https://apt.ceibal.edu.uy/wsl2/nssm-2.24.zip

  # unzip the nssm service manager in the downloads directory
  unzip "/mnt/c/users/${username}/downloads/nssm.zip" -d "/mnt/c/users/${username}/downloads/"

  # move the nssm service manager to the pulse directory
  mv "/mnt/c/users/${username}/downloads/nssm-2.24/win64/nssm.exe" /mnt/c/pulse/

fi


########
#   4
########
if [[ "$1" == "install_genie" ]]
then
	# download the microsoft public key
	apt-key adv --fetch-keys https://packages.microsoft.com/keys/microsoft.asc

	# add microsoft to the source list directory for ubuntu 20.04
	sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/ubuntu/20.04/prod focal main" > /etc/apt/sources.list.d/microsoft-prod.list'

	axel --output /etc/apt/trusted.gpg.d/wsl-transdebian.gpg https://arkane-systems.github.io/wsl-transdebian/apt/wsl-transdebian.gpg

	# grant permissions to the gpg key
	chmod a+r /etc/apt/trusted.gpg.d/wsl-transdebian.gpg
	# add arkane systems to the source list directory

cat << EOF > /etc/apt/sources.list.d/wsl-transdebian.list
deb https://arkane-systems.github.io/wsl-transdebian/apt/ $(lsb_release -cs) main
deb-src https://arkane-systems.github.io/wsl-transdebian/apt/ $(lsb_release -cs) main
EOF

	# update the linux package information
	apt update
	# install genie
	apt install --yes systemd-genie

    # Salgo de la botella en shutdown o reboot.
cat << EOF > /etc/init.d/K99_genie
/usr/bin/genie -u 
EOF
    chmod a+x /etc/init.d/K99_genie
    ln -s /etc/init.d/K99_genie /etc/rc0.d/K99_genie
    ln -s /etc/init.d/K99_genie /etc/rc6.d/K99_genie
fi


########
#   5
########
if [[ "$1" == "install_gui" ]]
then
  export DEBIAN_FRONTEND=noninteractive && dpkg --configure -a && apt --fix-broken install && apt install -y --no-install-recommends lxde language-selector-gnome gdebi gnome-software ceibal-configure-wsl2-desktop google-chrome-stable  

  #Associa los .deb a gdebi
  xdg-mime default gdebi.desktop application/vnd.debian.binary-package

fi


########
#   6
########
if [[ "$1" == "user_ceibal" ]]
then
  useradd -m -s /bin/bash ceibal && echo 'ceibal:ceibal' | chpasswd; usermod -aG sudo ceibal > /dev/null
  echo 'ceibal ALL=(ALL) NOPASSWD:ALL' |  EDITOR='tee' visudo --file /etc/sudoers.d/ceibal > /dev/null
  
fi


########
#   7
########
if [[ "$1" == "services" ]]
then
  disable_services
  fix_binfmt_misc
fi

