upd/setup.sh

169 lines
4.1 KiB
Bash
Raw Normal View History

2022-03-23 16:41:16 +01:00
#!/bin/bash
# file: setup.sh
2022-03-24 17:29:18 +01:00
# Folder: upd (from git clone)
# git clone https://git.a-lec.org/echolib/upd.git
2022-03-23 16:41:16 +01:00
# By echolib
# License: GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007
unset ${!setup_@}
#----------------------------------------------------------------------
# Configuration ; folders and files to create/chown/copy...
#----------------------------------------------------------------------
2022-03-24 17:57:45 +01:00
# Directories to check ONLY with mkdir -p
setup_dir_bin="usr/local/bin"
# main app name
setup_app_name="upd"
# bin uri
set_bin_uri="$setup_dir_bin/$setup_app_name"
2022-03-23 16:41:16 +01:00
# Directories to CHOWN -R
set_dir_R_CHO=(
2022-03-24 17:29:18 +01:00
var/lib/upd
etc/upd
2022-03-23 16:41:16 +01:00
)
2022-03-24 17:29:18 +01:00
# filename application to CHOWN + CHMOD 755
set_files_R_CHO=(
upd
)
2022-03-23 16:41:16 +01:00
2022-03-24 17:29:18 +01:00
#======================================================================
#----------------------
# Check commands exists
#----------------------
if__command() {
! [[ `command -v $1` ]] \
&& echo -e "! Missing $1 command" \
&& exit 1
}
if__command sudo
if__command rsync
2022-03-23 16:41:16 +01:00
#------------------------
# 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)
2022-03-24 17:29:18 +01:00
[[ -f "/$set_bin_uri" ]] \
&& echo "- Removing /$set_bin_uri" \
&& sudo rm -f "/$set_bin_uri"
2022-03-23 16:41:16 +01:00
for setup_folder in ${set_dir_R_CHO[@]}
do
2022-03-24 17:29:18 +01:00
[[ -d "/$setup_folder" ]] \
&& echo "- Removing /$setup_folder" \
&& sudo rm -rf "/$setup_folder"
2022-03-23 16:41:16 +01:00
done
echo "# upd is uninstalled. Thank you for testing."
;;
esac
#----------------------------------------------------------------------
# Installation Process
#----------------------------------------------------------------------
setup__install() {
2022-03-24 17:29:18 +01:00
setup__gen_process() {
echo "- Folder: /$1"
2022-03-23 16:41:16 +01:00
echo -ne " Created:"
2022-03-24 17:29:18 +01:00
sudo mkdir -p "/$1" \
2022-03-23 16:41:16 +01:00
&& cmd_res="$?" \
&& setup__check_err
2022-03-23 17:25:51 +01:00
2022-03-24 17:29:18 +01:00
case "$2" in
chdir)
echo -ne " Owner $setup_user:"
sudo chown -R "$setup_user":"$setup_user" "/$1" \
&& cmd_res="$?" \
&& setup__check_err
;;
esac
2022-03-23 17:46:53 +01:00
echo -ne " Copy files:"
2022-03-24 17:29:18 +01:00
sudo rsync -a "$1/" "/$1/" \
2022-03-23 17:06:34 +01:00
&& cmd_res="$?" \
&& setup__check_err
2022-03-24 17:29:18 +01:00
}
for setup_folder in ${set_dir_R_CHO[@]}
do
setup__gen_process "$setup_folder" chdir
done
setup__gen_process "$setup_dir_bin"
for setup_file in ${set_files_R_CHO[@]}
do
echo -ne " Owner $setup_user for $setup_file:"
sudo chown "$setup_user":"$setup_user" "/$setup_dir_bin/$setup_file" \
&& cmd_res="$?" \
&& setup__check_err
sudo chmod 755 "/$setup_dir_bin/$setup_file"
2022-03-23 16:41:16 +01:00
done
2022-03-23 17:25:51 +01:00
2022-03-24 11:40:21 +01:00
printf '\n%b\n%b\n%b\n' \
"# Thank you for installing upd." \
" (You can remove the repo and use upd -U to update)" \
" Have fun !"
2022-03-23 16:41:16 +01:00
}
2022-03-24 17:29:18 +01:00
#----------------------------------------------------------------------
# MAIN PROCESS
#----------------------------------------------------------------------
2022-03-23 16:41:16 +01:00
# PWD is Not a repo
! [[ -d ".git" ]] \
&& echo "! No repository here" \
&& unset ${!setup_@} \
&& exit 0
# Updaate && Check Version
setup_ins_version=`
2022-03-24 17:57:45 +01:00
awk 'NR==2 {print $3}' "/$set_bin_uri" 2>/dev/null`
2022-03-23 16:41:16 +01:00
setup_new_version=`
2022-03-24 17:57:45 +01:00
awk 'NR==2 {print $3}' "$set_bin_uri" 2>/dev/null`
2022-03-23 16:41:16 +01:00
2022-03-23 16:48:46 +01:00
[[ "$setup_ins_version" == "$setup_new_version" ]] \
2022-03-24 17:57:45 +01:00
&& echo "$setup_app_name is already up to date" \
2022-03-23 16:41:16 +01:00
&& unset ${!setup_@} \
&& exit 0
# Directory project name (First folder from git clone)
2022-03-24 17:29:18 +01:00
read -rp ": Set $setup_app_name for user $USER (Y|n|[set])? " setup_user
2022-03-23 16:41:16 +01:00
case "$setup_user" in
""|Y|y|O|o)
setup_user="$USER"
;;
2022-03-24 17:29:18 +01:00
N|n)
unset ${!setup_@}
exit 1
;;
2022-03-23 16:41:16 +01:00
*)
! [[ `awk -F: '{print $1}' /etc/passwd | grep "$setup_user"` ]] \
&& echo "! No registred user: $setup_user" \
&& unset ${!setup_@} \
&& exit 1
;;
esac
echo
2022-03-24 17:29:18 +01:00
echo "# $setup_app_name Version: $setup_ins_version > $setup_new_version"
2022-03-23 16:41:16 +01:00
read -rp "# Ready to install (y|N)? " setup_confirm
case "$setup_confirm" in
Y|y|O|o) setup__install ;;
*) unset ${!setup_@};exit 0 ;;
esac