Browse Source

POSIX is a... good thing that is difficult

main
zen 3 years ago
parent
commit
c16582fe63
  1. 2
      Makefile
  2. 54
      ingredients/lead
  3. 88
      recipes/alchemy.sh
  4. 2
      recipes/init.sh

2
Makefile

@ -29,7 +29,7 @@ preparations:
prosperity: prosperity:
@echo "This will install prestashop eventually" @echo "This will install prestashop eventually"
free: emancipation:
@echo "This will install freespace, once it exists" @echo "This will install freespace, once it exists"
flask: flask:

54
ingredients/lead

@ -59,11 +59,25 @@ RESET="\e[0m"
# We only want these to activate if we're running a recipe, so we # We only want these to activate if we're running a recipe, so we
# check to see if we're running a "shell inside of a shell" # check to see if we're running a "shell inside of a shell"
if [[ $SHLVL -gt 1 ]]; then if [ $SHLVL -gt 1 ]; then
# Make sure that ctrl+C consistently exits the script # Make sure that ctrl+C consistently exits the script
trap "exit" INT trap "exit" INT
# Give informative error messages when we receive ERR
trap 'say "${RED}Oops...${RESET} Something went wrong on line $LINENO of this script. Exit code was $?" >&2' ERR # Give informative error messages when we receive unguarded EXIT codes
set -e
handle_exit() {
case $? in
0)
exit
;;
*)
printf "${RED}Oops...${RESET} Something went wrong on line ${LINENO} of this script. Exit code was $?\n"
;;
esac
}
trap handle_exit EXIT
fi fi
# --------------- Functions --------------- # --------------- Functions ---------------
@ -119,16 +133,23 @@ source_env
# Checks to see if we can use a command or not # Checks to see if we can use a command or not
check_for() { check_for() {
command -v "$1" >/dev/null command -v "$1" > /dev/null
} }
if [ "$EUID" -eq 0 ]; then
echo -e "${RED}Woah there!${RESET} Seems you're running this script as a superuser." if [ "$(id -u)" -eq 0 ]; then
echo "" say "${RED}Woah there!${RESET} Seems you're running this script as a superuser."
echo "That might cause some issues with permissions and whatnot. Run this script as your default user (without sudo) and I'll ask you when I need superuser permissions" say ""
echo "" say "That might cause some issues with permissions and whatnot. Run this script as your default user (without sudo) and I'll ask you when I need superuser permissions"
say ""
exit 1 exit 1
fi fi
# I like using source as a keyword!
if ! check_for source; then
source() {
. ${1}
}
fi
# This one installs utilities to your OS (If you need them!) # This one installs utilities to your OS (If you need them!)
install_if_needed() { install_if_needed() {
@ -137,11 +158,11 @@ install_if_needed() {
case $DISTRO in case $DISTRO in
"debian") "debian")
# TODO Better installation detection than check_for # TODO Better installation detection than check_for
if [ -z $(check_for $package) ]; then if dpkg -l | grep -q "$package"; then
say "$package already installed!"
else
say "installing $package" say "installing $package"
sudo apt install -y $package sudo apt install -y $package
else
say "$package already installed!"
fi fi
;; ;;
"arch") "arch")
@ -195,7 +216,7 @@ forget() {
} }
remember() { remember() {
# Optionally choose to output somewhere other than Alchemy/ # Optionally choose to output somewhere other than Alchemy
if [ "${2}" = "to" ]; then if [ "${2}" = "to" ]; then
ENV_LOCATION=${3} ENV_LOCATION=${3}
else else
@ -205,20 +226,21 @@ remember() {
KEY=$(say ${1} | cut -d'=' -f 1) KEY=$(say ${1} | cut -d'=' -f 1)
VALUE=$(say ${1} | cut -d'=' -f 2) VALUE=$(say ${1} | cut -d'=' -f 2)
if [[ ! $KEY =~ ^[A-Z_]+$ ]]; then if say ${KEY} | grep -Eqv "^[A-Z_]+$"; then
say "Keys must consist only of capital letters and underscores" say "Keys must consist only of capital letters and underscores"
return 1
fi fi
VALID_CHARS='A-Za-z0-9/_.:=-' VALID_CHARS='A-Za-z0-9/_.:=-'
if [[ ! $VALUE =~ ^[$VALID_CHARS]+$ ]]; then if say ${VALUE} | grep -Eqv "^[${VALID_CHARS}]+$"; then
say "illegal VALUE: $VALUE" say "illegal VALUE: $VALUE"
say "for key $KEY" say "for key $KEY"
say "Valid characters for env values: letters, numbers, \".\",\"/\",\"_\"",\":\", \"-\" say "Valid characters for env values: letters, numbers, \".\",\"/\",\"_\"",\":\", \"-\"
fi fi
# If we're setting a valid key/value pair # If we're setting a valid key/value pair
if [[ ${1} =~ ^[A-Z_]+\=[$VALID_CHARS]+$ ]]; then if say ${1} | grep -Eq "^[A-Z_]+\=[$VALID_CHARS]+$"; then
DOTENV_ENTRY=$(cat $ENV_LOCATION | grep "^${KEY}" ) DOTENV_ENTRY=$(cat $ENV_LOCATION | grep "^${KEY}" )
# If something already exists and we're trying to set it to something new # If something already exists and we're trying to set it to something new

88
recipes/alchemy.sh

@ -1,4 +1,4 @@
#!/usr/bin/env sh #!/bin/sh
# This is the initialization script for Alchemy. # This is the initialization script for Alchemy.
# We can't rely on all systems being able to use 'make' etc. by default. # We can't rely on all systems being able to use 'make' etc. by default.
@ -6,76 +6,81 @@
# Bare Metal Alchemist, 2022 # Bare Metal Alchemist, 2022
# Just to let the system know what we're doing... # Just to let the system know what we're doing...
ALCHEMY="initializing" ALCHEMY=$(pwd)
# 'sourcing' a script is essentially running it to set up your system, # 'sourcing' a script is essentially running it to set up your system,
# making sure that some basic values / variables are defined # making sure that some basic values / variables are defined
source ingredients/lead . ingredients/lead
clear clear
echo -e "${BLUE}" say "${BLUE}"
echo -e " d8888 888 888 " say " d8888 888 888 "
echo -e " d88888 888 888 " say " d88888 888 888 "
echo -e " d88P888 888 888 " say " d88P888 888 888 "
echo -e " d88P 888 888 .d8888b 88888b. .d88b. 88888b.d88b. 888 888" say " d88P 888 888 .d8888b 88888b. .d88b. 88888b.d88b. 888 888"
echo -e " d88P 888 888 d88P' 888 '88b d8P Y8b 888 '888 '88b 888 888" say " d88P 888 888 d88P' 888 '88b d8P Y8b 888 '888 '88b 888 888"
echo -e " d88P 888 888 888 888 888 88888888 888 888 888 888 888" say " d88P 888 888 888 888 888 88888888 888 888 888 888 888"
echo -e " d8888888888 888 Y88b. 888 888 Y8b. 888 888 888 Y88b 888" say " d8888888888 888 Y88b. 888 888 Y8b. 888 888 888 Y88b 888"
echo -e "d88P 888 888 'Y8888P 888 888 'Y8888 888 888 888 'Y88888" say "d88P 888 888 'Y8888P 888 888 'Y8888 888 888 888 'Y88888"
echo -e " 888" say " 888"
echo -e " ${BOLD}Initialization Script -- BMA${BLUE} Y8b d88P" say " ${BOLD}Initialization Script -- BMA${BLUE} Y8b d88P"
echo -e "${RESET}" say "${RESET}"
echo -e "${GREEN}${ULINE}Environment${RESET}" say "${GREEN}${ULINE}Environment${RESET}"
if [ -f .env ]; then if [ -f .env ]; then
grep -v '^#' .env grep -v '^#' .env
export $(grep -v '^#' .env | xargs) export $(grep -v '^#' .env | xargs)
else else
echo "No .env file found, let's initialize it" say "No .env file found, let's initialize it"
remember "ALCHEMY=$(pwd)" touch "${ALCHEMY}/.env"
remember "ALCHEMY=${ALCHEMY}"
fi fi
echo "" say ""
echo -e "${GREEN}${ULINE}System Basics${RESET}" say "${GREEN}${ULINE}System Basics${RESET}"
if [[ $ISA && $DISTRO && $UPDATED ]]; then if [ -n "$ISA" ]; then
echo "Nothing to do!" if [ -n "$DISTRO" ]; then
if [ -n "$UPDATED" ]; then
say "Nothing to do!"
fi
fi
fi fi
if [[ ! $ISA ]]; then if [ -z $ISA ]; then
ISA=$(uname -m) ISA=$(uname -m)
if [ $ISA == 'x86_64' ]; then if [ $ISA = 'x86_64' ]; then
echo -e "Ayyy you got yourself an ${GREEN}x86${RESET} processor, cool" say "Ayyy you got yourself an ${GREEN}x86${RESET} processor, cool"
elif [ $ISA == 'armv7l' ]; then elif [ $ISA = 'armv7l' ]; then
echo -e "I see you rockin an ${GREEN}ARM${RESET} processor, neato" say "I see you rockin an ${GREEN}ARM${RESET} processor, neato"
fi fi
remember "ISA=$ISA" remember "ISA=$ISA"
fi fi
if [[ ! $DISTRO ]]; then if [ -z $DISTRO ]; then
if [ -f "/etc/debian_version" ]; then if [ -f "/etc/debian_version" ]; then
DISTRO="debian" DISTRO="debian"
echo -e "${GREEN}Debian${RESET}, Ubuntu, or Raspbian OS detected." say "${GREEN}Debian${RESET}, Ubuntu, or Raspbian OS detected."
elif [ -f "/etc/arch-release" ]; then elif [ -f "/etc/arch-release" ]; then
DISTRO="arch" DISTRO="arch"
echo -e "${GREEN}Arch or Manjaro-based${RESET} OS detected." say "${GREEN}Arch or Manjaro-based${RESET} OS detected."
elif [ -f "/etc/fedora-release" ]; then elif [ -f "/etc/fedora-release" ]; then
DISTRO="fedora" DISTRO="fedora"
echo -e "${GREEN}Fedora${RESET} detected as the Operating System" say "${GREEN}Fedora${RESET} detected as the Operating System"
elif [ $(uname | grep -c "Darwin") -eq 1 ]; then elif [ $(uname | grep -c "Darwin") -eq 1 ]; then
DISTRO="mac" DISTRO="mac"
echo -e "${GREEN}MacOS${RESET} detected." say "${GREEN}MacOS${RESET} detected."
else else
echo -e "I don't know ${RED}what OS you're running${RESET}! Cancelling this operation." say "I don't know ${RED}what OS you're running${RESET}! Cancelling this operation."
exit 1 exit 1
fi fi
remember "DISTRO=$DISTRO" remember "DISTRO=$DISTRO"
fi fi
# TODO - Update intermittently (like if you haven't run it in a week? use date +%s and (($INT+$INT2)) # TODO - Update intermittently (like if you haven't run it in a week? use date +%s and (($INT+$INT2))
if [[ ! $UPDATED ]]; then if [ -z "$UPDATED" ]; then
echo "" say ""
echo "Updating the repositories..." say "Updating the repositories..."
echo -e "(you'll probably need to input ${BLUE}your 'sudo' password${RESET} here)" say "(you'll probably need to input ${BLUE}your 'sudo' password${RESET} here)"
case $DISTRO in case $DISTRO in
"debian") "debian")
sudo apt update sudo apt update
@ -99,9 +104,10 @@ if [[ ! $UPDATED ]]; then
remember "UPDATED=true" remember "UPDATED=true"
fi fi
echo "" say ""
echo -e "${GREEN}${ULINE}Core Dependencies${RESET}" say "${GREEN}${ULINE}Core Dependencies${RESET}"
install_if_needed git wget make install_if_needed git wget make
echo "" say ""
echo -e "${BOLD}You're good to go!${RESET} Go ${BLUE}make something cool${RESET} :)" say "${BOLD}You're good to go!${RESET} Go ${BLUE}make something cool${RESET} :)"
say ""

2
recipes/init.sh

@ -1,4 +1,4 @@
#!/bin/bash #!/bin/sh
# This is a script to be run on a fresh installation of Raspbian in order to make it suitable (to me) for CLI development # This is a script to be run on a fresh installation of Raspbian in order to make it suitable (to me) for CLI development
# ~ Zen, 2022 # ~ Zen, 2022

Loading…
Cancel
Save