#!/bin/bash

# This file is a part of TorBox, an easy to use anonymizing router based on Raspberry Pi.
# Copyright (C) 2026 radio_24
# Contact: anonym@torbox.ch
# Website: https://www.torbox.ch
# Github:  https://github.com/radio24/TorBox
#
# This program 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 is 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/>.
#
# DESCRIPTION
# This script starts TFS and Nginx, if configured that way in torbox.run.
# It is executed by rc.local --> DON'T MOVE IT INTO TORBOX.LIB!
#
# SYNTAX
# ./start_tfs [<initial>]
# [<initial>] is optional and used, if it was started from rc.local
#
#
###### SET VARIABLES ######
#
#Set the the variables for the menu
MENU_WIDTH=80
MENU_HEIGHT_20=20

#Other variables
INITIAL=$1
TORBOX_PATH="/home/torbox/torbox"
RUNFILE="$TORBOX_PATH/run/torbox.run"
PID_PATH="$TORBOX_PATH/lib/fileshare/pid/"
WEBSERVER="nginx"
NGINX_DIR="/etc/nginx"

######## FUNCTIONS ########

# include lib
.  /home/torbox/torbox/lib/torbox.lib

######## MAIN ########

n=$(grep -c "^TFS-" "${RUNFILE}")
if [ $n -gt 0 ] ; then
	i=0
	until [ $i -eq $n ]; do
		((i++))
		TFS_CONFIG_LIST=$(grep -m $i "^TFS-" "${RUNFILE}" | cut -d "=" -f2-)
		SERVICE_NAME=$(cut -d " " -f3 <<< ${TFS_CONFIG_LIST})
		if [[ ! -f $PID_PATH$SERVICE_NAME.pid && -z "$INITIAL" ]]; then
			# This will give VIRTPORT and HIDDENSSERVICEPORT
			find_virtport "${SERVICE_NAME}"
			if (whiptail --title "TorBox - TorBox's File Sharing capabilities" --no-button "REMOVE" --yes-button "RESTART" --yesno "\nThere is already a TFS configuration for \"$SERVICE_NAME\" in the TorBox run-file, but not running yet. Do you want do restart TFS or remove the old configuration?" $MENU_HEIGHT_20 $MENU_WIDTH); then
				# NEW - TorBox v.0.5.5
				(sudo rm /var/run/tfs_$SERVICE_NAME.sock) &>/dev/null
				TFS_CONFIG_LIST="nohup sudo /home/torbox/torbox/$TFS_CONFIG_LIST &"
				# Start tfs program
				(eval $TFS_CONFIG_LIST) &>/dev/null
				# Check Nginx configuration
				if [ ! -f "${NGINX_DIR}/sites-available/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf" ]; then
					(cp "etc/nginx/sites-available/sample-onion-filesharing.conf" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf") >/dev/null
					UNIX_PATH="unix:/var/run/${SERVICE_NAME}-onion"
					HIDDENSSERVICEPORT=$(grep -m 1 "$UNIX_PATH" ${TORRC})
					#Bash specific, but should also work with other shells
					HIDDENSSERVICEPORT=${HIDDENSSERVICEPORT//#}
					TARGET=$(cut -d ' ' -f3 <<< $HIDDENSSERVICEPORT)
					sed -i'' "s|TARGET|${TARGET}|g" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf"
					# This will give $ONION_HOSTNAME
					test_service_exists $SERVICE_NAME
					sed -i'' "s|ONION_HOSTNAME|${ONION_HOSTNAME}|g" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf"
					sed -i'' "s|SERVICE|${SERVICE_NAME}|g" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf"
					# NEW - TorBox v.0.5.5: Not necessary with TFS v.2
					# sed -i'' "s|FOLDER|$TFS_PATH|" "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf"
					(sudo mv "/tmp/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf" "/etc/${WEBSERVER}/sites-available/") >/dev/null
					(sudo ln -sf "${NGINX_DIR}/sites-available/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf" ${NGINX_DIR}/sites-enabled/) >/dev/null
					echo -e "${RED}[+] Reloading Nginx to apply new configuration...${NOCOLOR}"
				fi
			else
				echo -e "${RED}[+] Removing TFS configuration...${NOCOLOR}"
				sudo sed -i "/^TFS-${SERVICE_NAME}/d" "${RUNFILE}"
				echo -e "${RED}[+] Removing Nginx configuration...${NOCOLOR}"
				(sudo rm "${NGINX_DIR}/sites-enabled/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf") &>/dev/null
				(sudo rm "${NGINX_DIR}/sites-available/${SERVICE_NAME}-${VIRTPORT}-onion-filesharing.conf") &>/dev/null
				echo -e "${RED}[+] Reloading Nginx to apply new configuration...${NOCOLOR}"
			fi
		elif [[ ! -f $PID_PATH$SERVICE_NAME.pid && "$INITIAL" == "initial" ]]; then
			# NEW - TorBox v.0.5.5
			(sudo rm /var/run/tfs_$SERVICE_NAME.sock) &>/dev/null
			TFS_CONFIG_LIST="nohup sudo /home/torbox/torbox/$TFS_CONFIG_LIST &"
			(eval $TFS_CONFIG_LIST) &>/dev/null
		elif [[ -f $PID_PATH$SERVICE_NAME.pid && "$INITIAL" == "initial" ]]; then
			# NEW - TorBox v.0.5.5
			(sudo rm $PID_PATH_PATH$SERVICE_NAME.pid) &>/dev/null
			(sudo rm /var/run/tfs_$SERVICE_NAME.sock) &>/dev/null
			TFS_CONFIG_LIST="nohup sudo /home/torbox/torbox/$TFS_CONFIG_LIST &"
			(eval $TFS_CONFIG_LIST) &>/dev/null
		fi
	done
fi

# sudo systemctl reload nginx will not create new socks !!
#sudo ls /var/run/ | grep .*-onion-.*.sock | xargs -I {} -d"\n" sudo rm /var/run/{}
#sudo systemctl restart nginx
sudo systemctl reload nginx
