#!/usr/bin/env bash

echo 'Adapting permissions of "./.Xmodmap.example"'
chown $(whoami) ./.Xmodmap.example
chmod 444 ./.Xmodmap.example

echo 'Adapting permissions of "./.Xresources"'
chown $(whoami) ./.Xresources
chmod 644 ./.Xresources

echo 'Adapting permissions of "./.bash_aliases"'
chown $(whoami) ./.bash_aliases
chmod 644 ./.bash_aliases

echo 'Adapting permissions of "./.bashrc"'
chown $(whoami) ./.bashrc
chmod 644 ./.bashrc

echo 'Adapting permissions of "./.config"'
chown $(whoami) ./.config
chmod 755 ./.config

echo 'Adapting permissions of "./.config/.gitignore"'
chown $(whoami) ./.config/.gitignore
chmod 644 ./.config/.gitignore

echo 'Adapting permissions of "./.config/alacritty"'
chown -R $(whoami) ./.config/alacritty
find ./.config/alacritty -type d -exec chmod 755 {} \;
find ./.config/alacritty -type f -exec chmod 644 {} \;

echo 'Adapting permissions of "./.config/bspwm/bspwmrc"'
chown -R $(whoami) ./.config/bspwm/bspwmrc
find ./.config/bspwm -type d -exec chmod 755 {} \;
find ./.config/bspwm -type f -exec chmod 644 {} \;
chmod 755 ./.config/bspwm/bspwmrc

echo 'Adapting permissions of "./.config/dmenu"'
chown -R $(whoami) ./.config/dmenu
find ./.config/dmenu -type d -exec chmod 755 {} \;
find ./.config/dmenu -type f -exec chmod 644 {} \;
chmod 755 ./.config/dmenu/dmenu_bspwm_desktop
chmod 755 ./.config/dmenu/dmenu_bspwm_desktop_content
chmod 755 ./.config/dmenu/dmenu_bspwm_monitor
chmod 755 ./.config/dmenu/dmenu_run_with_aliases

echo 'Adapting permissions of "./.config/nitrogen/.gitignore"'
chown -R $(whoami) ./.config/nitrogen/.gitignore
find ./.config/nitrogen -type d -exec chmod 755 {} \;
find ./.config/nitrogen -type f -exec chmod 644 {} \;

echo '> Executing "./.config/nvim/.restore-permissions"'
cd ./.config/nvim
if [ $? -eq 0 ]; then
    ./.restore-permissions
    cd ../..
fi
echo '< Done executing "./.config/nvim/.restore-permissions"'

echo 'Adapting permissions of "./.config/polybar"'
chown -R $(whoami) ./.config/polybar
find ./.config/polybar -type d -exec chmod 755 {} \;
find ./.config/polybar -type f -exec chmod 644 {} \;
chmod 755 ./.config/polybar/launch

echo 'Adapting permissions of "./.config/sxhkd/sxhkdrc"'
chown -R $(whoami) ./.config/sxhkd/sxhkdrc
find ./.config/sxhkd -type d -exec chmod 755 {} \;
find ./.config/sxhkd -type f -exec chmod 644 {} \;

echo 'Adapting permissions of "./.config/vconsole.conf"'
chown $(whoami) ./.config/vconsole.conf
chmod 644 ./.config/vconsole.conf

echo 'Adapting permissions of "./.gitconfig"'
chown $(whoami) ./.gitconfig
chmod 644 ./.gitconfig

echo 'Adapting permissions of "./.gitignore"'
chown $(whoami) ./.gitignore
chmod 644 ./.gitignore

echo 'Adapting permissions of "./.gitmodules"'
chown $(whoami) ./.gitmodules
chmod 644 ./.gitmodules

echo 'Adapting permissions of "./.gnupg"'
chown -R $(whoami) ./.gnupg
find ./.gnupg -type d -exec chmod 700 {} \;
find ./.gnupg -type f -exec chmod 600 {} \;

echo 'Adapting permissions of "./.inputrc"'
chown $(whoami) ./.inputrc
chmod 644 ./.inputrc

echo 'Adapting permissions of "./.nvidia-xinitrc"'
chown $(whoami) ./.nvidia-xinitrc
chmod 644 ./.nvidia-xinitrc

echo '> Executing "./.password-store/.restore-permissions"'
cd ./.password-store
if [ $? -eq 0 ]; then
    ./.restore-permissions
    cd ..
fi
echo '< Done executing "./.password-store/.restore-permissions"'

echo 'Adapting permissions of "./.ssh/.gitignore"'
chown -R $(whoami) ./.ssh/.gitignore
find ./.ssh -type d -exec chmod 700 {} \;
find ./.ssh -type f -exec chmod 600 {} \;

echo 'Adapting permissions of "./.tmate.conf"'
chown $(whoami) ./.tmate.conf
chmod 644 ./.tmate.conf

echo 'Adapting permissions of "./.tmux.conf"'
chown $(whoami) ./.tmux.conf
chmod 644 ./.tmux.conf

echo 'Adapting permissions of "./.xinitrc"'
chown $(whoami) ./.xinitrc
chmod 644 ./.xinitrc

echo 'Adapting permissions of "./.xsession"'
chown $(whoami) ./.xsession
chmod 755 ./.xsession

echo 'Adapting permissions of "./.zsh"'
chown -R $(whoami) ./.zsh
find ./.zsh -path ./.zsh/plugins -prune -or -type d -exec chmod 755 {} \;
find ./.zsh -path ./.zsh/plugins -prune -or -type f -exec chmod 644 {} \;

echo 'Adapting permissions of "./.zshenv"'
chown $(whoami) ./.zshenv
chmod 644 ./.zshenv

echo 'Adapting permissions of "./.zshpath.example"'
chown $(whoami) ./.zshpath.example
chmod 444 ./.zshpath.example

echo 'Adapting permissions of "./.zshrc"'
chown $(whoami) ./.zshrc
chmod 644 ./.zshrc

echo 'Adapting permissions of "./AUR"'
chown $(whoami) ./AUR
chmod 755 ./AUR

echo 'Adapting permissions of "./AUR/.gitignore"'
chown $(whoami) ./AUR/.gitignore
chmod 644 ./AUR/.gitignore

echo 'Adapting permissions of "./AUR/update"'
chown $(whoami) ./AUR/update
chmod 755 ./AUR/update

echo 'Adapting permissions of "./README.md"'
chown $(whoami) ./README.md
chmod 644 ./README.md

echo 'Adapting permissions of "./Wallpapers"'
chown -R $(whoami) ./Wallpapers
find ./Wallpapers -type d -exec chmod 755 {} \;
find ./Wallpapers -type f -exec chmod 644 {} \;

chown $(whoami) ./.restore-permissions
chmod 755 ./.restore-permissions
