#!/usr/bin/env sh

# Motivation: build gdb 12.1 from source because of this issue: https://github.com/Gallopsled/pwntools/issues/1783

# Optional: remove existing gdb
echo ">>>> remove existing gdb:"
echo ">>>> sudo apt remove gdb gdbserver"
#sudo apt remove gdb gdbserver

# Get latest source from https://ftp.gnu.org/gnu/gdb/
echo ">>>> Get latest source from https://ftp.gnu.org/gnu/gdb/:"
echo ">>>> wget https://ftp.gnu.org/gnu/gdb/gdb-12.1.tar.xz"
wget https://ftp.gnu.org/gnu/gdb/gdb-12.1.tar.xz

echo ">>>> tar xf gdb-12.1.tar.xz"
tar xf gdb-12.1.tar.xz

# Install necessary build tools
echo ">>>> Install necessary build tools:"
echo ">>>> sudo apt install -y build-essential texinfo bison flex"
sudo apt install -y build-essential texinfo bison flex python3-dev libgmp-dev
echo ">>>> cd gdb-12.1"
cd gdb-12.1
echo ">>>> Configure with python3 support"
echo ">>>> ./configure --with-python=/usr/bin/python3"
# Configure with python3 support
./configure --with-python=/usr/bin/python3

# Build (this takes a while)
echo ">>>> Build (this takes a while)"
echo ">>>> make -j8"
make -j8

# Install binary
echo ">>>> Install binary:"
echo ">>>> sudo make install"
sudo make install
