install/install.sh
Victor Westerlund 8b80663a32 fix: install composer packages even though --install=n is passed (#4)
Composer packages should still be installed even if the `--install=n` flag is set. The only thing "--install=n" won't do is download and add git submodules

Reviewed-on: https://codeberg.org/reflect/install/pulls/4
2025-08-07 12:22:52 +02:00

305 lines
No EOL
6.7 KiB
Bash
Executable file

#!/bin/bash
# Initialize variables
db=""
cwd=""
dir=""
host=""
user=""
seed=""
install=""
password=""
seed_host=""
seed_user=""
overwrite=""
endpoints=""
seed_password=""
echo_err() {
echo "!! -> $1"
}
# Bail out from an error with a message asking the user to report the incident
exit_report() {
echo_err "Please report this error at: https://codeberg.org/reflect/install/issues"
exit 1
}
# Make sure we have all the system packages we need to proceed with the installation
check_sys_depend() {
local valid=true
local packages=("git" "composer" "mysql")
for package in "${packages[@]}" ; do
if ! dpkg -l | grep -qw "ii ${package}" ; then
echo_err "Package '${package}' is not installed."
valid=false
fi
done
# Bail out if any required package is missing
if [ "$valid" = false ] ; then
exit 1
fi
}
install_reflect() {
if ! [ -d ".git" ] ; then
echo_err "Installation aborted: '${cwd}' is not a git repository"
exit 1
fi
# Download and install the Reflect repository
if [[ "$install" != "n" ]]; then
if ! [ -d "reflect" ] ; then
git submodule add https://codeberg.org/reflect/reflect
fi
# Update submodules
git submodule update --init --recursive
fi
# Install dependencies with composer
(cd reflect && composer install --classmap-authoritative)
# Bail out if composer didn't create a vendor folder
if ! [ -d "reflect/vendor" ] ; then
echo_err "Something went wrong with the installation."
# Script was not run with the install flag disabled, this is probably a bug
if [[ "$install" != "n" ]]; then
exit_report
fi
echo_err "Make sure you have cloned the Reflect repository. Or run without '--install=n'."
exit 1
fi
}
configure_reflect() {
if ! [ -d "reflect" ] ; then
echo_err "Reflect is not installed."
exit_report
fi
# A configuration file already exists
if [[ -f "reflect/.env.ini" ]] ; then
if ! [[ -n "$overwrite" ]]; then
echo "A Reflect configuration file already exists at: ${cwd}/reflect/.env.ini"
read -p "Do you want to overwrite this file? (y/n): " overwrite </dev/tty
fi
# Remove existing configuration file or abort
if [[ "$overwrite" == "y" || "$overwrite" == "Y" ]] ; then
echo "Removing existing Reflect configuration and proceeding with the installation in ${cwd}..."
rm reflect/.env.ini
else
echo_err "Installation aborted: Configuration file already exists"
exit 1
fi
fi
echo "Configuring Reflect database."
# Database hostname
if ! [[ -n "$host" ]]; then
echo
read -p "Enter the full hostname of your MySQL/MariaDB server: " host </dev/tty
fi
# Database user
if ! [[ -n "$user" ]]; then
echo
read -p "Enter the username for your MySQL/MariaDB server: " user </dev/tty
fi
# Database password
if ! [[ -n "$password" ]]; then
echo
read -p "Enter the password for your MySQL/MariaDB server: " password </dev/tty
fi
# Database table
if ! [[ -n "$db" ]]; then
echo
read -p "Enter the name of the database table that Reflect should use: " db </dev/tty
fi
# Prompt the user for seeding the database if argument not provided
if ! [[ -n "$seed" ]]; then
echo
echo "Do you want to seed the database '$db'?"
echo "The database '$db' must EXIST and be EMPTY before proceeding with 'y'."
read -p "Do you want to proceed? (y/n): " seed </dev/tty
fi
# Seed the database
if [[ "$seed" == "y" || "$seed" == "Y" ]] ; then
# Database seed host
if ! [[ -n "$seed_host" ]]; then
echo
echo "Enter the full hostname of your MySQL/MariaDB server to use in this script for seeding."
read -p "Press enter to use the same host as the Reflect configuration [$host]: " seed_host </dev/tty
if [[ "$seed_host" == "" ]] ; then
seed_host=$host
fi
fi
# Database seed user
if ! [[ -n "$seed_user" ]]; then
echo
echo "Enter the username for your MySQL/MariaDB server to use in this script for seeding."
read -p "Press enter to use the same username as the Reflect configuration [$user]: " seed_user </dev/tty
if [[ "$seed_user" == "" ]] ; then
seed_user=$user
fi
fi
# Database seed password
if ! [[ -n "$seed_password" ]]; then
echo
echo "Enter the password for your MySQL/MariaDB server to use in this script for seeding."
echo "Enter 'null' to disable password authentication."
read -p "Press enter to use the same password as the Reflect configuration [<database_password>]: " seed_password </dev/tty
# Coerce password keyword "null" to empty string
if [[ "$seed_password" == "null" ]]; then
seed_password=""
fi
if [[ "$seed_password" == "" ]] ; then
seed_password=$password
fi
fi
echo
mysql -h "$seed_host" -u "$seed_user" --password="$seed_password" "$db" < reflect/src/database/seed/reflect.sql
if [ $? -ne 0 ]; then
echo_err "Installation aborted: MySQL error"
exit 1
fi
fi
# Coerce password keyword "null" to empty string
if [[ "$password" == "null" ]]; then
password=""
fi
local config=(
"; This config file was generated automatically by https://codeberg.org/reflect/install"
"; Refer to '.env.example.ini' or https://reflect.vlw.se/docs/Reference/Env for more information"
"root_path = '${dir}'"
"endpoints_dir = '${endpoints:="api"}'"
"mysql_host = '${host}'"
"mysql_user = '${user}'"
"mysql_pass = '${password}'"
"mysql_db = '${db}'"
)
for line in "${config[@]}" ; do
echo "${line}" >> reflect/.env.ini
done
}
main() {
# Get the current working directory
cwd=$(pwd)
check_sys_depend
install_reflect
configure_reflect
echo
echo "Reflect has been sucessfully installed."
echo "Thank you for choosing Reflect :)"
echo
exit 0
}
# Loop through all arguments
for arg in "$@"; do
case $arg in
--db=*)
db="${arg#*=}"
;;
--dir=*)
dir="${arg#*=}"
;;
--host=*)
host="${arg#*=}"
;;
--user=*)
user="${arg#*=}"
;;
--seed=*)
seed="${arg#*=}"
;;
--install=*)
install="${arg#*=}"
;;
--seed-host=*)
seed_host="${arg#*=}"
;;
--seed-user=*)
seed_user="${arg#*=}"
;;
--password=*)
password="${arg#*=}"
;;
--overwrite=*)
overwrite="${arg#*=}"
;;
--endpoints=*)
endpoints="${arg#*=}"
;;
--seed-password=*)
seed_password="${arg#*=}"
;;
*)
echo_err "Unknown argument: $arg"
;;
esac
done
# Start installation in passed directory
if [[ -n "$dir" ]]; then
if ! [ -d "$dir" ]; then
echo_err "Installation aborted: '$dir' is not a directory"
exit 1
fi
cd $dir
cwd=$(pwd)
main
fi
# Prompt the user for confirmation
echo
echo "You are currently in: $(pwd)"
read -p "Do you want to proceed with the installation in this directory? (y/n): " choice </dev/tty
echo
# Check the user's response
if [[ "$choice" == "y" || "$choice" == "Y" ]] ; then
echo "Proceeding with the installation in $(pwd)..."
main
else
echo "Installation aborted."
fi