#!/data/data/com.termux/files/usr/bin/bash

show_help() {
    echo -e "Termux Fonts
Usage: termux-fonts [command]
                    [options]
Command:
        change        : Change termux font
        reset         : Reset termux font to default

Options:
        -h, --help    : Show this help
        -v, --version : Show the Version

for more info visit: https://github.com/ArieSR91
"
}

show_version() {
    echo "1.2.0-SR"
}

change_font() {
    FOLDER="$PREFIX/share/fonts/TTF"
    DAFTAR_FILE=$(find "$FOLDER" -maxdepth 1 -type f -exec basename {} \; | cut -d '.' -f1 | paste -sd, -)
    if [ -z "$DAFTAR_FILE" ]; then
        echo "Tidak ada file ditemukan di folder: $FOLDER"
        exit 1
    fi
    HASIL=$(termux-dialog radio -t "Select font" -v "$DAFTAR_FILE")
    STATUS=$(echo "$HASIL" | jq -r '.code' 2>/dev/null || echo "$HASIL" | sed -n 's/.*"code": \([^,]*\),.*/\1/p')
    if [ "$STATUS" != "-1" ]; then
        echo "Pemilihan file dibatalkan."
        exit 0
    fi
    FILE_TERPILIH=$(echo "$HASIL" | jq -r '.text' 2>/dev/null || echo "$HASIL" | sed -n 's/.*"text": "\(.*\)"}/\1/p')
    cp "${FOLDER}/${FILE_TERPILIH}.ttf" "$HOME/.termux/font.ttf"
    termux-reload-settings
    return 0
}

reset_font() {
    rm "$HOME/.termux/font.ttf"
    termux-reload-settings
}

case $1 in
    -h | --help)
        show_help
        ;;
    -v | --version)
        show_version
        ;;
    change)
        change_font
        ;;
    reset)
        reset_font
        ;;
    *)
        show_help
        ;;
esac
