#! /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" >&2)

echo "Update" >&3
sudo apt-get update -y
# install all programms from the install.list
echo "Installing install.list" >&3
cat install.list | xargs sudo apt-get -y install
# uninstal all programms from the list
echo "Uninstalling uninstall.list" >&3
cat uninstall.list | xargs sudo apt-get -y purge
# download additional packages 
echo "Downloading extra packages from Download.list" >&3
cat download.list | xargs sudo wget -nv
# install the packages
echo "Installing all .deb files" >&3
sudo apt-get install -y ./*.deb
# set up flatpack
echo "Setting up flatpack" >&3
flatpak remote-add --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo 
# install languages by ISO 639-1 short
echo "Installing additional languages" >&3
for l in en uk ru ar; do
	sudo apt-get -y install $(check-language-support -l $l)
done
# import panel settings from file
echo "setting up desktop UI" >&3
xfce4-panel-profiles load default_panel.tar.bz2
# clean the system
echo "Update, Upgrade and cleaning" >&3
sudo apt-get update -y
sudo apt-get full-upgrade -y
sudo apt-get autoremove -y
sudo apt-get clean -y 
sudo apt-get autoclean -y

#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
DESKTOP_PATH=$(xdg-user-dir DESKTOP)

sudo cp -r desktop_files/. $DESKTOP_PATH/

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

sudo chown -R root:root /etc/skel

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