#!/bin/bash
# Simple Solid Store Install Script 
# (c) Fabian V. Thobe for GMS 2025
# Stable Release version
# Tested against Ubuntu 24.04
#                                    
#
#
# Script Setup
## Ruby and Rails versions
rubyv=3.3.6
railsv=7.2.2.1
## Set Colors
red='\033[0;31m'
nc='\033[0m'
## Logo
echo "Logo 1"
echo -e "{RED}\n                ++++++++++++++                \n            ++++++++++++++++++++++            \n          ++++++++++++++++++++++++++          \n        ++++++++++++++++++++++++++++++        \n      +++++++++++++++++++++++++++++++++       \n      ++++++++++++++++++++++++++++++++++      \n     +++++++++++++++++++++++++++++++++++      Simple Solid Store Install Script \n      +++++++++++            +++              (c) Fabian V. Thobe for GMS 2025 \n        +++++++++            +++++            Stable Release version\n          +++++++            +++++++          \n            +++++            +++++++++        Tested against:\n             ++++            +++++++++++      Ubuntu 24.04Rails 7.2.2.1\n     ++++++++++++++++++++++++++++++++++++     Ruby 3.4.1\n     +++++++++++++++++++++++++++++++++++      nginx current\n      +++++++++++++++++++++++++++++++++       certbot current\n        ++++++++++++++++++++++++++++++        cloudflare dns\n         +++++++++++++++++++++++++++          \n            ++++++++++++++++++++++            \n               +++++++++++++++                \n                                              \n                                              \n\nFabian V. Thobe for GMS 2025 \nFree for Non-Commercial Use${NC}"                                                
echo "Logo 2"
echo -e "${RED}\n \n  ____   ___  _     ___ ____  _   _ ____  \n / ___| / _ \| |   |_ _|  _ \| | | / ___| \n \___ \| | | | |    | || | | | | | \___ \ \n  ___) | |_| | |___ | || |_| | |_| |___) |\n |____/ \___/|_____|___|____/ \___/|____/ \n${NC}" 
## Description
echo "This script installs Solidus including Ruby and Ruby on Rails in the latest maintained version on your machine."
echo "You will be asked to enter your password multiple times and this script will inform you about the purpose."
echo "Your machine will also be prepared to use nginx as reverse proxy and add certificates via let's encrypt using cloudflare."
echo "This script uses Ruby 3.4.1"
read -n 1 -s -r -p "Press any key to continue."

# Get a clean slate by updating Ubuntu (apt update & sudo apt upgrade)
apt update
echo "Please enter your password to upgrade Ubuntu"
sudo apt upgrade -y

# Install Notice Ruby Dependencies and dependencies to have a smooth install
echo -e "Following Programs will be installed if not present on the system: \ngit \ncurl  \nlibssl-dev \nlibreadline-dev \nzlib1g-dev \nautoconf \nbuson \nbuild-essential \nlibyaml-dev \nlibreadline-dev \nlibncurses5-dev \nlibffi-dev \nlibgdbm-dev \nginx \ncertbot \npython3-certbot-dns-cloudflare \nlibvips" 
echo "You might be asked to enter your password again."
read -n 1 -s -r -p "Press any key to continue."

# Install Ruby Dependencies, Rails Dependencies, certbot to have a smooth install
sudo apt install git curl libssl-dev libreadline-dev zlib1g-dev autoconf bison build-essential libyaml-dev libreadline-dev libncurses5-dev libffi-dev libgdbm-dev certbot python3 python3-certbot-dns-cloudflare libvips -y

# Install rbenv from the official install script and add rbenv and enabling it for this and future sessions
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/HEAD/bin/rbenv-installer | bash
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(rbenv init -)"' >> ~/.bashrc
source ~/.bashrc

# Install Ruby and configure supported version globally
rbenv install $rubyv
rbenv global $rubyv

# Skips local documentation of gem installs
echo "gem: --no-document" > ~/.gemrc

# Installs bundler 
gem install bundler

# Install Rails
gem install rails -v $railsv
rbenv rehash

# Asking user to enter FQDN of rails app
print "By now Ruby and Ruby on Rails are installed. Please enter the hostname of your application in the format server.mydomain.tld. \nYour app will not only be named like this, we will use this hostname also to configure your nginx server."
read hostname

# Setup a new rails app with the hostname as app name and installs solidus
rails new -T $hostname
cd ~/$hostname
bundle add solidus

# Checks if there are already Cloudflare DNS configuration files and creates the folders to store Cloudflare tokens
if [ -e ~/.secrets/certbot/cloudflare.ini ]; then
    echo "Cloudflare is already configured to be used by Certbot with DNS verification using Cloudflare. \nWe will try to request a certificate using following FQDN:"
    echo $hostname
    read -n 1 -s -r -p "Press any key to continue."
else
    echo -e "Cloudflare is not yet configured to be used for Certbot, \nPlease enter your API token to configure following FQDN:"
    echo $hostname
    read dns_cloudflare_api_token
    echo "We are now creating your file with the API token, you will find it in the following file: ~/.secrets/certbot/cloudflare.ini."
    sudo mkdir -p ~/.secrets/certbot/
    sudo touch ~/.secrets/certbot/cloudflare.ini
    sudo bash -c 'echo -e "# Cloudflare API token used by Certbot\ndns_cloudflare_api_token = test" > ~/.secrets/certbot/test.ini'
fi

# Refresh certificates
# echo "We are now creating sample certificates using the staigng system of Let's Encrypt"
# certbot certonly \
# --dns-cloudflare \
# --dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini \
# --dns-cloudflare-propagation-seconds 60 \
# -d $hostname

sudo apt install sqlite3
cd ~/$hostname