#! /bin/bash
# write out to a file and error to different one
LOG="install.log"
LOG_IN_USER=$(who | awk 'NR==1{print $1;}')
exec 3>&1

# exec > "$LOG" 2> >(tee -a "$LOG" >&3)

echo "Update" >&3
sudo apt-get -y -qq \
  -o Dpkg::Progress-Fancy=0 \
  -o APT::Color=0 update
clear
# install all programms from the install.list
echo "Installing install.list" >&3
cat install.list | xargs sudo apt-get -y -qq \
  -o Dpkg::Progress-Fancy=0 \
  -o APT::Color=0  install
clear
# uninstal all programms from the list
echo "Uninstalling uninstall.list" >&3
cat uninstall.list | xargs sudo apt-get -y -qq \
  -o Dpkg::Progress-Fancy=0 \
  -o APT::Color=0 purge
clear
# download additional packages 
echo "Downloading extra packages from Download.list" >&3
cat download.list | xargs sudo wget -nv 3>&1
# install the packages
clear
echo "Installing all .deb files" >&3
sudo apt-get -y -qq \
  -o Dpkg::Progress-Fancy=0 \
  -o APT::Color=0 install ./*.deb 
clear
# set up flatpack
echo "Setting up flatpack" >&3
flatpak remote-add --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo 
clear
# install languages by ISO 639-1 short
echo "Installing additional languages" >&3
for l in en uk ru ar; do
	sudo apt-get -y -qq \
  -o Dpkg::Progress-Fancy=0 \
  -o APT::Color=0 install $(check-language-support -l $l)
done
sudo killall rustdesk
# clean the system
echo "Update, Upgrade and cleaning" >&3
sudo apt-get -y -qq \
  -o Dpkg::Progress-Fancy=0 \
  -o APT::Color=0 update
sudo apt-get -y -qq \
  -o Dpkg::Progress-Fancy=0 \
  -o APT::Color=0 full-upgrade
sudo apt-get -y -qq \
  -o Dpkg::Progress-Fancy=0 \
  -o APT::Color=0 clean
clear
#set nomacs as default
echo "setting default programs" >&3
FILE="/usr/share/applications/mimeinfo.cache"
FILETYPES="org.nomacs.ImageLounge.desktop;XnView.desktop;gimp.desktop;simple-image-reducer.desktop;"
for type in png jpg jpeg gif; do
	sudo sed -i "s|^\(image/${type}=\).*|\1${FILETYPES}|" "$FILE"
done
# copy content of desktop_files to desktop

echo "Copying desktop files" >&3
cp -r desktop_files/. /home/csr/Schreibtisch/

# import panel settings
echo "setting up desktop UI" >&3
xfce4-panel-profiles load ./default_panel.tar.bz2
clear
# set up firefox
echo "Install UBlock" >&3
firefox https://addons.mozilla.org/de/firefox/addon/ublock-origin/ >/dev/null 2>&1 & disown
read -p "Continue with Enter" >&3
clear
echo "Set default Browser, Start page & Search engine" >&3
firefox about:preferences >/dev/null 2>&1 & disown
read -p "Continue with Enter" >&3
clear
echo "Create Chrome .desktop starter" >&3
read -p "Continue with Enter" >&3
clear
echo "Set up Click per mousepad" >&3
xfce4-mouse-settings
read -p "Continue with Enter" >&3
clear
echo "Set Font size to 11" >&3
xfwm4-settings
read -p "Continue with Enter" >&3
clear

echo "Add Keyboard layouts and shortcut" >&3
xfce4-keyboard-settings
read -p "Continue with Enter" >&3
clear

# create skel for new user UI
echo "Setting up skel" >&3
sudo mkdir /etc/skel/Schreibtisch
sudo mkdir /etc/skel/.config
sudo mkdir /etc/skel/.local
sudo cp -r /home/csr/.config/* /etc/skel/.config/
sudo cp -r /home/csr/.local/* /etc/skel/.local/
sudo cp -r /home/csr/Schreibtisch/* /etc/skel/Schreibtisch/

sudo chown -R root:root /etc/skel

read -n 1 -s -r -p "Fertig, schließen mit Tastendruck" >&3