#!/bin/bash
# Script to compile and install
# PREDICT under a Termux environment
# KD2BD -- July 2026

# Install all the required tools (if necessary). 

if [ ! -x /data/data/com.termux/files/usr/bin/gcc ]; then
	pkg install clang
fi

if [ ! -e /data/data/com.termux/files/usr/lib/libcurses.so ]; then
	pkg install ncurses
fi

if [ ! -e /data/data/com.termux/files/usr/lib/libcurl.so ]; then
	pkg install libcurl
fi

if [ ! -e /data/data/com.termux/files/usr/bin/man ]; then
	pkg install man
fi

if [ ! -x /data/data/com.termux/files/usr/bin/play-audio ]; then
	pkg install play-audio
fi

#if [ ! -x /data/data/com.termux/files/usr/bin/termux-volume ]; then
#	pkg install term-api
#fi

# Now configure, compile, and install PREDICT
# and PREDICT's man page.

rm -f predict
echo -n "Compiling PREDICT version `cat .version`... "

cc -Wall -Wno-deprecated-non-prototype -O3 -s -fomit-frame-pointer predict.c -lcurl -lncurses -o predict -lm -DBUILD_DIR="\"$(pwd)/\"" -DPREDICT_VERSION="\"$(cat .version)\""

if [ -x predict ]; then
	ln -sf `pwd`/predict /data/data/com.termux/files/usr/bin/predict
	echo "Built and installed!"
else
	echo "PREDICT compilation failed!  :-("
fi

gzip -c `pwd`/docs/man/predict.1 > /data/data/com.termux/files/usr/share/man/man1/predict.1.gz

if [ -e /data/data/com.termux/files/usr/share/man/man1/predict.1.gz ]; then
	echo "PREDICT's man page has been installed!"
fi
