PROJECT_DIR=$(pwd) DB_PATH="$PROJECT_DIR/test.sqlite3" SQL_DIR="$PROJECT_DIR/sql" insert_user() { read -p "Enter a username: " username read -sp "Enter a password: " password hash=$(mkpasswd -m bcrypt "$password") if [ $? -ne 0 ]; then echo "Error: Failed to hash password." exit 1 fi uuid=$(uuidgen) sqlite3 $DB_PATH "INSERT INTO users (name, password, uuid) VALUES ('$username', '$hash', '$uuid');" if [ $? -ne 0 ]; then echo "Error: Failed to insert user into the database." exit 1 fi } sqlite3 $DB_PATH < "$SQL_DIR/tables.sql" if [ $? -ne 0 ]; then echo "Error: Failed to create tables in the database." exit 1 fi echo "Before continuing you must create users. The reset of the schema depends on them" while true; do insert_user echo -e "\nDo you want to add anoter user? (y/n):" read choice if [ "$choice" != "y" ]; then break fi done echo -e '\nUsers inserted, now setting up the rest of the database.' echo -e "\nAdding views:" sqlite3 $DB_PATH < "$SQL_DIR/views.sql" if [ $? -ne 0 ]; then echo "Error: Failed to add views." exit 1 fi echo -e "\nAdding triggers:" sqlite3 $DB_PATH < "$SQL_DIR/triggers.sql" if [ $? -ne 0 ]; then echo "Error: Failed to add triggers." exit 1 fi echo -e "\nSeeding the database:" sqlite3 $DB_PATH < "$SQL_DIR/seed.sql" if [ $? -ne 0 ]; then echo "Error: Failed to seed the database." exit 1 fi echo -e "\nThe database schema" sqlite3 $DB_PATH .schema if [ $? -ne 0 ]; then echo "Error: Failed to print the database schema." exit 1 fi echo -e "\n+------------------------------------+" echo -e "| Database initialization completed. |" echo -e "+------------------------------------+\n"