#!/bin/bash
# typedload
# Copyright (C) 2026 Salvo "LtWorf" Tomaselli
#
# typedload is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# author Salvo "LtWorf" Tomaselli <tiposchi@tiscali.it>

set -e
cd $(dirname "$0")

VERSION=$1

if python$VERSION < /dev/null 2> /dev/null; then
    which python$VERSION
    exit 0
fi

case "$VERSION" in
  3.10)
    URL=https://www.python.org/ftp/python/3.10.20/Python-3.10.20.tgz
    FNAME=Python-3.10.20
    ;;
  3.11)
    URL=https://www.python.org/ftp/python/3.11.15/Python-3.11.15.tgz
    FNAME=Python-3.11.15
    ;;
  3.12)
    URL=https://www.python.org/ftp/python/3.12.13/Python-3.12.13.tgz
    FNAME=Python-3.12.13
    ;;
  3.13)
    URL=https://www.python.org/ftp/python/3.13.12/Python-3.13.12.tgz
    FNAME=Python-3.13.12
    ;;
  3.15)
    URL=https://www.python.org/ftp/python/3.15.0/Python-3.15.0b1.tgz
    FNAME=Python-3.15.0b1
esac

if [[ -e $FNAME/python ]]; then
    realpath "$FNAME/python"
    exit 0
fi


wget --continue "$URL"

tar -xvvf "$FNAME.tgz"
rm "$FNAME.tgz"

pushd $FNAME
./configure
make -j
popd

realpath "$FNAME/python"
