#!/bin/bash
#
# Freescout Service

PATH=$HOME/.docker/cli-plugins:/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin

config_freescout() {
  echo -ne "\n* Configuring freescout container.."
  spin &
  SPINPID=$!

  if [ ! -d "/federated/apps/freescout" ]; then
    mkdir -p /federated/apps/freescout/data/data
    mkdir -p /federated/apps/freescout/data/www/logs
  fi

cat > /federated/apps/freescout/docker-compose.yml <<EOF
version: "3.7"
services:
  freescout:
    image: ghcr.io/tiredofit/docker-freescout:\${IMAGE_VERSION}
    container_name: freescout
    hostname: freescout.$DOMAIN
    domainname: $DOMAIN
    restart: always
    networks:
      federated:
        ipv4_address: 172.99.0.37
    env_file:
      - ./.env
    volumes:
      - ./data/data:/data
      - ./data/www/logs:/www/logs
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.freescout.rule=Host(\`freescout.$DOMAIN\`)"
      - "traefik.http.routers.freescout.entrypoints=websecure"
      - "traefik.http.routers.freescout.tls.certresolver=letsencrypt"

networks:
  federated:
    external: true
EOF

ADMINPASS=`cat /federated/apps/panel/.env | grep SMTP_PASSWORD | awk -F= '{ print $2 }'`
EXTERNALIP=$(get_externalip);
FREESCOUT_SECRET=$(create_password);
# Set SMTP user based on if fcore exists
[[ $(docker exec ldap slapcat | grep fcore) ]] && SMTPUSER="fcore" || SMTPUSER="admin"

cat > /federated/apps/freescout/.env <<EOF
IMAGE_VERSION="latest_php8.3"
ADMIN_EMAIL=admin@$DOMAIN
ADMIN_PASS=$ADMINPASS
SITE_URL=https://freescout.$DOMAIN
DB_TYPE=pgsql
DB_HOST=postgresql.$DOMAIN
DB_USER=freescout
DB_PASS=$FREESCOUT_SECRET
DB_PORT=5432
DB_NAME=freescout
ENABLE_SSL_PROXY=TRUE
DISPLAY_ERRORS=FALSE
CONTAINER_ENABLE_MONITORING=FALSE
SMTP_HOST=mail.$DOMAIN
SMTP_USER=$SMTPUSER
SMTP_PASS=$ADMINPASS
SMTP_TLS=true
SMTP_PORT=587
CONTAINER_ENABLE_MESSAGING=TRUE
CONTAINER_MESSAGING_BACKEND=msmtp
EOF
chmod 600 /federated/apps/freescout/.env

# Create database and user in postgresql
docker exec postgresql psql -U postgres -c "CREATE USER freescout WITH PASSWORD '$FREESCOUT_SECRET'" &> /dev/null
docker exec postgresql psql -U postgres -c "CREATE DATABASE freescout" &> /dev/null
docker exec postgresql psql -U postgres -c "GRANT ALL PRIVILEGES ON DATABASE freescout TO freescout" &> /dev/null

kill -9 $SPINPID &> /dev/null
echo -ne "done.\n"
}
start_freescout() {
  # Start service with command to make sure it's up before proceeding
  start_service "freescout" "nc -z 172.99.0.37 80 &> /dev/null" "7"

  docker exec pdns pdnsutil add-record $DOMAIN freescout A 86400 $EXTERNALIP &> /dev/null
  [ $? -ne 0 ] && fail "Couldn't add dns record for freescout"

  kill -9 $SPINPID &> /dev/null
  echo -ne "done.\n"
}
email_freescout() {
  echo -ne "* Sending email to customer.."
  spin &
  SPINPID=$!

cat > /federated/apps/mail/data/root/certs/mailfile <<EOF
<html>
<img src="https://www.federated.computer/wp-content/uploads/2023/11/logo.png" alt="" /><br>
<p>
<h4>Freescout is now installed on $DOMAIN</h4>
<p>
Here is your applications chart on how to access this service:<br>
<p>
<h4>Applications</h4>
<style type="text/css">
.tg  {border-collapse:collapse;border-spacing:0;}
.tg td{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
  overflow:hidden;padding:10px 5px;word-break:normal;}
.tg th{border-color:black;border-style:solid;border-width:1px;font-family:Arial, sans-serif;font-size:14px;
  font-weight:normal;overflow:hidden;padding:10px 5px;word-break:normal;}
.tg .tg-cul6{border-color:inherit;color:#340096;text-align:left;text-decoration:underline;vertical-align:top}
.tg .tg-acii{background-color:#FFF;border-color:inherit;color:#333;text-align:left;vertical-align:top}
.tg .tg-0hty{background-color:#000000;border-color:inherit;color:#ffffff;font-weight:bold;text-align:left;vertical-align:top}
.tg .tg-kwiq{border-color:inherit;color:#000000;text-align:left;vertical-align:top}
.tg .tg-0pky{border-color:inherit;text-align:left;vertical-align:top}
</style>
<table class="tg" style="undefined;table-layout: fixed; width: 996px">
<colgroup>
<col style="width: 101.333333px">
<col style="width: 203.333333px">
<col style="width: 282.333333px">
<col style="width: 185.33333px">
<col style="width: 78.333333px">
<col style="width: 220.333333px">
</colgroup>
<thead>
  <tr>
    <th class="tg-0hty">Service</th>
    <th class="tg-0hty">Link</th>
    <th class="tg-0hty">User / Pass</th>
    <th class="tg-0hty">Access</th>
    <th class="tg-0hty">Docs</th>
    <th class="tg-0hty">Description</th>
  </tr>
</thead>
<tbody>
  <tr>
    <td class="tg-0pky">Freescout</td>
    <td class="tg-0pky"><a href="https://freescout.$DOMAIN/login" target="_blank" rel="noopener noreferrer"><span style="color:#340096">freescout.$DOMAIN</span></a></td>
    <td class="tg-0pky">admin@$DOMAIN<br>$ADMINPASS</td>
    <td class="tg-0pky">User access is separate from panel</td>
    <td class="tg-cul6"><a href="https://documentation.federated.computer/docs/getting_started/welcome/" target="_blank" rel="noopener noreferrer"><span style="color:#340096">Click here</span></a></td>
    <td class="tg-0pky">FreeScout is the super lightweight and powerful free open source help desk</td>
  </tr>
</tbody>
</table>
<h4>Thanks for your support!</h4>
<p>
Thank you for your support of Federated Computer. We really appreciate it and hope you have a very successful
time with Federated Core.
<p>
Again, if we can be of any assistance, please don't hesitate to get in touch.
<p>
Support: https://support.federated.computer<br>
Phone: (970) 722-8715<br>
Email: support@federated.computer<br>
<p>
It's <b>your</b> computer. Let's make it work for you!
</html>
EOF

  # Send out e-mail from mail container with details
  docker exec mail bash -c "mail -r admin@$DOMAIN -a \"Content-type: text/html\" -s \"Application installed on $DOMAIN\" $EMAIL < /root/certs/mailfile"
  rm /federated/apps/mail/data/root/certs/mailfile

  kill -9 $SPINPID &> /dev/null
  echo -ne "done.\n"
}
uninstall_freescout() {
  echo -ne "* Uninstalling freescout container.."
  spin &
  SPINPID=$!

  # First stop the service
  cd /federated/apps/freescout && docker-compose -f docker-compose.yml -p freescout down &> /dev/null
  
  # Delete database and user in postgresql
  docker exec postgresql psql -U postgres -c "DROP DATABASE freescout" &> /dev/null
  docker exec postgresql psql -U postgres -c "DROP USER freescout" &> /dev/null

  # Delete the app directory
  rm -rf /federated/apps/freescout

  kill -9 $SPINPID &> /dev/null
  echo -ne "done.\n"
}
