upd/setup.sh

169 lines
4.1 KiB
Bash

#!/bin/bash
# file: setup.sh
# Folder: upd (from git clone)
# git clone https://git.a-lec.org/echolib/upd.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 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"
# Directories to CHOWN -R
set_dir_R_CHO=(
var/lib/upd
etc/upd
)
# filename application to CHOWN + CHMOD 755
set_files_R_CHO=(
upd
)
#======================================================================
#----------------------
# Check commands exists
#----------------------
if__command() {
! [[ `command -v $1` ]] \
&& echo -e "! Missing $1 command" \
&& exit 1
}
if__command sudo
if__command rsync
#------------------------
# 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() {
setup__gen_process() {
echo "- Folder: /$1"
echo -ne " Created:"
sudo mkdir -p "/$1" \
&& cmd_res="$?" \
&& setup__check_err
case "$2" in
chdir)
echo -ne " Owner $setup_user:"
sudo chown -R "$setup_user":"$setup_user" "/$1" \
&& cmd_res="$?" \
&& setup__check_err
;;
esac
echo -ne " Copy files:"
sudo rsync -a "$1/" "/$1/" \
&& cmd_res="$?" \
&& setup__check_err
}
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"
done
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 !"
}
#----------------------------------------------------------------------
# MAIN PROCESS
#----------------------------------------------------------------------
# PWD is Not a repo
! [[ -d ".git" ]] \
&& echo "! No repository here" \
&& unset ${!setup_@} \
&& exit 0
# Updaate && Check Version
setup_ins_version=`
awk 'NR==2 {print $3}' "/$set_bin_uri" 2>/dev/null`
setup_new_version=`
awk 'NR==2 {print $3}' "$set_bin_uri" 2>/dev/null`
[[ "$setup_ins_version" == "$setup_new_version" ]] \
&& echo "$setup_app_name is already up to date" \
&& unset ${!setup_@} \
&& exit 0
# Directory project name (First folder from git clone)
read -rp ": Set $setup_app_name for user $USER (Y|n|[set])? " setup_user
case "$setup_user" in
""|Y|y|O|o)
setup_user="$USER"
;;
N|n)
unset ${!setup_@}
exit 1
;;
*)
! [[ `awk -F: '{print $1}' /etc/passwd | grep "$setup_user"` ]] \
&& echo "! No registred user: $setup_user" \
&& unset ${!setup_@} \
&& exit 1
;;
esac
echo
echo "# $setup_app_name 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