#!/bin/sh
############################################
# Project email2sms relay installer script #
# ver 1.0.7-20260115                       #
############################################

_PROJ_TAR="email2sms.tar.gz"
_PROJ_URL="http://intellect-module.ru/downloads/service/email2sms.tar.gz"
_PROJ_DIR="/etc/email2sms"
_PROJ_SETUP="$_PROJ_DIR/reconfig.sh --silent"

RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
MAGENTA='\033[0;35m'
CYAN='\033[0;36m'
GRAY='\033[0;37m'
NC='\033[0m' # No Color


if [ "$(id -u)" -ne 0 ]; then
    echo -e "${RED} Must run as root ${NC}"
    exit 1
fi

echo "Running as root..."
sleep 2
clear

if [ -f "/etc/openwrt_release" ]; then
    OW_REL=$(. /etc/openwrt_release ; echo ${DISTRIB_RELEASE%.*} | sed -n '/^[0-9]*\.*[0-9]*$/p')
else
    OW_REL=$((0))
fi

[ -z "$OW_REL" ] && OW_REL=$((0))

if [ "$(which bc |wc -l)" -ne 0 ]; then
    OW_21=$(echo "$OW_REL >= 21" | bc -l)
else
    OW_21=$((0))
fi

if [ "$OW_21" -eq $((0)) ]; then
    echo ""
    echo ""
    echo ""
    echo -e "${RED} OpenWRT Version error! ${NC}"
    echo " DISTRIB_RELEASE == ""$OW_REL"" detected. OpenWRT release must be 21 or greater!"
    echo -e " Select 7 for: Force continue. ${RED} NOT RECOMENDED! ${NC}"
    echo " Other choice to EXIT"

     read -p " -Select Option: " choice

    case $choice in

    7)
        echo -e "Force continue. ${RED} AT YOUR OWN RISK! ${NC}"
        sleep 2
        echo "Press any key..."
        read -s -n 1
        clear
        ;;

    *)
        echo ""
        echo -e "${GREEN}Exiting...${NC}"
        exit 0

        read -s -n 1
        ;;
    esac
fi

if [ "$(which sed |wc -l)" -eq $((0)) ]; then
    echo -e "${RED} sed not found! Exit ${NC}"
    exit 1
fi
if [ "$(which wget |wc -l)" -eq $((0)) ]; then
    echo -e "${RED} wget not found! Exit ${NC}"
    exit 1
fi
if [ "$(which tar |wc -l)" -eq $((0)) ]; then
    echo -e "${RED} tar not found! Exit ${NC}"
    exit 1
fi
if [ "$(which grep |wc -l)" -eq $((0)) ]; then
    echo -e "${RED} grep not found! Exit ${NC}"
    exit 1
fi
if [ "$(which xargs |wc -l)" -eq $((0)) ]; then
    echo -e "${RED} xargs not found! Exit ${NC}"
    exit 1
fi
if [ "$(which opkg |wc -l)" -eq $((0)) ]; then
    echo -e "${RED} opkg not found! Exit ${NC}"
    exit 1
fi
#if [ ! -f "/etc/init.d/smstools3" ]; then
#    echo -e "${RED} smstools3 not found! Exit ${NC}"
#    exit 1
#fi

if [ ! -d "/root" ]; then
    echo ""
    echo ""
    echo -e "${RED} /root folder not found ${NC}"
    echo " The /tmp folder will be used. "
    echo -e " Select 1 for: Exit"
    echo " Other choice to Continue"

     read -p " -Select Option: " choice

    case $choice in

    1)
        echo 
        echo -e "${GREEN}Exiting...${NC}"
        exit 0

        read -s -n 1
        ;;
    *)
        echo "Continue..."
        clear
        ;;

    esac
    _DIR="/tmp/root"
    mkdir -p "$_DIR"
else
    _DIR="/root"
fi

if [ -f "$_DIR/$_PROJ_TAR" ]; then
    rm -f "$_DIR/$_PROJ_TAR"
fi

cd "$_DIR"

echo -e "${GREEN}Please wait!"
echo -e "${NC}"
echo "Executing."

wget -q "$_PROJ_URL" -O "$_PROJ_TAR"

echo -n "."

if [ -d "$_PROJ_DIR".OLD1 ]; then
   rm -rf "$_PROJ_DIR".OLD1
fi

if [ -d "$_PROJ_DIR".OLD ]; then
   mv -f "$_PROJ_DIR".OLD "$_PROJ_DIR".OLD1
fi

if [ -d "$_PROJ_DIR" ]; then
   mv -f "$_PROJ_DIR" "$_PROJ_DIR".OLD
fi

echo -n "."

mkdir -p "$_PROJ_DIR"
tar -xzf "$_PROJ_TAR" -C /etc/  

echo -n "."

### Install package ###

opkg update
sleep 3

echo -n "."

if [ $(opkg list-installed | grep -oc "emailrelay ") -ne $((0)) ]; then
    opkg remove emailrelay
    sleep 1
    echo -n "."
fi

opkg install emailrelay
sleep 1

echo -n "."

if [ $(opkg list-installed | grep -oc "python3 ") -eq $((0)) ]; then
    opkg install python3
    sleep 1
    echo -n "."
fi

if [ $(opkg list-installed | grep -oc "mc ") -eq $((0)) ]; then
    opkg install mc
    sleep 1
    echo -n "."
fi

if [ $(opkg list-installed | grep -oc "smstools3 ") -eq $((0)) ]; then
    opkg install smstools3
    sleep 1
    echo -e "${RED} Утилита smstools не сконфигурирована!"
    echo -e " Отправка SMS не будет работать без настройки smstool3 \(https://smstools3.kekekasvi.com\)"
    echo -e " Измените файл конфигурации /etc/smsd.conf для работы с вашим модемом"
    echo -e " Пример команды для проверки отправки сообщений: sendsms 79991234567 \"test message\" "
    echo -e " Номер телефона указывается в международном формате, без + \(для РФ 7\), пробелов и разделителей"
    echo -e "${NC}"
fi

echo -e "${GREEN}OK"

### reconfigure 
echo -e "Configuring."
echo -e "${NC}"
sh -c "$_PROJ_SETUP"
