From e1b8cb20e3ae5fd23917011efb818a7a5d139daa Mon Sep 17 00:00:00 2001 From: LAN-TW Date: Fri, 20 Sep 2013 22:15:22 +0800 Subject: =?UTF-8?q?bash=5Finclude:=20=E9=87=8D=E5=AF=AB=E8=A8=AD=E5=AE=9A?= =?UTF-8?q?=E6=AA=94=E4=B8=8B=E8=BC=89=E5=B7=A5=E5=85=B7=E3=80=81=E5=8A=A0?= =?UTF-8?q?=E5=85=A5=E8=A8=AD=E5=AE=9A=E6=AA=94=E6=AF=94=E8=BC=83=E8=88=87?= =?UTF-8?q?=E4=BA=92=E5=8B=95=E5=BC=8F=E6=9B=B4=E6=96=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bash_include | 131 +++++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 105 insertions(+), 26 deletions(-) diff --git a/bash_include b/bash_include index b75b28a..719968d 100644 --- a/bash_include +++ b/bash_include @@ -932,44 +932,62 @@ function path_editor () function configfile_fetch () { - local mirror_list=" - http://www.tfcis.org/~lantw44/configfile/ - http://phantom.tfcis.org/~lantw44/mirror/forum_tfcis_org/configfile/ - http://master.lant.com.tw/~lantw44/configfile/ - http://www.csie.ntu.edu.tw/~b01902062/configfile/" - local url_append - local file_name + local cgit_mirror_list=( + "http://www.tfcis.org/~lantw44/cgit/cgit.cgi/configfile/plain" + "http://phantom.tfcis.org/~lantw44/cgit/cgit.cgi/configfile/plain" + "http://master.lant.com.tw/~lantw44/cgit/cgit.cgi/configfile/plain") + local github_mirror_list=( + "https://raw.github.com/lantw44/configfile" + "http://raw.github.com/lantw44/configfile") + local args + local file_url local file_version + local completed + local -a file_name + local -i i + local -i j - if [ "$3" ]; then - url_append="$3" - else - url_append="Unix-like/" - fi if [ "$1" ]; then - file_name="$1" + file_version="$1" else - file_name="bash_include vimrc screenrc" + file_version="master" fi - if [ "$2" ]; then - file_version="-$2" - else - url_append="${url_append}latest/" + + args=("$@") + for((i=2, j=0; i<=$#; i++, j++)){ + file_name[$j]="${args[$i]}" + } + + if [ -z "$file_name"]; then + file_name=("bash_include" "vimrc" "screenrc") fi - for file in $file_name + for file in ${file_name[@]} do - for site in $mirror_list + completed="false" + + for site in ${cgit_mirror_list[@]} + do + [ "$completed" = "true" ] && break + file_url="$site/$file?id=$file_version" + if fetch_and_merge "$HOME/.${file}" "$file_url" + then + completed="true" + break + fi + done + + for site in ${github_mirror_list[@]} do - if fetch_remote_file "$HOME/.${file}.new" \ - "${site}${url_append}${file}${file_version}" + [ "$completed" = "true" ] && break + file_url="$site/$file_version/$file" + if fetch_and_merge "$HOME/.${file}" "$file_url" then - [ -e "$HOME/.${file}" ] && \ - mv -vf "$HOME/.${file}" "$HOME/.${file}.old" - mv -vf "$HOME/.${file}.new" "$HOME/.${file}" + completed="true" break fi done + done } @@ -1021,6 +1039,66 @@ function fetch_remote_file () return $rval } +function fetch_and_merge () +{ + local merge_cmd + local merge_cmd_prog + local local_file="$1" + local remote_file="$2" + if fetch_remote_file "${local_file}.new" "${remote_file}" + then + if [ '!' -e "${local_file}" ]; then + mv -vf "${local_file}.new" "${local_file}" + else + while true + do + echo "" + printf "Configuration files update tool: " + printf "Updating \e[1;35m${local_file}\e[m\n" + echo "" + echo " Install: Install the new version" + echo " Keep: Keep the old version" + echo " Retry: Give up downloaded file and try the next mirror" + echo "" + echo " Diff: View the difference between versions" + echo " Merge: Merge two files by yourself" + echo "" + read -e -p "[I]nstall/(K)eep/(R)etry/(D)iff/(M)erge ? " merge_cmd + case "${merge_cmd}" in + ''|I|i) + mv -vf "${local_file}.new" "${local_file}" + break + ;; + K|k) + rm -f "${local_file}.new" + break + ;; + R|r) + rm -f "${local_file}.new" + return 1 + ;; + D|d) + diff -u "${local_file}" "${local_file}.new" | $PAGER + ;; + M|m) + read -e -p "Merge tool: " -i "$merge_cmd_prog" merge_cmd_prog + if $merge_cmd_prog "${local_file}" "${local_file}.new"; then + break + else + echo "Command is exited with error. Please try again." + fi + ;; + *) + printf " \e[33m*** Unknown command ***\e[m \n" + ;; + esac + done + fi + return 0 + fi + return 1 +} + ########## Configuration File End ########## function varset () @@ -1470,9 +1548,10 @@ function help_function () x newpath_init x newpath_gen <<< Group: Configuration File >>> - configfile_fetch [file_name_list [file_version [system_type]]] + configfile_fetch [git_tag [file_name_list ...]] configfile_initial_setup x fetch_remote_file local_file_name remote_url + x fetch_and_merge local_file_name remote_url <<< System: Terminal >>> set_console_title -- cgit v1.2.3