#!/bin/bash
## aa : 2023-01-18 15:25
## ps : 2023-02-01 15:25
## ps : 2023-12-18 15:25
## ps : 2024-01-17 15:25

set -o errexit
set -o pipefail
GITUSR=gitgsb
GITPRJ=gsb2024
apt-get update 
apt-get install -y lighttpd git 
STOREREP="/var/www/html/gsbstore" 
SRC="${SRC:-http://depl.sio.lan/gsbstore}"

GLPIREL=10.0.11
str="wget -nc -4 https://github.com/glpi-project/glpi/releases/download/${GLPIREL}/glpi-${GLPIREL}.tgz"

#GLPI Agent

GLPIAGVER=1.7
str31="wget -nc -4 https://github.com/glpi-project/glpi-agent/releases/download/${GLPIAGVER}/GLPI-Agent-${GLPIAGVER}-x64.msi"

#str32="wget -nc -4 https://github.com/glpi-project/glpi-agent/releases/download/${GLPIAGVER}/GLPI-Agent-${GLPIAGVER}-x86.msi"

FOGREL=1.5.10
str4="wget -nc -4 https://github.com/FOGProject/fogproject/archive/${FOGREL}.tar.gz -O fogproject-${FOGREL}.tar.gz"

WPREL=6.4.2
#v6.1.1 le 17/01/2023
str5="wget -nc -4 https://fr.wordpress.org/latest-fr_FR.tar.gz -O wordpress-6.4.2-fr_FR.tar.gz"

str6="wget -nc -4 https://github.com/goss-org/goss/releases/latest/download/goss-linux-amd64 -O goss"

str7="wget -nc -4 https://github.com/goss-org/goss/releases/latest/download/dgoss -O dgoss"

#GESTSUPREL=3.2.30
#str8="wget -nc -4 'https://gestsup.fr/index.php?page=download&channel=stable&version=${GESTSUPREL}&type=gestsup' -O gestsup_${GESTSUPREL}.zip"
str8="wget -nc -4 'https://gestsup.fr/index.php?page=download&channel=stable&version=3.2.30&type=gestsup' -O gestsup_3.2.30.zip"

#METRICBEAT ET FILEBEAT
ELKREL=8.11.4
str81="wget -nc -4 https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-${ELKREL}-amd64.deb"
str82="wget -nc -4 https://artifacts.elastic.co/downloads/beats/filebeat/filebeat-${ELKREL}-windows-x86_64.zip"
str83="wget -nc -4 https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-${ELKREL}-windows-x86_64.zip"
str84="wget -nc -4 https://artifacts.elastic.co/downloads/beats/metricbeat/metricbeat-${ELKREL}-amd64.deb"


[[ -d "${STOREREP}" ]] || mkdir "${STOREREP}"

(cat <<EOT > "${STOREREP}/getall"
#!/bin/bash
if [[ -z "${SRC+x}" ]]; then
  echo "erreur : variable SRC indefinie"
  echo " SRC : URL serveur deploiement"
  echo "export SRC=http://depl.sio.adm/gsbstore ; ./$0"
  exit 1
fi

${str}
${str31}
${str4}
${str5}
${str6}
${str7}

chmod +x ./goss ./dgoss

wget -nc -4 https://get.docker.com -O getdocker.sh 
chmod +x ./getdocker.sh

wget -nc -4 https://github.com/FiloSottile/mkcert/releases/download/v1.4.4/mkcert-v1.4.4-linux-amd64 -O mkcert
chmod +x ./mkcert

#${str8}

${str81} 
${str82} 
${str83} 
${str84} 
wget -nc -4 "${SRC}/zabbix.sql.gz" -O zabbix.sql.gz

EOT
)

cat "${STOREREP}/getall"

cd "${STOREREP}" || exit 2
bash getall
cp goss /usr/local/bin

(cat <<'EOT' > "${STOREREP}/inst1"
#!/bin/bash
if [[ -z "${HOST+x}" ]]; then
  echo "erreur : variable HOST indefinie"
  echo " HOST : adresse serveur deploiement"
  echo "export HOST=s-xyzt ; ./$0"
  exit 1
fi

hostname=$(hostname)
echo "${HOST}" > /etc/hostname
hostnamectl set-hostname "${HOST}"
sed -i "s/${hostname}/${HOST}/g" /etc/hosts
echo "vous pouvez redemarrer ..."
EOT
)

(cat <<'EOT' > "${STOREREP}/inst2"
#!/bin/bash

mkdir -p ~/tools/ansible ; cd ~/tools/ansible
git clone https://gitea.lyc-lecastel.fr/gsb/gsb2024.git
cd gsb2024/pre
DEPL=192.168.99.99 bash gsbboot
cd ../.. ; bash pull-config
EOT
)