carbs

Cem's Auto Rice Bootstrapping Scripts
git clone git://git.ckyln.com/carbs.git
Log | Files | Refs | README | LICENSE

carbs (8962B)


      1 #!/bin/sh
      2 
      3 ## CARBS, Cem's Auto-Rice Bootstrapping Script
      4 ## Copyright (C) 2019 Cem Keylan <cem@ckyln.com>
      5 
      6 ##    This program is free software: you can redistribute it and/or modify
      7 ##    it under the terms of the GNU General Public License as published by
      8 ##    the Free Software Foundation, either version 3 of the License, or
      9 ##    (at your option) any later version.
     10 ##
     11 ##    This program is distributed in the hope that it will be useful,
     12 ##    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 ##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 ##    GNU General Public License for more details.
     15 ##
     16 ##    You should have received a copy of the GNU General Public License
     17 ##    along with this program.  If not, see <https://www.gnu.org/licenses/>.
     18 
     19 # DEFAULT VARIABLES
     20 DEFCARBS="git://git.ckyln.com/carbs"                  # Default CARBS Repo Location
     21 DEFPROGS="https://git.ckyln.com/raw/carbs/progs.csv"  # Default PROGS.CSV link
     22 DEFDOTFILES="git://git.ckyln.com/dotfiles"            # Default Dotfiles Repo Location
     23 
     24 # Messages below are taken from Arch scripts
     25 out() { printf "$1\\n"; }
     26 error() { out "==> ERROR: $@"; } >&2
     27 warning() { out "==> WARNING: $@"; } >&2
     28 msg() { out "==> $@"; }
     29 msg2() { out "  -> $@";}
     30 die() { error "$@"; exit 1; }
     31 fallback() { error "$@"; return 1; }
     32 
     33 [ "$(id -u)" = "0" ] && die 'Please run this script without root priviliges'
     34 
     35 usage() {
     36 	cat <<EOF
     37 usage: ${0##*/} [options]
     38 
     39   Options:
     40     -p <progs.csv>    Use an alternative progs.csv file (must be local)
     41     -r <repo url>     Use an alternative dotfiles repo
     42     -d <location>     Specify another place to install dotfiles
     43     -n		      Run the script noninteractively
     44     -P <filename>     Download progs.csv and exit
     45     -k <X11-Keymap>   Set X11 Keymap
     46     -s <location>     Use an alternative post-install script directory
     47 
     48     -h/--help         Print this help message
     49 
     50 CARBS installs a functional desktop environment.
     51 
     52 EOF
     53 }
     54 
     55 if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
     56 	usage
     57 	exit
     58 fi
     59 
     60 while getopts ':p::d::r:n:P::k::s:' flag; do
     61 	case $flag in
     62 		P) curl -Lo "$OPTARG" "$DEFPROGS" && exit || die 'Could not download progs.csv' ;;
     63 		p) [ -e "$OPTARG" ] && progs="$(realpath "$OPTARG")" || die "Could not find progs.csv file" ;;
     64 		r) git ls-remote "$OPTARG" >/dev/null 2>&1 && dotfiles=$OPTARG || die "Could not find repo" ;;
     65 		d) [ -d "$OPTARG" ] && targetdir="$(realpath "$OPTARG")" || die "Target location $OPTARG does not exist or is not a directory" ;;
     66 		n) sudo -V >/dev/null 2>&1 && interactive=0 || die "Sudo must be installed in order to have a non interactive installation" ;;
     67 		k) XK=$OPTARG ;;
     68 		s) [ -d "$OPTARG" ] && PIDIR="$(realpath "$OPTARG")" || die "Could not find post-install directory" ;;
     69 		:) die "${0##*/}: option requires an argument -- $OPTARG" ;;
     70 		?) die "${0##*/}: invalid option -- $OPTARG" ;;
     71 	esac
     72 done
     73 
     74 [ -z "$dotfiles" ] && dotfiles="$DEFDOTFILES"
     75 [ -z "$targetdir" ] && targetdir="$HOME"
     76 [ -z "$progs" ] && curl -Lo /tmp/progs.csv "$DEFPROGS" && progs="/tmp/progs.csv"
     77 [ -z "$interactive" ] && interactive=1
     78 [ "$interactive" = "0" ] && [ -z "$XK" ] && XK="us"
     79 
     80 welcomepage() {
     81 	clear
     82 	cat <<EOF
     83 ================================================== 
     84 ===     ======  =====       ===      =====      == 
     85 ==  ===  ====    ====  ====  ==  ===  ===  ====  = 
     86 =  =========  ==  ===  ====  ==  ====  ==  ====  = 
     87 =  ========  ====  ==  ===   ==  ===  ====  ====== 
     88 =  ========  ====  ==      ====      =======  ==== 
     89 =  ========        ==  ====  ==  ===  ========  == 
     90 =  ========  ====  ==  ====  ==  ====  ==  ====  = 
     91 ==  ===  ==  ====  ==  ====  ==  ===  ===  ====  = 
     92 ===     ===  ====  ==  ====  ==      =====      == 
     93 ================================================== 
     94 
     95 Welcome to Cem's Auto-Rice Bootstrapping Script!
     96 
     97 Please make sure that this is a brand new installation
     98 of Arch Linux, Manjaro Linux, or Anarchy Linux.
     99 
    100 WARNING: Carbs will alter your home directory ($HOME), so you might
    101 want to backup in case you have important files on your home directory
    102 EOF
    103 printf "Proceed? (y/N) "
    104 read ans
    105 [ "$ans" = "y" ] || die 'User exited'
    106 }
    107 
    108 refreshsystem() {
    109 	out "Refreshing system"
    110 	sudo pacman -Sy || die "Could not refresh pacman db"
    111 	sudo pacman -Syu --noconfirm || die "Could not upgrade system"
    112 	msg "System up to date!"
    113 }
    114 
    115 makeinstall() {
    116 	cd /tmp || fallback "Could not change directory to /tmp"
    117 	dir="$(mktemp -d)"
    118 	git clone --depth 1 "$1" "$dir" || die 'Could not clone git repo'
    119 	cd "$dir" || fallback "Could not change directory to $dir"
    120 	make 2>/dev/null
    121 	sudo make install || die "Could not build or install $1"
    122 }
    123 
    124 makelist() {
    125 	printf "Distribution Programs\\n=====================\\n\\n"
    126 	grep '^,' $progs | cut -d , -f 2
    127 	printf "\\nGit Programs\\n============\\n\\n"
    128 	grep '^g' $progs | cut -d , -f 2
    129 	printf "\\nYay Programs\\n============\\n\\n"
    130 	grep '^y' $progs | cut -d , -f 2
    131 }
    132 
    133 installcsv() {\
    134 	[ -e $progs ] || die 'Could not retrieve progs.csv'
    135 	mkdir -p "$HOME/.local/share/carbs" && makelist > "$HOME/.local/share/carbs/packages"
    136 	while IFS=, read -r src name
    137 	do
    138 		case $src in
    139 			y)
    140 				out "Installing $name"
    141 				yay -S --noconfirm --needed "$name" >/dev/null || die "Could not install $name"
    142 				msg "Installed $name" ;;
    143 			g)
    144 				out "Installing $(basename "$name")"
    145 				makeinstall "$name" || die "Could not install $name"
    146 				msg "Installed $(basename "$name")" ;;
    147 			*)
    148 				out "Installing $name"
    149 				sudo pacman -S --noconfirm --needed "$name" >/dev/null || die "Could not install $name"
    150 				msg "Installed $name" ;;
    151 		esac
    152 	done < $progs
    153 }
    154 
    155 installyay() {\
    156 	error 'Could not install yay-bin, building from source'
    157 	cd /tmp || fallback "Could not change directory to /tmp"
    158 	dir=$(mktemp -d)
    159 	cd "$dir" || fallback "Could not change directory to $dir"
    160 	curl -Lo "$dir/PKGBUILD" "https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD?h=yay"
    161 	makepkg --noconfirm --ignore-arch -si || die 'Could not install yay'
    162 }
    163 
    164 basedevelopenrc () {
    165 	ORBDL="pacman autoconf automake binutils bison fakeroot file findutils flex gawk gcc \
    166 	gettext grep groff gzip libtool m4 make patch pkgconf sed sudo texinfo util-linux which"
    167 	for pkg in ${ORBDL}; do
    168 		out "Installing $pkg"
    169 		sudo pacman -S --noconfirm --needed "$pkg" >/dev/null || die "Could not install $pkg"
    170 		msg "Installed $pkg"
    171 	done
    172 }
    173 
    174 installyaybin() {\
    175 	if [ -d /etc/init.d ]; then
    176 		basedevelopenrc
    177 	else
    178 		sudo pacman -S --noconfirm --needed base-devel || die 'Could not install base-devel'
    179 	fi
    180 	cd /tmp || fallback "Could not change directory to /tmp"
    181 	dir=$(mktemp -d)
    182 	cd "$dir" || fallback "Could not change directory to $dir"
    183 	curl -Lo "$dir/PKGBUILD" "https://aur.archlinux.org/cgit/aur.git/plain/PKGBUILD?h=yay-bin"
    184 	makepkg --noconfirm -si || installyay
    185 }
    186 
    187 sudouser() {\
    188 	warning "You don't seem to have sudo installed\nCarbs will now install sudo and add you\nto sudoers"
    189 	su -c "pacman -Sy && pacman -S sudo --noconfirm && \
    190 	printf '$1 ALL=(ALL) NOPASSWD: ALL\n' > /etc/sudoers.d/carbs" 
    191 }
    192 
    193 dotfilesinstall() {
    194 	cd /tmp || fallback "Could not change directory to /tmp"
    195 	dir="$(mktemp -d)"
    196 	git clone --recursive --depth 1 "$1" "$dir/repo"
    197 	cp -rfT "$dir/repo" "$targetdir"
    198 	rm "$targetdir/README"* "$targetdir/LICENSE" "$targetdir/.git" -rf "$targetdir/screenshots"
    199 }
    200 
    201 wallpaperinstall() {
    202 	out ' Installing wallpapers'
    203 	mkdir -p "$targetdir/Pictures"
    204 	git clone --depth 1 git://git.ckyln.com/Wallpapers "$targetdir/Pictures/Wallpapers" && msg 'Downloaded Wallpapers' || error 'Could not install wallpapers'
    205 }
    206 
    207 keyboardset() {
    208 	if [ -z $XK ]; then
    209 		XK="$(sed '/^! layout$/,/^ *$/!d;//d' /usr/share/X11/xkb/rules/base.lst | fzf --prompt "What X11 Keymap are you going to use? (default: us) " | awk '{print $1}')"
    210 		[ -z "$XK" ] && XK="us"
    211 	fi
    212 	printf "KEYMAP=$XK\\n" > "$HOME/.config/xkeymap"
    213 }
    214 
    215 addusergroups() {
    216 	groups="wheel audio video power"
    217 	for group in ${groups}; do
    218 		msg "Adding user to the $group group"
    219 		sudo usermod -a -G "$group" "$USER" || error "Could not add you to the $group group, you might want to do it yourself"
    220 	done
    221 }
    222 
    223 finalize() {
    224 	clear
    225 	cat <<EOF
    226 Carbs is finished, it is best to do a reboot
    227 
    228 EOF
    229 }
    230 
    231 postinstall() {
    232 	out "Starting post-installation"
    233 	if [ -z "$PIDIR" ]; then
    234 		cd /tmp || fallback "Could not change directory to /tmp"
    235 		DIR="$(mktemp -d)"
    236 		PIDIR="${DIR}/post-install"
    237 		git clone "$DEFCARBS" "$DIR" >/dev/null
    238 		out "Cloning CARBS repository for post-installation scripts"
    239 	fi
    240 	cd "$PIDIR" || fallback "Could not change directory to $PIDIR"
    241 	for script in *; do
    242 		case $script in
    243 			*.sh) ./"${script}" "$targetdir" ;;
    244 			README) continue ;;
    245 			template) continue ;;
    246 			*) warning "$script is not a shell script, not running" ;;
    247 		esac
    248 	done
    249 }
    250 
    251 
    252 
    253 if [ $interactive = 1 ]; then
    254 	welcomepage
    255 	sudo -V >/dev/null 2>&1 || sudouser "$USER"
    256 fi
    257 refreshsystem
    258 sudo pacman -S --noconfirm --needed fzf curl git
    259 yay -V >/dev/null 2>&1 || installyaybin
    260 installcsv
    261 dotfilesinstall $dotfiles
    262 wallpaperinstall
    263 postinstall
    264 keyboardset
    265 addusergroups
    266 finalize