#!/bin/sh

echo TODO ENABLE NETWORK INTERFACE

# ---- Deploy vars start

REGISTRY=thinkbook.valonso.devices.vpn.alonsom.com:5000

MYSQL_CONTAINER_NAME=alteria-mysql-arm64v8
SHINOBI_CONTAINER_NAME=alteria-shinobi-arm64v8

MYSQL_CONTAINER_VERSION=1.2
# MYSQL_CONTAINER_VERSION=1.1.1 # Sleep
SHINOBI_CONTAINER_VERSION=1.3
# SHINOBI_CONTAINER_VERSION=1.1.1 # Sleep

MYSQL_DIR=/etc/alteria/ip-cam/mysql
SHINOBI_DIR=/etc/alteria/ip-cam/shinobi
NGINX_DIR=/etc/alteria/ip-cam/nginx

MYSQL_ROOT_PASSWORD=rootpassword
MYSQL_DATABASE=ccio
MYSQL_USER=majesticflame
MYSQL_PASSWORD=1234
MYSQL_HOST_NAME=shinobiSQL

SHINOBI_HOME=/home/Shinobi
SHINOBI_DB_HOST="$MYSQL_HOST_NAME"
SHINOBI_DB_USER="$MYSQL_USER"
SHINOBI_DB_PASSWORD="$MYSQL_PASSWORD"
SHINOBI_DB_DATABASE="$MYSQL_DATABASE"
SHINOBI_UPDATE=false

# ---- Deploy vars end 

echo ------- MYSQL ---------

docker pull $REGISTRY/$MYSQL_CONTAINER_NAME:$MYSQL_CONTAINER_VERSION

docker network create --attachable --attachable -d bridge mysqlNet

mkdir -p $MYSQL_DIR/mysql-init
rm -f $MYSQL_DIR/mysql-init/user.sql || true

cat > $MYSQL_DIR/mysql-init/user.sql <<EOF
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY '$MYSQL_ROOT_PASSWORD';
CREATE DATABASE IF NOT EXISTS \`$MYSQL_DATABASE\`;
CREATE USER IF NOT EXISTS '$MYSQL_USER'@'%' IDENTIFIED WITH mysql_native_password BY '$MYSQL_PASSWORD'; 
GRANT ALL PRIVILEGES ON \`$MYSQL_DATABASE\`.\`*\` TO '$MYSQL_USER'@'%';
FLUSH PRIVILEGES;
EOF

mkdir -p $MYSQL_DIR/data
chown -R 999:999 $MYSQL_DIR/data

docker run -d \
  --platform linux/arm64/v8 \
  -v "$MYSQL_DIR/data:/var/lib/mysql" \
  -v "$MYSQL_DIR/my.cnf:/etc/my.cnf:ro" \
  -v "$MYSQL_DIR/mysql-init:/docker-entrypoint-initdb.d" \
  -e "MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD" \
  -e "MYSQL_DATABASE=$MYSQL_DATABASE" \
  -e "MYSQL_USER=$MYSQL_USER" \
  -e "MYSQL_PASSWORD=$MYSQL_PASSWORD" \
  --cpus=0.5 \
  --restart unless-stopped \
  --name shinobiSQL \
  --network mysqlNet \
  $REGISTRY/$MYSQL_CONTAINER_NAME:$MYSQL_CONTAINER_VERSION --mysql-native-password=ON \
  && sleep 1

MY_SQL=$(docker ps -a | grep shinobiSQL | awk '{ print $1 }')

docker logs shinobiSQL --tail 100

echo ---------------------------------------------
echo remove and finish?
echo ---------------------------------------------

read remove

echo MYSQL
echo $MY_SQL

if [[ "$remove" = "y" ]]
then
  echo "REMOVING !!!!!!!!!!!!!!!!!!!!!!"
  docker stop $MY_SQL || true
  docker rm $MY_SQL
  docker network remove mysqlNet
  rm -rf $MYSQL_DIR/data/*
  # rm -rf $SHINOBI_DIR/data/*
  # exit
fi

echo ------- SHINOBI ---------
echo  -----------------------------------------
echo pulling $REGISTRY/$SHINOBI_CONTAINER_NAME:$SHINOBI_CONTAINER_VERSION
echo  -----------------------------------------

docker pull $REGISTRY/$SHINOBI_CONTAINER_NAME:$SHINOBI_CONTAINER_VERSION

mkdir -p $SHINOBI_DIR/data
chown -R 999:999 $SHINOBI_DIR/data

docker run -d \
  -e "HOME=$SHINOBI_HOME" \
  -e "DB_HOST=$SHINOBI_DB_HOST" \
  -e "DB_USER=$SHINOBI_DB_USER" \
  -e "DB_PASSWORD=$SHINOBI_DB_PASSWORD" \
  -e "DB_DATABASE=$SHINOBI_DB_DATABASE" \
  -e "SHINOBI_UPDATE=$SHINOBI_UPDATE" \
  --cpus=2.0 \
  --restart unless-stopped \
  -v "$SHINOBI_DIR/data:/home/Shinobi" \
  -v "/dev/shm/ShinobiRAM:/dev/shm/streams" \
  --name shinobiMain \
  --network mysqlNet \
  $REGISTRY/$SHINOBI_CONTAINER_NAME:$SHINOBI_CONTAINER_VERSION && sleep 5

MY_SHINOBI=$(docker ps -a | grep shinobiMain | awk '{ print $1 }')

docker logs shinobiMain --tail 100                                
                                                  
echo ---------------------------------------------
echo remove and finish shinobiMain?                           
echo ---------------------------------------------
                            
read remove                 
                                                  
echo MY_SHINOBI                        
echo $MY_SHINOBI                    
echo MYSQL                        
echo $MY_SQL                                      
                                           
if [[ "$remove" = "y" ]]                   
then                                       
  echo "REMOVING !!!!!!!!!!!!!!!!!!!!!!"
  docker stop $MY_SHINOBI || true                
  docker rm $MY_SHINOBI                                     
  docker network remove mysqlNet
  rm -rf $SHINOBI_DIR/data/*
fi     

echo ------- NGINX ---------

  # -p 554:554 \
  # -p 8080:8080 \
  # TODO add selfsigned certs
docker run -d \
  --network mysqlNet \
  --restart unless-stopped \
  -p 80:80 \
  -p 443:443 \
  --cpus=0.5 \
  --name nginx \
  -v $PWD/nginx/nginx.conf:/etc/nginx/nginx.conf \
  -v $PWD/nginx/http.d:/etc/nginx/http.d \
  -v $PWD/nginx/index.html:/ip-cam/index.html \
  nginx && sleep 5

MY_C=$(docker ps -a | grep nginx | awk '{ print $1 }')

docker logs nginx

echo
echo
echo
echo ---------------------------------------------
echo remove?
echo ---------------------------------------------

read remove

echo 
echo $MY_C

if [[ "$remove" = "y" ]]
then
docker stop $MY_C || true
docker rm $MY_C
fi

echo ------DONE--------
