#!/bin#!/bin/bash

# Get the current timestamp for logging or unique identifier
startDate=$(date "+%Y%m%d%H%M%S")

# Log file for all actions
log_file="/var/log/pandabot.log"

# Function to log messages to both console and log file with timestamp
log_message() {
    local message=$1
    local timestamp=$(date '+%Y-%m-%d %H:%M:%S')
    
    # Log to console
    echo "$timestamp - $message"

    # Log to file
    echo "$timestamp - $message" >> "$log_file"
}

# Source external bot configuration and utility functions
. inc/sh/inc.bot.sh

# Define various bot service paths and session files
bot_root=$(pwd)
bot_pid="${bot_root}/log/tmp/pandabot.pid"
madeline_session="${bot_root}/session.madeline"
madeline_pid=$(ps -ef | grep "${madeline_session}" | grep -v grep | awk '{print $2}')
apache_pid="${bot_root}/log/tmp/httpd.pid"
fpm_pid="${bot_root}/log/tmp/php-fpm.pid"

# Configuration file for bot and services
pandabotconf="${bot_root}/conf/pandabot.conf"
services="bot|web|apache|fpm|madeline|"

# Check if pandabot.conf exists before attempting to read
if [ ! -f "$pandabotconf" ]; then
    log_message "ERROR: pandabot.conf not found at $pandabotconf"
    exit 2
fi

# Get the wait timer from the configuration file
waittimer=$(getpandabotconf "${pandabotconf}" "botsh_restart_wait_timer")
if [ $? -ne 0 ]; then
    log_message "ERROR: botsh_restart_wait_timer not configured @ ${pandabotconf}"
    exit 2
fi

# Check if madeline session file exists before trying to use it
if [ ! -f "$madeline_session" ]; then
    log_message "ERROR: Madeline session file not found at $madeline_session"
    exit 2
fi

# Ensure that at least one argument is provided
if [ $# -eq 0 ]; then
    bot_sh_usage
    exit 1
fi

# Process command line arguments and options
OPTIND=2
while getopts "s:" arg; do
  case "${arg}" in
    s) service=${OPTARG} ;;   # Set the service if -s option is used
  esac
done

# Check if the specified service is valid
if [ ! -z "${service}" ]; then
    out=$(checkservice "${service}" "${services}")
    if [ $? -ne 0 ]; then
        log_message "ERROR: Service '${service}' not found in the list: ${services}"
        bot_sh_usage
        exit 4
    fi
else
    service="all"  # Default to all services if no specific service is given
fi

# Main logic for managing services based on the command
case "$1" in
  "start")
    start_service "${service}"
    log_message "Started service: ${service}"
    ;;
  
  "stop")
    stop_service "${service}"
    log_message "Stopped service: ${service}"
    ;;
  
  "restart")
    stop_service "${service}"
    log_message "Stopped service: ${service}"
    echo "Wait ${waittimer} seconds"
    
    # Validate waittimer (ensure it's a positive integer)
    if ! [[ "${waittimer}" =~ ^[0-9]+$ ]] || [ "${waittimer}" -le 0 ]; then
        log_message "ERROR: Invalid waittimer value in ${pandabotconf}. Must be a positive integer."
        exit 3
    fi
    sleep "${waittimer}"
    start_service "${service}"
    log_message "Restarted service: ${service}"
    ;;
  
  "setuptg")
    echo "Setting up TG for service: ${service}"
    log_message "Setting up TG for service: ${service}"
    bin/setuptg.php
    ;;
  
  "status")
    status_service "${service}"
    log_message "Checked status of service: ${service}"
    ;;
  
  *)
    # Default case: show usage if no valid argument is given
    bot_sh_usage
    log_message "ERROR: Invalid argument provided"
    ;;
esac

/bash
startDate=`date "+%Y%m%d%H%M%S"`;

. inc/sh/inc.bot.sh

bot_root=`pwd`
bot_pid=${bot_root}/log/tmp/pandabot.pid 
madeline_session=${bot_root}/session.madeline
madeline_pid=`ps -ef | grep ${madeline_session} | grep -v grep | awk '{print $2}'` 
apache_pid=${bot_root}/log/tmp/httpd.pid 
fpm_pid=${bot_root}/log/tmp/php-fpm.pid

pandabotconf=${bot_root}/conf/pandabot.conf
services="bot|web|apache|fpm|madeline|";

waittimer=`getpandabotconf "${pandabotconf}" "botsh_restart_wait_timer"`
[ $? -ne 0 ] && echo "ERR: botsh_restart_wait_timer not configured @ ${pandabotconf}" && exit 1

[ $# -eq 0 ] && bot_sh_usage && exit 1

OPTIND=2;
while getopts "s:" arg; do
 case "${arg}" in
  s) service=${OPTARG} ;;
 esac
done

if [ ! -z "${service}" ]; then 
 out=`checkservice "${service}" "${services}"` 
 [ $? -ne 0 ] && echo "service: ${out} not found" && bot_sh_usage && exit 1
else 
 service="all"
fi 

case "$1" in
 "start") 
   start_service "${service}"
  ;;	 
 "stop") 
   stop_service  "${service}"
  ;;	 
 "restart") 
   stop_service  "${service}"
   echo "wait ${waittimer} seconds"
   sleep ${waittimer}
   start_service "${service}"
  ;;	 
 "setuptg") 
   echo "${service}"
   bin/setuptg.php 
  ;;	 
  "status") 
   status_service "${service}" 
  ;;	 
  *)
   bot_sh_usage
  ;;
esac

