#!/bin/bash

##

set -e

set -x

##

reset

clear

##


##

processorCount=`grep -c '^processor' /proc/cpuinfo`

processorCount=$((processorCount - 1))

echo "num cpus / "$processorCount

for i in $(seq 2 "$processorCount"); do

  echo "Processing unit: $i"

##

rm -f compose.yaml

echo "services:" >> compose.yaml

##

for x in $(seq 1 "$processorCount"); do

  i=$(printf "%03d" "$x")

  # Create the service definition
  service_def=$(cat <<EOF


##########################################
##
## orchestration / yankee-desktop-remix
##
##########################################$i

    yankee-desktop-remix-$i:
        container_name: yankee-desktop-remix-$i
        image: softwareshinobi/yankee-desktop-remix
        restart: unless-stopped
        build:
            context: .
            dockerfile: Dockerfile
        volumes:
            - yankee-nfs-downloads:/root/Downloads
        hostname: yankee-desktop-$i
        ports:
            - "2${i}2:22"
            - "2${i}8:80"
            - "2${i}9:5900"
        environment:
            TZ: America/Bogota
            USER_UID: 1000
            USER_GID: 1000
            USER: root
            PASSWORD: yankee
            HTTP_PASSWORD: yankee

EOF
)

  # Append the service definition to compose.yaml
  echo "$service_def" >> compose.yaml

done


# Add the volumes section ONCE after the loop (outside)
cat <<EOF >> compose.yaml

##########################################
##
## volumes / yankee-desktop-remix
##
##########################################

volumes:
    yankee-nfs-downloads:
        driver_opts:
            type: "nfs"
            o: nfsvers=4,addr=osiris.yankee.embanet.online,nolock,hard,rw
            device: ":/"

EOF
  #... your commands to process each unit...

done
##

docker compose down --remove-orphans

docker compose up -d

docker stats
