installify/install.sh

263 lines
5.8 KiB
Bash
Executable File

#!/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() {
# selection menu for zsh
printf %s "$YELLOW"
deleteOption=""
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 deleteOption
# check for valid input
while [ ${deleteOption,,} != "y" ] && [ ${deleteOption,,} != "n" ]; do
echo "Invalid input. Please press 'y' or 'n'"
read deleteOption
done
printf %s "$RESET"
# run option
if [[ ${deleteOption,,} = "n" ]]; then
return
elif [[ ${deleteOption,,} = "y" ]]; then
echo "nice choice lets use zsh"
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() {
printf %s "$YELLOW"
# Show all the options
cd ../temp
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() {
if [[ $1 = "-h" || $1 = "-help" ]]; then
print_logo
print_help
exit 1
elif [[ $1 = "-v" || $1 = "-version" ]]; then
print_version
exit 1
fi
}
check_sudo_arguments() {
check_arguments $@
if [[ $1 = "-d" || $1 = "-debug" ]]; then
debug=true
main
fi
}
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
# run the program
if [ $# -eq 0 ]; then
main
else
check_sudo_arguments $@
fi