#!/bin/bash
apt-get -qq update
apt-get -qq upgrade
echo "Installing nginx"
apt-get -qq install nginx
ufw allow 'Nginx Full'
echo "Installing zip"
apt-get -qq install zip
echo "Installing mysql"
apt-get -qq install mysql-server
echo "Installing Node.js"
apt-get -qq install -y ca-certificates curl gnupg
mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
NODE_MAJOR=18 # Use a supported version
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_$NODE_MAJOR.x nodistro main" | tee /etc/apt/sources.list.d/nodesource.list
apt-get -qq update
apt-get -qq install nodejs
echo "Creating 1GB swapfile"
fallocate -l 1G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap sw 0 0' | tee -a /etc/fstab
sysctl vm.swappiness=15
echo 'sysctl vm.swappiness=15' | tee -a /etc/sysctl.conf
sysctl vm.vfs_cache_pressure=60
echo 'sysctl vm.vfs_cache_pressure=60' | tee -a /etc/sysctl.conf
echo "Installing Ghost CLI"
npm install ghost-cli@latest -g
echo "Create bash scripts in /home/ubuntu"

OUTFILE=/home/ubuntu/setmysqlpassword.sh         # Name of the file to generate.
# -----------------------------------------------------------
# 'Here document containing the body of the setmysqlpassword.sh script.
(
cat <<'EOF'
#!/bin/bash

# Prompt for the native mySQL password
echo "Please enter the new native mySQL password: "
read password

# Enter mysql
sudo mysql <<mySQL_Input
# Update permissions
ALTER USER 'root'@'localhost' IDENTIFIED WITH 'mysql_native_password' BY '$password';
# Reread permissions
FLUSH PRIVILEGES;
# exit mysql
exit

mySQL_Input

# Prompt for the ghost directory name
echo "Please enter the new ghost installation directory: "
read ghostdirectory

sudo mkdir -p /var/www/$ghostdirectory
sudo chmod 775 /var/www/$ghostdirectory
sudo chown ubuntu:ubuntu /var/www/$ghostdirectory
echo "Created ghost installation directory"

exit 0
EOF
) > $OUTFILE
# -----------------------------------------------------------
if [ -f "$OUTFILE" ]
then
  chmod 755 $OUTFILE
  # Make the generated file executable.
else
  echo "Problem in creating file: \"$OUTFILE\""
fi

OUTFILE=/home/ubuntu/installtailscale.sh         # Name of the file to generate.
# -----------------------------------------------------------
# 'Here document containing the body of the installtailscale.sh script.
(
cat <<'EOF'
#!/bin/bash

# Prompt for the native mySQL password
echo "Install tailscale VPN as an exit node and enable ssh"
curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/jammy.noarmor.gpg | sudo tee /usr/share/keyrings/tailscale-archive-keyring.gpg >/dev/null
curl -fsSL https://pkgs.tailscale.com/stable/ubuntu/jammy.tailscale-keyring.list | sudo tee /etc/apt/sources.list.d/tailscale.list
sudo apt-get -qq update
sudo apt-get -qq install tailscale
echo 'net.ipv4.ip_forward = 1' | sudo tee -a /etc/sysctl.d/99-tailscale.conf
echo 'net.ipv6.conf.all.forwarding = 1' | sudo tee -a /etc/sysctl.d/99-tailscale.conf
sudo sysctl -p /etc/sysctl.d/99-tailscale.conf
printf '#!/bin/sh\n\nethtool -K %s rx-udp-gro-forwarding on rx-gro-list off \n' "$(ip -o route get 8.8.8.8 | cut -f 5 -d " ")" | sudo tee /etc/networkd-dispatcher/routable.d/50-tailscale
sudo chmod 755 /etc/networkd-dispatcher/routable.d/50-tailscale
sudo tailscale up --advertise-exit-node --ssh

exit 0
EOF
) > $OUTFILE
# -----------------------------------------------------------
if [ -f "$OUTFILE" ]
then
  chmod 755 $OUTFILE
  # Make the generated file executable.
else
  echo "Problem in creating file: \"$OUTFILE\""
fi

echo "Completed InstanceCustomInstallScript.bash"
exit 0

