#!/bin/bash

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[0;93m'
MAGENTA='\033[0;35m'
NC='\033[0m' # No Color

SLEEP_TIME="0.5"

LOG="/var/log/remove_i386.log"

whoami | grep root || {
    echo "Error, should be launched as root"
    echo "Error, should be launched as root" >> $HOME/remove_i386.log
    exit 1
}

function print() {
    echo -e "${GREEN}$1${NC}"
    echo $1 >> $LOG
}

function print_error() {
    echo -e "${RED}$1${NC}"
    echo $1 >> $LOG
}

date > $LOG
print "Starting i386 remove procedure"

apt list --installed 2>&1 | grep " i386 " | awk -F \/ '{print $1}' > /tmp/i386.list
echo >> /tmp/i386.list
cat /tmp/i386.list | sed ':a;N;$!ba;s/\n/:i386 /g' > /tmp/i386.list.2
list=`cat /tmp/i386.list.2`

echo $list | grep "i386" || {
    print_error "No i386 packs found, exiting"
    exit 2
}

full_list=`LANGUAGE=C apt remove -s $list 2>&1 | awk '/REMOVED:/,/Remv /' | sed  '$ d'|sed  '$ d' | sed -n '1!p'`

echo $LANG | grep "ru_" ; code=$?
if [ $code -eq 0 ]
then
    title="Внимание"
    text1="Будут удалены следующие пакеты:\n$full_list"
    text2="Удалить сейчас?"
else
    title="Warning"
    text1="Packages to be removed:\n$full_list"
    text2="Do you want to proceed?"
fi


zenity  --width=384 --height=128 --question --title "$title"  --text "$text1\n\n$text2"; code=$?
if [ $code -eq 0 ]
    then print "Proceeding..."
    else echo -e "${RED}Exiting...${NC}"
        exit 1
fi

apt remove -y --allow-remove-essential $list 2>&1 | grep "" >> $LOG
print "Packages removed. Please reboot"
