#!/bin/bash
# Rescapp Chat run script
# Copyright (C) 2012,2013,2014,2015,2016,2017,2018,2019,2020 Adrian Gibanel Lopez
#
# Rescapp 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.
#
# Rescapp is distributed in the hope that it will be 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 Rescapp.  If not, see <http://www.gnu.org/licenses/>.

source ${RESCAPP_LIB_FILE}


XCHAT_OK_STR="Hexchat was successfully started in the background."
XCHAT_KO_STR="Rescapp was not able to start Hexchat, is it installed?"

# Top right corner
WIDTH=600
HEIGHT=460
GRAVITY=0
DESKTOP_WIDTH=$(rtux_Get_Desktop_Width)
let X_POSITION=${DESKTOP_WIDTH}-${WIDTH}
Y_POSITION=0

USER_NUMBER=$$
let USER_NUMBER_2=$USER_NUMBER+1
let USER_NUMBER_3=$USER_NUMBER+2

XCHAT_CONF_DIR="$(mktemp -t -d hexchat-conf-XXXXXXXX)"

XCHAT_RESCAPP_VERSION="$(echo ${RESCAPP_VERSION} | sed 's/\.//g')"

cat << EOF > ${XCHAT_CONF_DIR}/hexchat.conf
irc_nick1 = ${RESC_USER_IRC_PREFIX}${XCHAT_RESCAPP_VERSION}_${USER_NUMBER}
irc_nick2 = ${RESC_USER_IRC_PREFIX}${XCHAT_RESCAPP_VERSION}_${USER_NUMBER_2}
irc_nick3 = ${RESC_USER_IRC_PREFIX}${XCHAT_RESCAPP_VERSION}_${USER_NUMBER_3}
EOF

XCHAT_FULL_PATH="$(which ${XCHAT_COMMAND})"
if [ -x "${XCHAT_FULL_PATH}" ] ; then
    "${XCHAT_FULL_PATH}" --cfgdir=${XCHAT_CONF_DIR} --url=${RESCATUX_IRC_URL} &
    sleep 2s;

    wmctrl \
    -r ${XCHAT_WINDOW_STR} \
    -e ${GRAVITY},\
${X_POSITION},\
${Y_POSITION},\
${WIDTH},\
${HEIGHT}
    rtux_Message_Success ${XCHAT_OK_STR}
else
    rtux_Message_Failure ${XCHAT_KO_STR}
fi

