vcarbs

Cem's Auto Rice Bootstrapping Scripts, for Void Linux
git clone git://git.ckyln.com/vcarbs.git
Log | Files | Refs | README | LICENSE

carbs (7826B)


      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/vcarbs"                  # Default CARBS Repo Location
     21 DEFPROGS="https://git.ckyln.com/raw/vcarbs/progs.csv"  # Default PROGS.CSV link
     22 DEFDOTFILES="git://git.ckyln.com/vdotfiles"            # Default Dotfiles Repo Location
     23 
     24 # shellcheck disable=SC2048
     25 
     26 # Messages below are taken from Arch scripts
     27 out() { printf "$1\\n"; }
     28 error() { out "==> ERROR: $@"; } >&2
     29 warning() { out "==> WARNING: $@"; } >&2
     30 msg() { out "==> $@"; }
     31 msg2() { out "  -> $@";}
     32 die() { error "$@"; exit 1; }
     33 fallback() { error "$@"; return 1; }
     34 
     35 [ "$(id -u)" = "0" ] && die 'Please run this script without root priviliges'
     36 
     37 usage() {
     38 	cat <<EOF
     39 usage: ${0##*/} [options]
     40 
     41   Options:
     42     -p <progs.csv>    Use an alternative progs.csv file (must be local)
     43     -r <repo url>     Use an alternative dotfiles repo
     44     -d <location>     Specify another place to install dotfiles
     45     -n		      Run the script noninteractively
     46     -P <filename>     Download progs.csv and exit
     47     -k <X11-Keymap>   Set X11 Keymap
     48     -s <location>     Use an alternative post-install script directory
     49 
     50     -h/--help         Print this help message
     51 
     52 CARBS installs a functional desktop environment.
     53 
     54 EOF
     55 }
     56 
     57 if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
     58 	usage
     59 	exit
     60 fi
     61 
     62 while getopts ':p::d::r:n:P::k::s:' flag; do
     63 	case $flag in
     64 		P) curl -Lo "$OPTARG" "$DEFPROGS" && exit || die 'Could not download progs.csv' ;;
     65 		p) [ -e "$OPTARG" ] && progs="$(realpath "$OPTARG")" || die "Could not find progs.csv file" ;;
     66 		r) git ls-remote "$OPTARG" >/dev/null 2>&1 && dotfiles=$OPTARG || die "Could not find repo" ;;
     67 		d) [ -d "$OPTARG" ] && targetdir="$(realpath "$OPTARG")" || die "Target location $OPTARG does not exist or is not a directory" ;;
     68 		n) sudo -V >/dev/null 2>&1 && interactive=0 || die "Sudo must be installed in order to have a non interactive installation" ;;
     69 		k) XK=$OPTARG ;;
     70 		s) [ -d "$OPTARG" ] && PIDIR="$(realpath "$OPTARG")" || die "Could not find post-install directory" ;;
     71 		:) die "${0##*/}: option requires an argument -- $OPTARG" ;;
     72 		?) die "${0##*/}: invalid option -- $OPTARG" ;;
     73 	esac
     74 done
     75 
     76 [ -z "$dotfiles" ] && dotfiles="$DEFDOTFILES"
     77 [ -z "$targetdir" ] && targetdir="$HOME"
     78 [ -z "$progs" ] && curl -Lo /tmp/progs.csv "$DEFPROGS" && progs="/tmp/progs.csv"
     79 [ -z "$interactive" ] && interactive=1
     80 [ "$interactive" = "0" ] && [ -z "$XK" ] && XK="us"
     81 
     82 welcomepage() {
     83 	clear
     84 	cat <<EOF
     85 ================================================== 
     86 ===     ======  =====       ===      =====      == 
     87 ==  ===  ====    ====  ====  ==  ===  ===  ====  = 
     88 =  =========  ==  ===  ====  ==  ====  ==  ====  = 
     89 =  ========  ====  ==  ===   ==  ===  ====  ====== 
     90 =  ========  ====  ==      ====      =======  ==== 
     91 =  ========        ==  ====  ==  ===  ========  == 
     92 =  ========  ====  ==  ====  ==  ====  ==  ====  = 
     93 ==  ===  ==  ====  ==  ====  ==  ===  ===  ====  = 
     94 ===     ===  ====  ==  ====  ==      =====      == 
     95 ================================================== 
     96 
     97 Welcome to Cem's Auto-Rice Bootstrapping Script!
     98 
     99 Please make sure that this is a brand new installation
    100 of Void Linux.
    101 
    102 WARNING: Carbs will alter your home directory ($HOME), so you might
    103 want to backup in case you have important files on your home directory
    104 EOF
    105 printf "Proceed? (y/N) "
    106 read ans
    107 [ "$ans" = "y" ] || die 'User exited'
    108 }
    109 
    110 makeinstall() {
    111 	cd /tmp || fallback "Could not change directory to /tmp"
    112 	dir="$(mktemp -d)"
    113 	git clone --depth 1 "$1" "$dir" || die 'Could not clone git repo'
    114 	cd "$dir" || fallback "Could not change directory to $dir"
    115 	make 2>/dev/null
    116 	sudo make install || die "Could not build or install $1"
    117 }
    118 
    119 makelist() {
    120 	printf "Distribution Programs\\n=====================\\n\\n"
    121 	grep '^,' $progs | cut -d , -f 2
    122 	printf "\\nGit Programs\\n============\\n\\n"
    123 	grep '^g' $progs | cut -d , -f 2
    124 }
    125 
    126 installcsv() {\
    127 	[ -e $progs ] || die 'Could not retrieve progs.csv'
    128 	mkdir -p "$HOME/.local/share/carbs" && makelist > "$HOME/.local/share/carbs/packages"
    129 	while IFS=, read -r src name
    130 	do
    131 		case $src in
    132 			g)
    133 				out "Installing $(basename "$name")"
    134 				makeinstall "$name" || die "Could not install $name"
    135 				msg "Installed $(basename "$name")" ;;
    136 			*)
    137 				out "Installing $name"
    138 				sudo xbps-install -y "$name" >/dev/null || die "Could not install $name"
    139 				msg "Installed $name" ;;
    140 		esac
    141 	done < $progs
    142 }
    143 
    144 
    145 sudouser() {\
    146 	warning "You don't seem to have sudo installed\nCarbs will now install sudo and add you\nto sudoers"
    147 	su -c "xbps-install -Su && xbps-install -y sudo && \
    148 	printf '$1 ALL=(ALL) NOPASSWD: ALL\nDefaults umask = 022\n' > /etc/sudoers.d/carbs" 
    149 	sudouser="1"
    150 	export sudouser
    151 }
    152 
    153 dotfilesinstall() {
    154 	cd /tmp || fallback "Could not change directory to /tmp"
    155 	dir="$(mktemp -d)"
    156 	git clone --recursive --depth 1 "$1" "$dir/repo"
    157 	cp -rfT "$dir/repo" "$targetdir"
    158 	rm "$targetdir/README"* "$targetdir/LICENSE" "$targetdir/.git" -rf "$targetdir/screenshots"
    159 }
    160 
    161 wallpaperinstall() {
    162 	out ' Installing wallpapers'
    163 	mkdir -p "$targetdir/Pictures"
    164 	git clone --depth 1 git://git.ckyln.com/Wallpapers "$targetdir/Pictures/Wallpapers" && msg 'Downloaded Wallpapers' || error 'Could not install wallpapers'
    165 }
    166 
    167 keyboardset() {
    168 	if [ -z $XK ]; then
    169 		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}')"
    170 		[ -z "$XK" ] && XK="us"
    171 	fi
    172 	printf "KEYMAP=$XK\\n" > "$HOME/.config/xkeymap"
    173 }
    174 
    175 serviceinit() {
    176 	for service in $*; do
    177 		out "Creating service for $service"
    178 		sudo ln -s "/etc/sv/$service" /var/service
    179 		sudo sv start "$service"
    180 	done
    181 }
    182 
    183 addusergroups() {
    184 	groups="wheel audio video"
    185 	for group in ${groups}; do
    186 		msg "Adding user to the $group group"
    187 		sudo usermod -a -G "$group" "$USER" || error "Could not add you to the $group group, you might want to do it yourself"
    188 	done
    189 }
    190 
    191 finalize() {
    192 	clear
    193 	cat <<EOF
    194 Carbs is finished, it is best to do a reboot
    195 
    196 EOF
    197 }
    198 
    199 postinstall() {
    200 	out "Starting post-installation"
    201 	if [ -z "$PIDIR" ]; then
    202 		cd /tmp || fallback "Could not change directory to /tmp"
    203 		DIR="$(mktemp -d)"
    204 		PIDIR="${DIR}/post-install"
    205 		git clone "$DEFCARBS" "$DIR" >/dev/null
    206 		out "Cloning CARBS repository for post-installation scripts"
    207 	fi
    208 	cd "$PIDIR" || fallback "Could not change directory to $PIDIR"
    209 	for script in *; do
    210 		case $script in
    211 			*.sh) ./"${script}" "$targetdir" ;;
    212 			README) continue ;;
    213 			template) continue ;;
    214 			*) warning "$script is not a shell script, not running" ;;
    215 		esac
    216 	done
    217 }
    218 
    219 
    220 umaskfix() {
    221 	out "Setting default umask to 022 and adding user to sudoers with NOPASSWD"
    222 	su -c "printf '$1 ALL=(ALL) NOPASSWD: ALL\nDefaults umask = 022\n' > /etc/sudoers.d/carbs" 
    223 }
    224 
    225 if [ $interactive = 1 ]; then
    226 	welcomepage
    227 	sudo -V >/dev/null 2>&1 || sudouser "$USER"
    228 fi
    229 [ -z "$sudouser" ] && umaskfix "$USER"
    230 sudo xbps-install -Syu && sudo xbps-install -Syu || die 'Could not refresh xbps database'
    231 sudo xbps-install -y git curl fzf
    232 installcsv
    233 dotfilesinstall $dotfiles
    234 wallpaperinstall
    235 postinstall
    236 keyboardset
    237 addusergroups
    238 serviceinit dbus
    239 finalize