upd/setup.sh

145 lines
3.8 KiB
Bash

#!/bin/bash
# version: 0.1
# file: setup.sh
# Folder: filebox (from git clone)
# git clone https://git.a-lec.org/echolib/filebox.git
# By echolib
# License: GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007
unset ${!setup_@}
#----------------------------------------------------------------------
# Configuration ; folders and files to create/chown/copy...
#----------------------------------------------------------------------
# Directories to CHOWN -R
set_dir_R_CHO=(
/var/lib/upd
/etc/upd
)
# bin Directories: Check ONLY with mkdir -p
setup_dir_bin="/usr/local/bin"
# filename application to CHOWN
setup_file_bin="upd"
# bin uri
set_bin_uri="$setup_dir_bin/$setup_file_bin"
#------------------------
# Check commands function
#------------------------
setup__check_err() {
if (( cmd_res == 0 ));then
echo -ne " OK\n"
else
echo -ne " Error $cmd_res\n"
exit 1
fi
}
#----------------------------------------------------------------------
# Remove Process
#----------------------------------------------------------------------
case "$1" in
remove|-R)
[[ -f "$set_bin_uri" ]] \
&& echo "- Removing $set_bin_uri" \
&& sudo rm -f "$set_bin_uri"
for setup_folder in ${set_dir_R_CHO[@]}
do
[[ -d "$setup_folder" ]] \
&& echo "- Removing $setup_folder" \
&& sudo rm -rf "$setup_folder"
done
echo "# upd is uninstalled. Thank you for testing."
;;
esac
#----------------------------------------------------------------------
# Installation Process
#----------------------------------------------------------------------
setup__install() {
for setup_folder in ${set_dir_R_CHO[@]}
do
echo "- Folder: $setup_folder"
echo -ne " Created:"
sudo mkdir -p "$setup_folder" \
&& cmd_res="$?" \
&& setup__check_err
echo -ne " Owner $setup_user:"
sudo chown -R "$setup_user":"$setup_user" "$setup_folder" \
&& cmd_res="$?" \
&& setup__check_err
done
echo "- Folder: $setup_dir_bin"
echo -ne " Created:"
sudo mkdir -p "$setup_dir_bin" \
&& cmd_res="$?" \
&& setup__check_err
echo "- File: $setup_file_bin"
echo -ne " Copy:"
sudo cp "$PWD$set_bin_uri" \
"$set_bin_uri" \
&& cmd_res="$?" \
&& setup__check_err
echo -ne " Owner $setup_user:"
sudo chown "$setup_user":"$setup_user" "$set_bin_uri" \
&& cmd_res="$?" \
&& setup__check_err
sudo chmod 755 "$set_bin_uri"
echo -e "\n# Thank you for installing upd. Have fun !"
}
# PWD is Not a repo
! [[ -d ".git" ]] \
&& echo "! No repository here" \
&& unset ${!setup_@} \
&& exit 0
# Updaate && Check Version
git pull
setup_ins_version=`
awk 'NR==2 {print $3}' "$setup_dir_bin/$setup_file_bin" 2>/dev/null`
setup_new_version=`
awk 'NR==2 {print $3}' "$PWD$setup_dir_bin/$setup_file_bin"`
[[ "$setup_ins_version" == "$setup_ins_version" ]] \
&& echo "$setup_file_bin is already up to date" \
&& unset ${!setup_@} \
&& exit 0
# Directory project name (First folder from git clone)
setup_folder_git_name=`basename ${PWD}`
read -rp ": Set $setup_folder_git_name for user ($USER)? " setup_user
case "$setup_user" in
""|Y|y|O|o)
setup_user="$USER"
;;
*)
! [[ `awk -F: '{print $1}' /etc/passwd | grep "$setup_user"` ]] \
&& echo "! No registred user: $setup_user" \
&& unset ${!setup_@} \
&& exit 1
;;
esac
echo -e "\n# Installation Schema:"
for setup_folder in ${set_dir_R_CHO[@]}
do
printf '%s%s\n' \
"- Folder: $setup_folder " \
"; sudo mkdir -p + sudo chown -R $setup_user:$setup_user"
done
echo "- Folder: $setup_dir_bin ; sudo mkdir -p"
echo "- File: $set_bin_uri ; sudo cp + sudo chown $setup_user:$setup_user"
echo
echo "# Version: $setup_ins_version > $setup_new_version"
read -rp "# Ready to install (y|N)? " setup_confirm
case "$setup_confirm" in
Y|y|O|o) setup__install ;;
*) unset ${!setup_@};exit 0 ;;
esac