#!/bin/bash
# Thanks to tmn505
# https://aur.archlinux.org/packages/p7zip-gui/
#
# (U) KAPITALSIN 2022
#
# p7zip 16.02 GUI compiler B23022022 for Linux mint 19.3 (Debian forks)
#
# Script tested and working under:
# ================================
# -Linux mint 19.3 x86_64 GLIBC 2.27
# -Linux mint 19.3 i686 GLIBC 2.27
#
#-CAMBIADO GTK2 POR GTK3
#
# WxWidgets are integrated in the binary files
#
#sudo xbps-install -S gcc yasm gtk+3-devel wget patch pkg-config xz git
#sudo apt-get install yasm linux-libc-dev libgtk-3-dev libwxgtk3.0-dev p7zip-full
#
clear
currentver="$(gcc -dumpversion)"
requiredver="9"
 if [ "$(printf '%s\n' "$requiredver" "$currentver" | sort -V | head -n1)" = "$requiredver" ]; then
        echo "GCC valid found"
 else
        echo "GCC version "$currentver" found, version ${requiredver} or greater is required to compile p7zipGUI, now we are going to install the required GCC"

notify-send "INSTALLING NEW GCC"
echo "INSTALLING NEW GCC"
sudo apt-get update
sudo apt-get install software-properties-common
sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-9 g++-9 -y
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 9
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 9 
sudo update-alternatives --auto gcc
#sudo cp --archive /usr/include/asm-generic /usr/include/asm
fi

sudo apt-get install yasm linux-libc-dev libgtk-3-dev libwxgtk3.0-dev p7zip-full -y

MACHINE_TYPE=`uname -m`
glibc="$(ldd --version | awk '/ldd/{print $NF}')"

time_start=`date +%s`
wget -N https://fossies.org/linux/misc/p7zip_16.02_src_all.tar.xz
tar xvfa p7zip_16.02_src_all.tar.xz

cd p7zip_16.02
wget  -N https://src.fedoraproject.org/rpms/p7zip/raw/rawhide/f/14-Fix-g++-warning.patch
wget  -N https://raw.githubusercontent.com/archlinux/svntogit-packages/a82b67f5d36f374afd154e7648bb13ec38a3c497/trunk/CVE-2016-9296.patch
wget  -N https://raw.githubusercontent.com/archlinux/svntogit-packages/a82b67f5d36f374afd154e7648bb13ec38a3c497/trunk/CVE-2017-17969.patch
wget  -N https://raw.githubusercontent.com/archlinux/svntogit-packages/a82b67f5d36f374afd154e7648bb13ec38a3c497/trunk/CVE-2018-5996.patch
wget  -N https://raw.githubusercontent.com/archlinux/svntogit-packages/a82b67f5d36f374afd154e7648bb13ec38a3c497/trunk/CVE-2018-10115.patch
wget  -N https://src.fedoraproject.org/rpms/p7zip/raw/rawhide/f/gcc10-conversion.patch

patch -p1 -i 14-Fix-g++-warning.patch
patch -p1 -i CVE-2016-9296.patch
patch -p1 -i CVE-2017-17969.patch
patch -p1 -i CVE-2018-5996.patch
patch -p1 -i CVE-2018-10115.patch
patch -p1 -i gcc10-conversion.patch

cp makefile.linux_amd64_asm makefile.machine
sed -i 's/-O -s/-O3 -s/g' makefile.machine
# sed -i 's/-m64/-m32/g' makefile.machine
# sed -i 's/-Dx64/-Dx86/g' makefile.machine
# sed -i 's/x64/x86/g' makefile.machine
# sed -i 's/amd64/x86/g' makefile.machines
# sed -i 's/x86_64/x86/g' makefile.machine
sed -i 's/x86_64-linux-gnu//g' CPP/7zip/*/*/*.depend
rm GUI/kde4/p7zip_compress.desktop

PATH=$PWD/wx3:$PATH
wget -N https://github.com/johna23-lab/filezilla/raw/main/wx3.7z
7z -y x wx3.7z
cd wx3
./configure --prefix=$(pwd) --with-gtk=3 --enable-monolithic  --disable-shared --enable-static --enable-unicode --with-libpng=builtin   --with-libjpeg=builtin  --with-libtiff=builtin  --with-zlib=builtin --with-expat=builtin
make -j$(nproc)
cd ..

cd Utils
sed -i 's/_do_not_use//g' generate.py
./generate.py
cd ..

make -j$(nproc) 7zFM 7zG
cd bin
strip 7zFM 7zG 7z.so
mkdir p7zip.16.02.gui

echo '#!/bin/sh' > p7zip.16.02.gui/7zf.sh
echo '   BIN=./7zFM' >> p7zip.16.02.gui/7zf.sh
echo 'export LD_LIBRARY_PATH=lib/x86_64-linux-gnu:/lib64:usr/lib/x86_64-linux-gnu/' >> p7zip.16.02.gui/7zf.sh
echo 'exec $BIN $@' >> p7zip.16.02.gui/7zf.sh
chmod a+x p7zip.16.02.gui/7zf.sh

for library in $(ldd "7zFM" "7zG" "7z.so"  | cut -d '>' -f 2 | awk '{print $1}')
do
	[ -f "${library}" ] && cp --verbose --parents "${library}" "p7zip.16.02.gui"
done

cp 7zFM 7zG 7z.so "p7zip.16.02.gui"
cp -r Codecs "p7zip.16.02.gui"

mv "p7zip.16.02.gui"  "../../p7zip.16.02.gui_GLIBC_"$glibc"_GTK3_"$MACHINE_TYPE""

echo "p7zip.16.02.gui COMPILED AT p7zip.16.02.gui_GLIBC_"$glibc"_GTK3_"$MACHINE_TYPE""

time_end=`date +%s`
time_exec=`expr $(( $time_end - $time_start ))`
echo "EL PROCESO DE COMPILADO HA TARDADO UN TOTAL DE $(($time_exec / 60)) minutos y $(($time_exec % 60)) segundos."
