#!/bin/bash


####THIS FIRST SECTION DEALS WITH PURGING FILES FROM AND RECONFIGURING SYSLOG/JOURNAL/SNAP SERVICES

# Create the file if it does not exist and clear the contents of /etc/logrotate.d/rsyslog
if [ ! -f /etc/logrotate.d/rsyslog ]; then
    sudo  touch /etc/logrotate.d/rsyslog
    sudo  chown root:root /etc/logrotate.d/rsyslog 2> /dev/null
else
    sudo truncate -s 0 /etc/logrotate.d/rsyslog
    sudo chown root:root /etc/logrotate.d/rsyslog 2> /dev/null
fi

sudo tee /etc/logrotate.d/rsyslog > /dev/null << EOL
su root syslog
/var/log/kern.log
/var/log/syslog
{
    rotate 2
    daily
    size 350M
    missingok
    notifempty
    delaycompress
    compress
    postrotate
        /usr/lib/rsyslog/rsyslog-rotate
    endscript
}
EOL

# Insert the new line before the specified pattern in /etc/rsyslog.d/50-default.conf
if ! grep -q '\$outchannel mysyslog,/var/log/syslog,367001600' /etc/rsyslog.d/50-default.conf; then
   sudo sed -i '/\*\.\*;auth,authpriv\.none/i \$outchannel mysyslog,/var/log/syslog,367001600' /etc/rsyslog.d/50-default.conf
fi
# Replace the specified pattern with the new line in /etc/rsyslog.d/50-default.conf
   sudo sed -i "s/\*\.\*;auth,authpriv\.none\s*\-\/var\/log\/syslog/\*\.\*;auth,authpriv\.none          :omfile:\$mysyslog/" /etc/rsyslog.d/50-default.conf


sudo chown -R root:root /etc 2> /dev/null

sudo rm /var/log/syslog*
sudo systemctl restart rsyslog.service &
sudo logrotate -f /etc/logrotate.conf > /dev/null 2>&1
sudo logrotate -d /etc/logrotate.d/rsyslog > /dev/null 2>&1

#Purge journal files and change journald config.
sudo journalctl --vacuum-size=100M > /dev/null 2>&1
sudo journalctl --vacuum-files=5 > /dev/null  2>&1
sudo sed -i \
-e 's/^#\?SystemMaxUse=.*/SystemMaxUse=100M/' \
-e 's/^#\?SystemKeepFree=.*/SystemKeepFree=50M/' \
-e 's/^#\?SystemMaxFileSize=.*/SystemMaxFileSize=50M/' \
-e 's/^#\?SystemMaxFiles=.*/SystemMaxFiles=5/' \
/etc/systemd/journald.conf

sudo systemctl restart systemd-journald &

#Purge disabled snaps and set to retain only last two versions.
sudo snap set system refresh.retain=2 > /dev/null 2>&1
sudo sh -c 'rm -rf /var/lib/snapd/cache/*' > /dev/null 2>&1

LANG=C snap list --all | awk '/disabled/{print $1, $3}' | while read snapname revision; do sudo snap remove "$snapname" --revision="$revision" > /dev/null 2>&1 ; done

#Fully purge and clean and libreoffice associated packages/libraries.
sudo apt purge --auto-remove libreoffice* -y > /dev/null 2>&1 && sudo apt purge --auto-remove thunderbird* -y > /dev/null 2>&1 && sudo apt purge --auto-remove valgrind* -y > /dev/null 2>&1 && \
sudo apt clean -y > /dev/null 2>&1


###INTERACTIVE PORTION OF THE SCRIPT TO DEAL WITH VIDEO DATA AND DOWNLOADS/TRASH folders.

#First, let's add the GUID folders we find into an array.
GUID_ARRAY=()
for i in /opt/hanwha/mediaserver/var/data/* ; do
    if [ -d "$i" ] ; then GUID_ARRAY+=("$i") ; fi
    done

#Now, for the interactive part.

interact_one() ( while true; do
read -p "Would you like to delete any/all video recorded by WAVE to your OS drive (including analytics metadata)? Note, this will not affect video stored on external devices such as HDDs. Enter 'y' to continue or 'n' to deny : " answer
    if [[ "$answer" == "y" || "$answer" == "Y" ]]; then
        echo "--------------------------"
        echo "Performing the action..."
        echo "--------------------------"
         for i in "${GUID_ARRAY[@]}"; do
                sudo chown root:root  "$i"
                sudo rm -rf "$i"/*
                done

        break
    elif [[ "$answer" == "n" || "$answer" == "N" ]]; then
        echo "--------------------------"
        echo "FINE, we won't touch your footage."
        echo "--------------------------"
        break
    else
        echo "--------------------------"
        echo "Invalid input. Please enter 'y' or 'n'."
        echo "--------------------------"
    fi
done
)

interact_two() ( while true; do
read -p "Would you like to delete the contents of your Ubuntu user Downloads and Trash directories (this is not related to the media downloads performed via WAVE client unless the download folder was configured to be /home/wave/Downloads)? Please enter 'y' or 'n' : " answer
    if [[ "$answer" == "y" || "$answer" == "Y" ]]; then
        echo "--------------------------"
        echo "Performing the action..."
        echo "--------------------------"
        sudo rm -rf /home/wave/Downloads/*
        sudo rm -rf /home/wave/.local/share/Trash/*
        break
    elif [[ "$answer" == "n" || "$answer" == "N" ]]; then
        echo "--------------------------"
        echo "FINE, enjoy your already installed applications."
        echo "--------------------------"
        break
    else
        echo "--------------------------"
        echo "Invalid input. Please enter 'y' or 'n'."
        echo "--------------------------"
    fi
done
)

#interact_one
#interact_two

wait