Last active
February 10, 2023 01:16
-
-
Save 007revad/8df1075b6a67168e1c2e9042de42885b to your computer and use it in GitHub Desktop.
Script to install my other scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# shellcheck disable=SC2016,SC2012 | |
Script=Script_Name | |
bin=bin | |
config=bin/config | |
# TODO | |
# What if the user does not have a home folder ??? | |
# Can't create bin and profile in home! | |
# Install in /usr/local/bin with chmod 755 ??? | |
Cyan='\e[0;36m' | |
Off=$'\e[0m' | |
# If shell is BusyBox and not bash then advise how to install bash on Asustor NAS | |
Shell=$(/proc/self/exe --version 2>/dev/null | grep "GNU bash" | cut -d "," -f1) | |
if [ "$Shell" != "GNU bash" ]; then | |
echo -e "\nYou need to install bash to be able to run this script.\n" | |
echo "1. Install Entware from App Central" | |
echo "2. Run the following commands in a shell:" | |
echo "opkg update && opkg upgrade" | |
echo -e "opkg install bash\n" | |
exit 1 | |
fi | |
if [[ -d "$HOME" ]]; then | |
echo -e "${Cyan}Do you want to install for:${Off}" | |
echo -e "${Cyan}1)${Off} All users" | |
echo -e "${Cyan}2)${Off} Just $(whoami)" | |
read -r answer | |
if [[ ${answer,,} == "1" ]]; then | |
path=/usr/local | |
perms=755 | |
else | |
path="$HOME" | |
perms=700 | |
fi | |
else | |
path=/usr/local | |
perms=755 | |
fi | |
echo -e "${Cyan}Remove .sh extension?${Off} [y/n]" | |
read -r answer | |
if [[ ${answer,,} == "y" ]]; then remove_ext=yes; fi | |
function makedir() { | |
if [[ -n "$1" ]] && [[ ! -d "$1" ]]; then | |
mkdir "$1" || { echo "Failed to create $1"; exit 1; } | |
echo "Created $1" | |
fi | |
return | |
} | |
makedir "$path/$bin" | |
# Is it okay to create a config folder inside the bin folder? | |
makedir "$path/$config" | |
# Should I only change the home/bin permissions if I created the bin folder? | |
if [[ -d "$path/$bin" ]]; then | |
# Check current permissions | |
chmod "$perms" "$path/$bin" ||\ | |
{ echo "Failed to set permissions on $path/$bin"; exit 1; } | |
fi | |
# Should I only change the config permissions if I created the config folder? | |
if [[ -d "$path/$config" ]]; then | |
# Check current permissions | |
chmod "$perms" "$path/$config" ||\ | |
{ echo "Failed to set permissions on $path/$config"; exit 1; } | |
fi | |
cd "$(dirname "$0")" || { echo "cd $(dirname "$0") failed!"; exit 1; } | |
# Add script's files to array | |
shopt -s lastpipe | |
filelist=() | |
find . -maxdepth 1 \( -iname "*.sh" -o -iname "*.config" -o -iname "*.txt" \) -print0 |\ | |
while IFS= read -r -d $'\0'; do filelist+=("$REPLY"); done; declare -p filelist >/dev/null | |
# Remove install.sh and upgrade.sh from the array | |
num="0" | |
while [[ $num -lt "${#filelist[@]}" ]]; do | |
if [[ ${filelist[num]} == "./install.sh" ]] ||\ | |
[[ ${filelist[num]} == "./upgrade.sh" ]]; then | |
unset "filelist[num]" | |
fi | |
num=$((num +1)) | |
done | |
# Contract array to remove indexes with no elements left after we unset elements | |
filelist=( "${filelist[@]}" ) | |
# Check filelist array isn't empty | |
if [[ ${#filelist[@]} -gt "0" ]]; then | |
echo "Installing $Script files:" | |
else | |
echo "No $Script files to copy!" | |
exit 1 | |
fi | |
# Copy script's files to bin and set permissions | |
num="0" | |
while [[ $num -lt "${#filelist[@]}" ]]; do | |
f="${filelist[num]}" | |
filename=$(basename "$f") | |
echo -e "${Cyan}$((num +1)))${Off} $filename" # debug | |
if [[ ${f##*.} == "sh" ]] && [[ $filename != "$(basename -- "$0")" ]]; then | |
if [[ $remove_ext == "yes" ]]; then | |
file="$path/$bin/${f%.*}" # remove .sh | |
else | |
file="$path/$bin/$f" | |
fi | |
if cp -pu "$f" "$file"; then | |
chmod "$perms" "$file" || echo "Failed to set permissions on $file" | |
else | |
echo "Failed to copy $file" | |
fi | |
fi | |
if [[ ${f##*.} == "txt" ]]; then | |
# Ask before overwriting .txt files in /bin/config | |
if [[ ! -f "$path/$config/$f" ]]; then | |
if cp -ipu "$f" "$path/$config/$f"; then | |
chmod "$perms" "$path/$config/$f" ||\ | |
echo "Failed to set permissions on $path/$config/$f" | |
else | |
echo "Failed to copy $f" | |
fi | |
fi | |
fi | |
if [[ ${f##*.} == "config" ]]; then | |
if [[ -f "$path/$config/$f" ]]; then | |
# Edit existing file to include new settings | |
# We copy existing non-default values to new file | |
# then overwrite existing file with edited new file | |
while read -r line; do | |
if [[ "$line" =~ ^[a-zA-Z0-9_]+=.* ]]; then | |
name=$(printf %s "$line" | cut -d'=' -f 1) | |
line="${line//\//\\/}" # Replace / with \/ in variable for sed | |
line="${line/ /\\ }" # Replace spaces with '\ ' in variable for sed | |
sed -i -e 's/^'"$name"'=.*/'"$line"'/g' "$f" | |
fi | |
done <"$path/$config/$f" | |
fi | |
# Copy .config files to /bin/config | |
if [[ ! -f "$path/$config/$f" ]]; then | |
if cp -pu "$f" "$path/$config/$f"; then | |
chmod "$perms" "$path/$config/$f" ||\ | |
echo "Failed to set permissions on $path/$config/$f" | |
else | |
echo "Failed to copy $f" | |
fi | |
fi | |
fi | |
num=$((num +1)) | |
done | |
# Add "export PATH=$PATH:$HOME/bin" if needed | |
if [[ $path == "$HOME" ]]; then | |
cd "$HOME" || { echo "cd $path failed!"; exit 1; } | |
# Find highest precedence "profile" file | |
if [[ -f ".bash_profile" ]]; then | |
profile=".bash_profile" | |
elif [[ -f ".bash_login" ]]; then | |
profile=".bash_login" | |
elif [[ -f ".profile" ]]; then | |
profile=".profile" | |
else | |
touch ".bash_profile" | |
profile=".bash_profile" | |
fi | |
# Check if "export PATH=$PATH:$HOME/bin" in profile | |
if [[ -f "$HOME/$profile" ]]; then | |
while read -r line; do | |
if [[ $line == 'export PATH=$PATH:$HOME/'$bin ]]; then | |
echo -e "\nPath already exists in $profile" | |
addpath=no | |
break | |
else | |
addpath=yes | |
fi | |
done <"$HOME/$profile" | |
if [[ ! -s "$HOME/$profile" ]]; then | |
# File is empty | |
addpath=yes | |
fi | |
else | |
echo -e "\n$HOME/$profile not found!" | |
fi | |
# Add "export PATH=$PATH:$HOME/bin" if needed | |
if [[ $addpath == "yes" ]]; then | |
echo 'export PATH=$PATH:$HOME/'$bin >> "$profile" | |
# Check we successfully added path | |
while read -r line; do | |
if [[ $line == 'export PATH=$PATH:$HOME/'$bin ]]; then | |
echo -e "\nAdded path to $profile" | |
break | |
fi | |
done <"$HOME/$profile" | |
fi | |
fi | |
echo -e "\nFinished installing. You can run the script with:" | |
if [[ $remove_ext == "yes" ]]; then | |
echo -e "${Cyan}sudo i $Script${Off}\n" | |
else | |
echo -e "${Cyan}sudo i $Script.sh${Off}\n" | |
fi | |
exit | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment