From de3ed9a061a421b528333b025c23adefef27199b Mon Sep 17 00:00:00 2001 From: Cyrille L Date: Thu, 24 Mar 2022 17:29:18 +0100 Subject: [PATCH] New setup ; updater ; more options --- README.md | 9 ++- etc/upd/upd.conf | 7 +- setup.sh | 129 ++++++++++++++++++++---------------- usr/local/bin/upd | 18 +++-- var/lib/upd/README.md | 9 ++- var/lib/upd/scripts/check | 1 - var/lib/upd/scripts/help | 13 ++-- var/lib/upd/scripts/tools | 2 +- var/lib/upd/scripts/updater | 41 +++++++----- var/lib/upd/setup.sh | 129 ++++++++++++++++++++---------------- 10 files changed, 209 insertions(+), 149 deletions(-) diff --git a/README.md b/README.md index 37f4130..df3a933 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,9 @@ version for debian and archlinux (separrating AUR and System available updates). More functions will be added soon... # Dependancies -- curl (to check available version and let upd -U update himself) +- bash +- curl (to check available version) +- rsync (to update files with upd -U) # How to install ``` @@ -22,3 +24,8 @@ cd upd chmod +x setup.sh ./setup.sh ``` + +# How to upgrade +``` +upd -U +``` diff --git a/etc/upd/upd.conf b/etc/upd/upd.conf index d3212ed..c7da906 100644 --- a/etc/upd/upd.conf +++ b/etc/upd/upd.conf @@ -1,10 +1,11 @@ #!/bin/bash -# version: 0.0.1 # file: upd.conf # Folder: /etc/upd # By echolib # License: GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007 +app_name="upd" + # local home upd_dir_home="$HOME/.config/upd" @@ -18,5 +19,5 @@ upd_script_help="/var/lib/upd/scripts/help" upd_script_updater="/var/lib/upd/scripts/updater" # Onlinde Repo -upd_repo_upd="https://git.a-lec.org/echolib/upd/-/raw/main/usr/local/bin/upd" -upd_repo_tar="https://git.a-lec.org/echolib/upd/-/archive/main/upd-main.tar.gz" +repo="https://git.a-lec.org/echolib/upd/-/raw/main/usr/local/bin/upd" +repo_tar="https://git.a-lec.org/echolib/upd/-/archive/main/upd-main.tar.gz" diff --git a/setup.sh b/setup.sh index baa47aa..172423e 100644 --- a/setup.sh +++ b/setup.sh @@ -1,8 +1,7 @@ #!/bin/bash -# version: 0.1 # file: setup.sh -# Folder: filebox (from git clone) -# git clone https://git.a-lec.org/echolib/filebox.git +# 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_@} @@ -12,19 +11,37 @@ unset ${!setup_@} #---------------------------------------------------------------------- # Directories to CHOWN -R set_dir_R_CHO=( -/var/lib/upd -/etc/upd +var/lib/upd +etc/upd ) -# bin Directories: Check ONLY with mkdir -p -setup_dir_bin="/usr/local/bin" +# Directories to check ONLY with mkdir -p +setup_dir_bin="usr/local/bin" -# filename application to CHOWN -setup_file_bin="upd" +# main app name +setup_app_name="upd" +# filename application to CHOWN + CHMOD 755 +set_files_R_CHO=( +upd +) # bin uri set_bin_uri="$setup_dir_bin/$setup_file_bin" +#====================================================================== + +#---------------------- +# Check commands exists +#---------------------- +if__command() { +! [[ `command -v $1` ]] \ + && echo -e "! Missing $1 command" \ + && exit 1 +} + +if__command sudo +if__command rsync + #------------------------ # Check commands function #------------------------ @@ -42,15 +59,15 @@ fi #---------------------------------------------------------------------- case "$1" in remove|-R) - [[ -f "$set_bin_uri" ]] \ - && echo "- Removing $set_bin_uri" \ - && sudo rm -f "$set_bin_uri" + [[ -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" + [[ -d "/$setup_folder" ]] \ + && echo "- Removing /$setup_folder" \ + && sudo rm -rf "/$setup_folder" done echo "# upd is uninstalled. Thank you for testing." ;; @@ -60,51 +77,54 @@ esac # Installation Process #---------------------------------------------------------------------- setup__install() { -for setup_folder in ${set_dir_R_CHO[@]} -do - echo "- Folder: $setup_folder" + + setup__gen_process() { + echo "- Folder: /$1" + echo -ne " Created:" - sudo mkdir -p "$setup_folder" \ + sudo mkdir -p "/$1" \ && cmd_res="$?" \ && setup__check_err - echo -ne " Owner $setup_user:" - sudo chown -R "$setup_user":"$setup_user" "$setup_folder" \ - && 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 "${PWD}$setup_folder/" "$setup_folder/" \ + 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 -echo "- Folder: $setup_dir_bin" -echo -ne " Created:" -sudo mkdir -p "$setup_dir_bin" \ - && cmd_res="$?" \ - && setup__check_err +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 -echo "- File: $setup_file_bin" -echo -ne " Copy $PWD$set_bin_uri + chmod 755:" -sudo cp -f "$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" - 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" \ @@ -113,9 +133,9 @@ printf '\n%b\n%b\n%b\n' \ # Updaate && Check Version setup_ins_version=` -awk 'NR==2 {print $3}' "$setup_dir_bin/$setup_file_bin" 2>/dev/null` +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"` +awk 'NR==2 {print $3}' "$setup_dir_bin/$setup_file_bin" 2>/dev/null` [[ "$setup_ins_version" == "$setup_new_version" ]] \ && echo "$setup_file_bin is already up to date" \ @@ -123,12 +143,15 @@ awk 'NR==2 {print $3}' "$PWD$setup_dir_bin/$setup_file_bin"` && 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 +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" \ @@ -137,18 +160,8 @@ case "$setup_user" in ;; 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" +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 ;; diff --git a/usr/local/bin/upd b/usr/local/bin/upd index 59cda2f..2b2a0e2 100755 --- a/usr/local/bin/upd +++ b/usr/local/bin/upd @@ -1,5 +1,5 @@ #!/bin/bash -# version: 0.0.9 +# version: 0.0.10 # file: upd # Folder: /usr/local/bin # By echolib @@ -27,6 +27,10 @@ case "$2" in ! [[ -f "$1" ]] \ && touch "$1" ;; + read) + clear + cat "$1" + ;; esac } @@ -105,14 +109,20 @@ case "$1" in ;; -vv) - awk 'NR==2 {print "# Installed:",$3}' "/usr/local/bin/upd" + v_cur=`awk 'NR==2 {print $3}' "/usr/local/bin/$app_name"` + echo -n "# $app_name Current: $v_cur | " if__command curl - curl -s "$upd_repo_upd" | awk 'NR==2 {print "# Available:",$3}' + v_onl=`curl -s "$repo" | awk 'NR==2 {print $3}'` + echo -ne "Online: $v_onl\n" ;; -U) if__file "$upd_script_updater" source - upd__Updater + Updater + ;; + + --readme) + if__file "/var/lib/upd/README.md" read ;; help|--help|-h|*) diff --git a/var/lib/upd/README.md b/var/lib/upd/README.md index 37f4130..df3a933 100644 --- a/var/lib/upd/README.md +++ b/var/lib/upd/README.md @@ -13,7 +13,9 @@ version for debian and archlinux (separrating AUR and System available updates). More functions will be added soon... # Dependancies -- curl (to check available version and let upd -U update himself) +- bash +- curl (to check available version) +- rsync (to update files with upd -U) # How to install ``` @@ -22,3 +24,8 @@ cd upd chmod +x setup.sh ./setup.sh ``` + +# How to upgrade +``` +upd -U +``` diff --git a/var/lib/upd/scripts/check b/var/lib/upd/scripts/check index ecfe859..0f03434 100644 --- a/var/lib/upd/scripts/check +++ b/var/lib/upd/scripts/check @@ -1,5 +1,4 @@ #!/bin/bash -# version: 0.0.1 # file: check # Folder: /var/lib/upd/scripts # By echolib diff --git a/var/lib/upd/scripts/help b/var/lib/upd/scripts/help index 0fac5dd..f0f4921 100644 --- a/var/lib/upd/scripts/help +++ b/var/lib/upd/scripts/help @@ -6,11 +6,12 @@ upd__help() { cat << EOHELP -# upd : System Updater - check | -c : Check for updates (or just type upd) - list | -l : List updates (Check if none) - -v : Show Installed version - -vv : Show Installed and Available versions - -U : Check and Update upd +# upd : System Updater + --readme : Show readme + check | -c : Check for updates (or just type upd) + list | -l : List updates (Check if none) + -v : Show Installed version + -vv : Show Installed and Available versions + -U : Check and Update upd EOHELP } diff --git a/var/lib/upd/scripts/tools b/var/lib/upd/scripts/tools index e22bcc2..0ea0ddd 100644 --- a/var/lib/upd/scripts/tools +++ b/var/lib/upd/scripts/tools @@ -1,6 +1,6 @@ #!/bin/bash # file: tools -# Folder: /var/lib/filebox/scripts/ +# Folder: /var/lib/filebox/scripts # By echolib # License: GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007 diff --git a/var/lib/upd/scripts/updater b/var/lib/upd/scripts/updater index 180938f..10dd240 100644 --- a/var/lib/upd/scripts/updater +++ b/var/lib/upd/scripts/updater @@ -7,33 +7,42 @@ #====================================================================== # Automatic Updater #====================================================================== -upd__Updater() { +Updater() { if__command curl -upd_cur=`awk 'NR==2 {print $3}' "/usr/local/bin/upd"` -upd_onl=`curl -s "$upd_repo_upd" | awk 'NR==2 {print $3}'` +if__command rsync -printf -v versions '%s\n%s' "$upd_cur" "$upd_onl" -[[ `sort -V <<< "$versions" | tail -1` == $upd_cur ]] \ - && echo "# upd is up to date. Current: $upd_cur | Online: $upd_onl" \ +v_cur=`awk 'NR==2 {print $3}' "/usr/local/bin/$app_name"` +echo -n "# $app_name Current: $v_cur | " +v_onl=`curl -s "$repo" | awk 'NR==2 {print $3}'` +if ! [[ $v_onl ]];then + echo -ne "Online: Unknown\n" + exit 1 +else + echo -ne "Online: $v_onl\n" +fi + +printf -v versions '%s\n%s' "$v_cur" "$v_onl" + +[[ `sort -V <<< "$versions" | tail -1` == $v_cur ]] \ && return # Get archive cd /tmp -curl -L -o upd-main.tar.gz "$upd_repo_tar" 2>/dev/null -! [[ -f "upd-main.tar.gz" ]] \ +curl -L -o ${app_name}-main.tar.gz "$repo_tar" 2>/dev/null +! [[ -f "${app_name}-main.tar.gz" ]] \ && echo "! Download error" \ && return -tar -xzf upd-main.tar.gz -cd upd-main +tar -xzf ${app_name}-main.tar.gz +cd ${app_name}-main rsync -a --delete "usr/local/bin/" "/usr/local/bin" -rsync -a --delete "var/lib/upd/" "/var/lib/upd/" -rsync -a --delete "etc/upd/" "/etc/upd/" +rsync -a --delete "var/lib/${app_name}/" "/var/lib/${app_name}/" +rsync -a --delete "etc/${app_name}/" "/etc/${app_name}/" # Check -upd_cur=`awk 'NR==2 {print $3}' "/usr/local/bin/upd"` -echo "# upd updated to: $upd_cur" +v_cur=`awk 'NR==2 {print $3}' "/usr/local/bin/$app_name"` +echo "# $app_name updated to: $v_cur" -rm -rf /tmp/upd-main -rm -f upd-main.tar.gz +rm -rf /tmp/${app_name}-main +rm -f ${app_name}-main.tar.gz } diff --git a/var/lib/upd/setup.sh b/var/lib/upd/setup.sh index baa47aa..172423e 100644 --- a/var/lib/upd/setup.sh +++ b/var/lib/upd/setup.sh @@ -1,8 +1,7 @@ #!/bin/bash -# version: 0.1 # file: setup.sh -# Folder: filebox (from git clone) -# git clone https://git.a-lec.org/echolib/filebox.git +# 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_@} @@ -12,19 +11,37 @@ unset ${!setup_@} #---------------------------------------------------------------------- # Directories to CHOWN -R set_dir_R_CHO=( -/var/lib/upd -/etc/upd +var/lib/upd +etc/upd ) -# bin Directories: Check ONLY with mkdir -p -setup_dir_bin="/usr/local/bin" +# Directories to check ONLY with mkdir -p +setup_dir_bin="usr/local/bin" -# filename application to CHOWN -setup_file_bin="upd" +# main app name +setup_app_name="upd" +# filename application to CHOWN + CHMOD 755 +set_files_R_CHO=( +upd +) # bin uri set_bin_uri="$setup_dir_bin/$setup_file_bin" +#====================================================================== + +#---------------------- +# Check commands exists +#---------------------- +if__command() { +! [[ `command -v $1` ]] \ + && echo -e "! Missing $1 command" \ + && exit 1 +} + +if__command sudo +if__command rsync + #------------------------ # Check commands function #------------------------ @@ -42,15 +59,15 @@ fi #---------------------------------------------------------------------- case "$1" in remove|-R) - [[ -f "$set_bin_uri" ]] \ - && echo "- Removing $set_bin_uri" \ - && sudo rm -f "$set_bin_uri" + [[ -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" + [[ -d "/$setup_folder" ]] \ + && echo "- Removing /$setup_folder" \ + && sudo rm -rf "/$setup_folder" done echo "# upd is uninstalled. Thank you for testing." ;; @@ -60,51 +77,54 @@ esac # Installation Process #---------------------------------------------------------------------- setup__install() { -for setup_folder in ${set_dir_R_CHO[@]} -do - echo "- Folder: $setup_folder" + + setup__gen_process() { + echo "- Folder: /$1" + echo -ne " Created:" - sudo mkdir -p "$setup_folder" \ + sudo mkdir -p "/$1" \ && cmd_res="$?" \ && setup__check_err - echo -ne " Owner $setup_user:" - sudo chown -R "$setup_user":"$setup_user" "$setup_folder" \ - && 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 "${PWD}$setup_folder/" "$setup_folder/" \ + 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 -echo "- Folder: $setup_dir_bin" -echo -ne " Created:" -sudo mkdir -p "$setup_dir_bin" \ - && cmd_res="$?" \ - && setup__check_err +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 -echo "- File: $setup_file_bin" -echo -ne " Copy $PWD$set_bin_uri + chmod 755:" -sudo cp -f "$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" - 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" \ @@ -113,9 +133,9 @@ printf '\n%b\n%b\n%b\n' \ # Updaate && Check Version setup_ins_version=` -awk 'NR==2 {print $3}' "$setup_dir_bin/$setup_file_bin" 2>/dev/null` +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"` +awk 'NR==2 {print $3}' "$setup_dir_bin/$setup_file_bin" 2>/dev/null` [[ "$setup_ins_version" == "$setup_new_version" ]] \ && echo "$setup_file_bin is already up to date" \ @@ -123,12 +143,15 @@ awk 'NR==2 {print $3}' "$PWD$setup_dir_bin/$setup_file_bin"` && 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 +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" \ @@ -137,18 +160,8 @@ case "$setup_user" in ;; 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" +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 ;;