#!/bin/bash
#
# MSP Service

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

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

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

EXTERNALIP=$(get_externalip);
ADMINPASS=`cat /federated/apps/panel/.env | grep SMTP_PASSWORD | awk -F= '{ print $2 }'`
TRAEFIK_HTTPAUTH_STRING_MSP=$(echo `htpasswd -nb admin@$DOMAIN $ADMINPASS` | sed -e s/\\$/\\$\\$/g)

cat > /federated/apps/msp/docker-compose.yml <<EOF
version: '3.7'

services:
  msp:
    image: thebells1111/federated-msp:\${IMAGE_VERSION}
    container_name: msp
    hostname: msp.$DOMAIN
    domainname: $DOMAIN
    restart: always
    networks:
      federated:
        ipv4_address: 172.99.0.38
    volumes:
      - ./data/var/docker/msp/credentials:/usr/src/app/msp/credentials
      - ./data/var/docker/msp/mspDB:/usr/src/app/msp/mspDB
      - ./data/var/docker/msp/albums:/usr/src/app/msp/public
    env_file:
      - ./.env
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.msp.rule=Host(\`msp.$DOMAIN\`)"
      - "traefik.http.routers.msp.service=msp"
      - "traefik.http.routers.msp.entrypoints=websecure"
      - "traefik.http.routers.msp.tls.certresolver=letsencrypt"
      - "traefik.http.services.msp.loadbalancer.server.port=3000"
      - "traefik.http.routers.msp.middlewares=msp-auth"
      - "traefik.http.middlewares.msp-auth.basicauth.users=$TRAEFIK_HTTPAUTH_STRING_MSP"
      - "traefik.http.routers.mspnew.rule=Host(\`msp.$DOMAIN\`) && PathPrefix(\`/albums\`)"
      - "traefik.http.routers.mspnew.entrypoints=websecure"
      - "traefik.http.routers.mspnew.tls.certresolver=letsencrypt"
      - "traefik.http.routers.mspnew.service=mspnew"
      - "traefik.http.services.mspnew.loadbalancer.server.port=3000"

networks:
  federated:
    external: true
EOF

cat > /federated/apps/msp/.env <<EOF
IMAGE_VERSION="latest"
EOF
chmod 600 /federated/apps/msp/.env

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

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

  kill -9 $SPINPID &> /dev/null
  echo -ne "done.\n"
}
email_msp() {
  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>MSP (Music Side Project) 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-kwiq">Music Side Project</td>
    <td class="tg-kwiq"><a href="https://msp.$DOMAIN/" target="_blank" rel="noopener noreferrer"><span style="color:#340096">msp.$DOMAIN</span></a></td>
    <td class="tg-kwiq">admin@$DOMAIN<br>$ADMINPASS</td>
    <td class="tg-kwiq">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-kwiq">Music Side Project Studio enables you to harness the potential of RSS feeds and the Lightning Network, allowing you to self-host your music and receive direct payment from your fans</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"
}
