Add auto-detection of OS and distro to scripts

This commit is contained in:
2025-10-06 00:45:59 -05:00
parent 05ca9cfb85
commit 0ad0691120
7 changed files with 56 additions and 64 deletions

View File

@@ -1,41 +1,47 @@
#!/bin/zsh
[[ -z $1 ]] && {
echo "OS must be passed as an arg, run \`./box_setup.sh --help\` for more info"
exit 1
}
[[ $1 = "--help" ]] && {
echo "usage: ./box_setup.sh <OS-name> [system-type]\n"
echo "OS-name options: arch, artix, debian, macos"
echo "system-type options: personal, studio-music, work-placeholder\n"
echo "examples:\n./box_setup.sh arch studio-music\n./box_setup.sh macos\n"
echo "\nusage: ./box_setup.sh [system-type]"
echo "\nsystem-type options: personal, studio-music, work-placeholder"
echo "\nexamples:\n ./box_setup.sh\n ./box_setup.sh studio-music\n"
exit 0
}
# set env vars for installs
install_cmd=''
update_pkg_manager_and_defs_cmd=''
update_pkgs_cmd=''
case $1 in
(arch | artix)
install_cmd="sudo pacman -S"
update_pkg_manager_and_defs_cmd='' # don't; update system instead?
update_pkgs_cmd='sudo pacman -Syu'
;;
(debian)
install_cmd="sudo apt install"
update_pkg_manager_and_defs_cmd='sudo apt update'
update_pkgs_cmd='sudo apt upgrade'
;;
(macos)
install_cmd="brew install"
update_pkg_manager_and_defs_cmd='brew update'
update_pkgs_cmd='brew upgrade'
;;
esac
# determine OS and, if linux, distro
[[ "$OSTYPE" = *"darwin"* ]] && setup_os="macos" || {
[[ "$OSTYPE" = *"linux"* ]] && setup_os="linux" && {
[[ -f /etc/os-release ]] && . /etc/os-release
setup_distro=$(echo "${NAME%% *}" | tr '[:upper:]' '[:lower:]')
[[ -z "$setup_distro" ]] && echo "OS: linux; distro not detected" && exit 1
}
}
[[ -z "$setup_os" ]] && echo "OS not detected" && exit 1
export BOX_SETUP_OS=$1
# set package manager commands for installs
[[ "$setup_os" = "macos" ]] && {
install_cmd="brew install"
update_pkg_manager_and_defs_cmd='brew update'
update_pkgs_cmd='brew upgrade'
} || {
[[ "$setup_os" = "linux" ]] && {
case $setup_distro in
(arch | artix)
install_cmd="sudo pacman -S"
update_pkg_manager_and_defs_cmd='' # don't; update system instead?
update_pkgs_cmd='sudo pacman -Syu'
;;
(debian)
install_cmd="sudo apt install"
update_pkg_manager_and_defs_cmd='sudo apt update'
update_pkgs_cmd='sudo apt upgrade'
;;
esac
}
}
# export vars for scripts
export BOX_SETUP_OS="$setup_os"
export BOX_SETUP_DISTRO="$setup_distro"
export BOX_SETUP_INSTALL_COMMAND="$install_cmd"
export BOX_SETUP_UPDATE_PKG_MANAGER_AND_DEFS_CMD="$update_pkg_manager_and_defs_cmd"
export BOX_SETUP_UPDATE_PKGS_CMD="$update_pkgs_cmd"
@@ -43,10 +49,10 @@ export BOX_SETUP_UPDATE_PKGS_CMD="$update_pkgs_cmd"
# make dirs and copy configs/dotfiles
source ./src_files/.config/zsh/.zshenv
./make_dirs.sh
./copy_dotfiles.sh $2
./copy_dotfiles.sh $1
# install programs
source $ZDOTDIR/.zshenv
source $ZDOTDIR/.zshrc
./install_programs.sh $2
./install_programs.sh $1