#!/usr/bin/env bash # set -e set -u # FUNCTIONS create_app_folder() { # create at home dir cd $homedir if [[ ! -d "Applications" ]]; then sudo -u $SUDO_USER mkdir Applications fi # go back to install folder cd $dir } zsh_prompt() { if [[ $usezsh == "" ]]; then # selection menu for zsh printf %s "$YELLOW" zshOption="" echo "Do you want Oh-My-ZSH as terminal extension?" echo "To use 'Oh-My-ZSH' enter 'y' to to continue in bash enter 'n'" read zshOption # check for valid input while [ ${zshOption,,} != "y" ] && [ ${zshOption,,} != "n" ]; do echo "Invalid input. Please press 'y' or 'n'" read zshOption done printf %s "$RESET" elif [[ $usezsh == "bash" ]]; then zshOption="n" elif [[ $usezsh == "zsh" ]]; then zshOption="y" fi # run option if [[ ${zshOption,,} = "n" ]]; then return elif [[ ${zshOption,,} = "y" ]]; then sh ../applications/Oh-MY-ZSH.sh fi } get_files() { # Create temp place where files will be stored cd src/applications > ../temp/installations.txt # File info applications="" counter=0 # Write every file name into installations for file in *; do # Every .sh file in application folder will be noted in installations.txt if [[ $file == *.sh ]]; then counter=$(( $counter + 1 )) echo "[$counter] ${file%.*}" >> ../temp/installations.txt fi done } select_files() { cd ../temp if $downloadall; then # remove all numbers from items sed -i 's/[^ ]* //' installations.txt clear return fi printf %s "$YELLOW" # Show all the options echo "The following items will be installed." cat installations.txt printf %s "$RESET" # Remove installations from file echo while read -p "${YELLOW}Select the number of the item you don't want to install (select '0' to continue): " input; do # Restart loop if wrong character is used if [[ -n ${input//[0-9]/} ]]; then echo "Please enter a valid number!" continue fi # Break the loop on input if [[ $input == "0" || $input == "" ]]; then clear break else # If pattern exist remove line if grep -wq "$input" "installations.txt"; then # get the full text a="$(grep -F [$input] installations.txt)" # reverse and remove full text grep -Fv "$a" installations.txt > filename2; mv filename2 installations.txt else echo "Please try again" fi clear print_logo printf %s "$YELLOW" echo "The following items will be installed." cat installations.txt echo printf %s "$RESET" fi done printf %s "$RESET" # remove all numbers from items sed -i 's/[^ ]* //' installations.txt } install_files() { # Check if debug is activated if $debug; then while read line; do #Reading each line and execute file if ! [ -x "$(command -v $line)" ]; then sh ../applications/$line.sh else echo "$line is already installed" fi done < "installations.txt" else # set standard info source ./../services/loadbar.sh start=1 end=100 amount="$(wc -l < installations.txt)" amount=$(($end / $amount)) # set design print_logo printf %s "$YELLOW" echo "The following items are going to be installed:" cat installations.txt echo "" printf %s "$RESET" while read line; do printf %s "$YELLOW" ProgressBar ${start} ${end} ${line} #Reading each line and execute file if ! [ -x "$(command -v $line)" ]; then sh ../applications/$line.sh >> /dev/null 2>&1 fi # update progressbar reach=$(( $start + $amount )) for number in $(seq ${start} ${reach}); do ProgressBar ${number} ${end} ${line} sleep 0.02 done printf %s "$RESET" start=$reach done < "installations.txt" # Completing if [[ $start != 100 ]]; then for number in $(seq ${start} 100); do ProgressBar ${number} ${end} "..." sleep 0.02 done fi fi } check_arguments() { case $1 in -h|--help) # display help print_logo print_help exit 1 ;; -v|--version) # display help print_version exit 1 ;; esac } check_sudo_arguments() { check_arguments $@ while getopts ":abdz" option; do case $option in d) # enable debug debug=true ;; a) # enable all downloadall=true ;; z) # enable zsh usezsh="zsh" ;; b) # enable bash usezsh="bash" ;; *) # incorrect option echo "Wrong argument given, please use '-h' to see what arguments are available." exit 1 ;; esac done main } main() { # check if the computer has a stable connections source ./src/services/connection.sh check_connection # Create applications folder create_app_folder # Print the logo clear print_logo # ZSH prompt zsh_prompt # Print the logo clear print_logo # create file with all items get_files # sort the file with the items select_files # install the selected files install_files # remove all files in temp cd ~ cd $dir cd src rm temp/* } # PROGRAM BODY # Safe important paths dir="$(pwd)" # Make sure the code is runned in the right way if [[ `basename "$dir"` != "BashInstaller" ]]; then echo "Please run this script in the Bashinstaller folder." exit 1 fi # add help.sh source ./src/services/help.sh setup_color if [[ $EUID != 0 ]]; then check_arguments $@ echo "Please run this script with sudo. Run it like this:" echo "sudo ./install.sh" exit 1 fi # Safe important variables homedir="$(getent passwd $SUDO_USER | cut -d: -f6)" debug=false downloadall=false usezsh="" # run the program if [ $# -eq 0 ]; then main else check_sudo_arguments $@ fi