6 ######################################################################################
10 ["Undefined operation"]="未定義のオペレーションです"
11 ["only one operation may be used at a time"]="一度に使用できるオペレーションは1つだけです"
12 ["Failed to set the argument of %s"]="%sの引数の設定に失敗しました"
13 ["Setting that command is not currently supported"]="そのコマンドの設定は現在サポートされていません"
14 ["Install dependent packages..."]="依存パッケージをインストールします..."
15 ["No package with an exact name match was found"]="完全に一致する名前のパッケージが見つかりませんでした"
16 ["Select a package %s with an exact name match"]="名前が完全に一致するパッケージ %s を選択します"
17 ["Download PKGBUILD of %s"]="%s のPKGBUILDをダウンロード"
18 ["Get PKGBUILD from %s"]="%sからPKGBUILDを取得します"
19 ["PKGBUILD has already been downloaded"]="PKGBUILDは既にダウンロードされています"
20 ["Do you want to overwrite and download? [n] :"]="上書きダウンロードしますか? [n] :"
21 ["Unpacking the tarball of PKGBUILD ..."]="PKGBUILDを展開しています ..."
23 ["This is a feature that has not been implemented yet"]="まだ実装されていない機能です"
25 ######################################################################################
37 aururl="https://aur.archlinux.org/"
40 #-- options (bool) --#
54 makepkg_command="/usr/bin/makepkg"
56 makepkg_config="/etc/makepkg.conf"
62 pacman_command="/usr/bin/pacman"
64 pacman_config="/etc/pacman.conf"
70 git_command="/usr/bin/git"
76 gpg_command="/usr/bin/gpg"
82 sudo_command="/usr/bin/sudo"
88 ######################################################################################
93 # https://github.com/FascodeNet/alterlinux/blob/dev/tools/msg.sh の変数名にアンダーバーを追加し関数化
95 local OPTIND OPTARG arg
97 local _appname="msg.sh"
98 local _bash_debug=false
102 local _msg_type="info"
104 local _label_space="7"
105 local _adjust_chr=" "
106 local _customized_label=false
107 local _customized_label_color=false
109 local _noappname=false
110 local _noadjust=false
111 local _output="stdout"
114 echo "usage ${0} [option] [type] [message]"
116 echo "Display a message with a colored app name and message type label"
118 echo " General type:"
119 echo " info General message"
120 echo " warn Warning message"
121 echo " error Error message"
122 echo " debug Debug message"
124 echo " General options:"
125 echo " -a [name] Specify the app name"
126 echo " -c [character] Specify the character to adjust the label"
127 echo " -l [label] Specify the label."
128 echo " -n | --nocolor No output colored output"
129 echo " -o [option] Specify echo options"
130 echo " -r [color] Specify the color of label"
131 echo " -s [number] Specifies the label space."
132 echo " -x | --bash-debug Enables output bash debugging"
133 echo " -h | --help This help message"
135 echo " --nolabel Do not output label"
136 echo " --noappname Do not output app name"
137 echo " --noadjust Do not adjust the width of the label"
140 while getopts "a:c:l:no:r:s:xh-:" arg; do
146 _adjust_chr="${OPTARG}"
149 _customized_label=true
150 _msg_label="${OPTARG}"
156 _echo_opts="${OPTARG}"
159 _customized_label_color=true
191 _label_space="${OPTARG}"
232 shift $((OPTIND - 1))
257 # You can specify multiple decorations with ;.
258 # 0 => All attributs off (ノーマル)
260 # 4 => Underscore (下線)
262 # 7 => Reverse video on (色反転)
269 [[ "${_customized_label_color}" = false ]] && _labelcolor="32"
270 [[ "${_customized_label}" = false ]] && _msg_label="Info"
276 [[ "${_customized_label_color}" = false ]] && _labelcolor="33"
277 [[ "${_customized_label}" = false ]] && _msg_label="Warning"
283 [[ "${_customized_label_color}" = false ]] && _labelcolor="35"
284 [[ "${_customized_label}" = false ]] && _msg_label="Debug"
290 [[ "${_customized_label_color}" = false ]] && _labelcolor="31"
291 [[ "${_customized_label}" = false ]] && _msg_label="Error"
295 echo "Please specify the message type" >&2
299 echo "Unknown message type" >&2
304 _word_count="${#_msg_label}"
309 if [[ "${_nolabel}" = false ]]; then
310 if [[ "${_noadjust}" = false ]]; then
311 for __time in $( seq 1 $(( ${_label_space} - ${_word_count})) ); do
312 echo -ne "${_adjust_chr}"
315 if [[ "${_nocolor}" = false ]]; then
316 echo -ne "\e[$([[ -v _backcolor ]] && echo -n "${_backcolor}"; [[ -v _labelcolor ]] && echo -n ";${_labelcolor}"; [[ -v _decotypes ]] && echo -n ";${_decotypes}")m${_msg_label}\e[m "
318 echo -ne "${_msg_label} "
324 if [[ "${_noappname}" = false ]]; then
325 if [[ "${_nocolor}" = false ]]; then
326 echo -ne "\e[36m[${_appname}]\e[m "
328 echo -ne "[${_appname}] "
333 for _count in $(seq "1" "$(echo -ne "${_message}\n" | wc -l)"); do
334 _echo_message=$(echo -ne "${_message}\n" |head -n "${_count}" | tail -n 1 )
335 _full_message="$(echo_appname)$(echo_type)${_echo_message}"
338 echo ${_echo_opts} "${_full_message}" >&1
341 echo ${_echo_opts} "${_full_message}" >&2
344 echo ${_echo_opts} "${_full_message}" > ${_output}
356 local _locale="$(echo "${LANG}" | cut -d "." -f 1)"
360 local _translated_text="$(eval echo '$'{${_locale}[\"${*}\"]})"
362 if [[ -z "${_translated_text}" ]]; then
365 echo "${_translated_text}"
369 local _text _fulltext=() _main
371 if declare -p "${_locale}" 2> /dev/null 1>/dev/null; then
372 _main="$(_get_text ${1})"
377 echo "$(printf "${_main}" ${@})"
379 _msg_translate "${@}"
384 # Show an INFO message
387 if [[ "${msgdebug}" = false ]]; then
390 local _msg_opts="-a ${wfa_name}"
391 if [[ "${1}" = "-n" ]]; then
392 _msg_opts="${_msg_opts} -o -n"
395 [[ "${msgdebug}" = true ]] && _msg_opts="${_msg_opts} -x"
396 [[ "${nocolor}" = true ]] && _msg_opts="${_msg_opts} -n"
397 msg ${_msg_opts} info "$(translate "${@}")"
398 if [[ "${bash_debug}" = true ]]; then
403 # Show an Warning message
406 if [[ "${msgdebug}" = false ]]; then
409 local _msg_opts="-a ${wfa_name}"
410 if [[ "${1}" = "-n" ]]; then
411 _msg_opts="${_msg_opts} -o -n"
414 [[ "${msgdebug}" = true ]] && _msg_opts="${_msg_opts} -x"
415 [[ "${nocolor}" = true ]] && _msg_opts="${_msg_opts} -n"
416 msg ${_msg_opts} warn "$(translate "${@}")"
417 if [[ "${bash_debug}" = true ]]; then
422 # Show an debug message
425 if [[ "${msgdebug}" = false ]]; then
428 if [[ "${debug}" = true ]]; then
429 local _msg_opts="-a ${wfa_name}"
430 if [[ "${1}" = "-n" ]]; then
431 _msg_opts="${_msg_opts} -o -n"
434 [[ "${msgdebug}" = true ]] && _msg_opts="${_msg_opts} -x"
435 [[ "${nocolor}" = true ]] && _msg_opts="${_msg_opts} -n"
436 msg ${_msg_opts} debug "$(translate "${@}")"
438 if [[ "${bash_debug}" = true ]]; then
443 # Show an ERROR message then exit with status
445 # $2: exit code number (with 0 does not exit)
447 if [[ "${msgdebug}" = false ]]; then
450 local _msg_opts="-a ${wfa_name}"
451 if [[ "${1}" = "-n" ]]; then
452 _msg_opts="${_msg_opts} -o -n"
455 [[ "${msgdebug}" = true ]] && _msg_opts="${_msg_opts} -x"
456 [[ "${nocolor}" = true ]] && _msg_opts="${_msg_opts} -n"
457 msg ${_msg_opts} error "$(translate "${@}")"
458 if [[ "${bash_debug}" = true ]]; then
463 ######################################################################################
468 # Delete the file if it exists.
469 # For directories, rm -rf is used.
470 # If the file does not exist, skip it.
471 # remove <file> <file> ...
473 local _list=($(echo "$@")) _file
474 for _file in "${_list[@]}"; do
475 msg_debug "Removing ${_file}"
476 if [[ -f "${_file}" ]]; then
478 elif [[ -d "${_file}" ]]; then
485 local _pacman_help=false
490 echo "${wfa_command}"
491 echo "${wfa_command} <operation> [...]"
494 echo " ${wfa_command} {-h --help}"
495 echo " ${wfa_command} {-V --version}"
496 #echo " ${wfa_command} {-D --database} <options> <package(s)>"
497 #echo " ${wfa_command} {-F --files} [options] [package(s)]"
498 echo " ${wfa_command} {-Q --query} [options] [package(s)]"
499 echo " ${wfa_command} {-R --remove} [options] <package(s)>"
500 echo " ${wfa_command} {-S --sync} [options] [package(s)]"
501 #echo " ${wfa_command} {-T --deptest} [options] [package(s)]"
502 #echo " ${wfa_command} {-U --upgrade} [options] <file(s)>"
504 #echo "New operations:"
505 #echo " ${wfa_command} {-P --show} [options]"
506 #echo " ${wfa_command} {-G --getpkgbuild} [package(s)]"
509 echo " --repo Assume targets are from the repositories"
510 echo " -a --aur Assume targets are from the AUR"
512 echo "Permanent configuration options:"
513 echo " --aururl <url> Set an alternative AUR URL"
514 echo " --makepkg <file> makepkg command to use"
515 echo " --mflags <flags> Pass arguments to makepkg"
516 echo " --pacman <file> pacman command to use"
517 echo " --git <file> git command to use"
518 echo " --gitflags <flags> Pass arguments to git"
519 echo " --gpg <file> gpg command to use"
520 echo " --gpgflags <flags> Pass arguments to gpg"
521 echo " --config <file> pacman.conf file to use"
522 echo " --makepkgconf <file> makepkg.conf file to use"
523 echo " --nomakepkgconf Use the default makepkg.conf"
527 local _wfa_usage_sync
529 echo "usage: ${wfa_command} {-S --sync} [options] [package(s)]"
531 echo " -b, --dbpath <path> set an alternate database location"
532 echo " --config <path> set an alternate configuration file"
533 echo " --noconfirm do not ask for any confirmation"
537 if [[ "${operation}" = "none" ]]; then
539 elif [[ "${_pacman_help}" = true ]]; then
540 "${pacman_command}" -h --${operation}
541 elif [[ "$(type -t "_wfa_usage_${operation}" )" = "function" ]]; then
542 _wfa_usage_${operation}
544 msg_error "Undefined operation"
550 if [[ "${operation}" = "none" ]]; then
552 add_args pacman "--${operation}"
554 msg_error "only one operation may be used at a time"
560 if (( ${UID} == 0 )); then
568 run_sudo "${pacman_command}" ${@}
572 # https://github.com/FascodeNet/aptpac/blob/master/aptpac のADD_OPTION関数を参考
573 # Usage: add_args [pacman/makepkg] <args1> <args2>...
581 _args_array=(${makepkg_args})
583 makepkg_args=${_args_array[@]}
584 msg_debug "makepkg ARGS: ${makepkg_args}"
588 _args_array=(${pacman_args})
590 pacman_args=${_args_array[@]}
591 msg_debug "pacman ARGS: ${pacman_args}"
594 _args_array=(${mpg_args})
596 git_args=${_args_array[@]}
597 msg_debug "git ARGS: ${git_args}"
600 _args_array=(${gpg_args})
602 gpg_args=${_args_array[@]}
603 msg_debug "gpg ARGS: ${gpg_args}"
606 _args_array=(${sudo_args})
608 sudo_args=${_args_array[@]}
609 msg_debug "sudo ARGS: ${sudo_args}"
612 msg_error "Failed to set the argument of %s" "${_target}"
613 msg_error "Setting that command is not currently supported"
619 # 引数で指定されたパッケージがAUR以外の場所に存在しない場合にのみ正常終了します(AURのパッケージの場合に正常終了)
620 check_aur_package() {
621 local _package="${1}"
622 # 参考: https://qiita.com/Hayao0819/items/a8740a17301fafa2fdab
623 if [[ -z "$(pacman -Ssq "${_package}" 2>/dev/null | grep -o ".*${_package}$")" ]]; then
632 # 引数で指定されたパッケージが既にインストールされている場合は正常終了します。
633 check_installed_package() {
634 local _package="${1}"
635 if "${pacman_command}" -Qq "${_package}" > /dev/null 2>&1; then
643 local _user_config_dir
644 if [[ -v XDG_CONFIG_HOME ]]; then
645 _user_config_dir="${XDG_CONFIG_HOME}"
647 _use_config_dir="${HOME}/.config"
649 if [[ -f "${_use_config_dir}/user-dirs.dirs" ]]; then
650 source "${_use_config_dir}/user-dirs.dirs"
652 if [[ -v XDG_CACHE_HOME ]]; then
653 echo -n "${XDG_CACHE_HOME}"
656 echo -n "${HOME}/.cache"
661 # Usage: get_srcinfo_data <path> <var>
662 # 参考: https://qiita.com/withelmo/items/b0e1ffba639dd3ae18c0
664 local _srcinfo="${1}" _ver="${2}"
665 local _srcinfo_json=$(python << EOF
666 from srcinfo.parse import parse_srcinfo; import json
670 parsed, errors = parse_srcinfo(text)
671 print(json.dumps(parsed))
674 echo "${_srcinfo_json}" | jq -rc "${2}" | tr '\n' ' '
678 # AURからパッケージをビルドしてインストールします
679 # 現在1つのパッケージしか指定できません
680 install_aur_package() {
681 local _package="${1}"
684 if [[ ! -v wfa_cache_dir ]]; then
685 wfa_cache_dir="$(get_cache_dir)/wfa"
687 mkdir -p "${wfa_cache_dir}/archive"
688 mkdir -p "${wfa_cache_dir}/build/${_package}"
691 local _aur_json=$(curl -sL "https://aur.archlinux.org/rpc/?v=5&type=search&by=name&arg=${_package}" | jq -r)
692 if (( "$(echo "${_aur_json}" | jq -r ".resultcount")" == 0 )); then
693 msg_error "Could not find all required packages:\n ${_package}"
697 local _found_packages="$(echo "${_aur_json}" | jq -r --tab '.results[].Name')"
698 #msg_debug "Found package: $(echo ${_found_packages} | tr '\n' ' ')"
700 if [[ -n "$(echo "${_found_packages}" | grep -x "${_package}" )" ]]; then
701 msg_debug "Select a package %s with an exact name match" "${_package}"
703 msg_error "No package with an exact name match was found"
708 msg_info "Download PKGBUILD of %s" "${_package}"
709 _aur_json=$(echo "${_aur_json}" | jq -r ".results[] | select(.Name == \"${_package}\")" )
710 local _aur_snapshot_url="${aururl%/}$(echo "${_aur_json}" | jq -r ".URLPath")"
711 local _aur_version="$(echo "${_aur_json}" | jq -r ".Version")"
712 msg_debug "Get PKGBUILD from %s" "${_aur_snapshot_url}"
714 local _pkgbuild_archive_path="${wfa_cache_dir}/archive/${_package}-${_aur_version}"
715 local _download_pkgbuild=true
716 if [[ -f "${_pkgbuild_archive_path}" ]]; then
717 msg_warn "PKGBUILD has already been downloaded"
718 msg_warn -n "Do you want to overwrite and download? [n] :"
720 if [[ "${noconfirm}" = true ]]; then
726 case "${_yes_or_no}" in
727 "y" | "Y" | "yes" | "Yes" | "YES" ) _download_pkgbuild=true ;;
728 * ) _download_pkgbuild=false ;;
731 if [[ "${_download_pkgbuild}" = true ]]; then
732 remove "${_pkgbuild_archive_path}"
733 curl -L -C - -f -o "${_pkgbuild_archive_path}" "${_aur_snapshot_url}"
737 msg_info "Unpacking the tarball of PKGBUILD ..." #ここまで翻訳
738 tar -xv -f "${_pkgbuild_archive_path}" -C "${wfa_cache_dir}/build/" > /dev/null 2>&1
741 local _build_dir="${wfa_cache_dir}/build/${_package}"
742 if [[ ! -f "${_build_dir}/.SRCINFO" ]]; then
743 msg_warn ".SRCINFO was not found.\nGenerate it using makepkg."
746 "${makepkg_command}" --printsrcinfo > "${_build_dir}/.SRCINFO"
750 local _makedepends="$(get_srcinfo_data "${_build_dir}/.SRCINFO" ".makedepends[]?")"
751 local _depends="$(get_srcinfo_data "${_build_dir}/.SRCINFO" ".depends[]?")"
752 local _conflicts="$(get_srcinfo_data "${_build_dir}/.SRCINFO" ".conflicts[]?")"
753 msg_debug "makedepends: ${_makedepends}"
754 msg_debug "depends: ${_depends}"
755 msg_debug "conflicts: ${_conflicts}"
759 local _pkg _conflicts_found=false
760 for _pkg in ${_conflicts[@]}; do
761 if "${pacman_command}" -Qq "${_pkg}" > /dev/null 2>&1 ; then
762 _conflicts_found=true
763 msg_error "${_package} is colliding with ${_pkg}"
766 if "${pacman_command}" -Qq "${_package}" > /dev/null 2>&1 && [[ ! "$("${pacman_command}" -Qq "${_package}" 2> /dev/null)" = "${_package}" ]]; then
767 msg_error "${_package} is colliding with $("${pacman_command}" -Qq "${_package}")"
768 _conflicts_found=true
770 if [[ "${_conflicts_found}" = true ]]; then
771 msg_error "A conflict was found."
777 if [[ "${nodeps}" = false ]]; then
778 msg_info "Install dependent packages..."
779 local _force_aur="${force_aur}"
781 install_package "${_depends}"
782 force_aur="${_force_aur}"
788 if [[ -d "${_build_dir}/src" ]]; then
789 msg_info "Found ${_build_dir}/src"
790 msg_info "Packages to cleanBuild? [n] :"
793 if [[ "${noconfirm}" = true ]]; then
799 case "${_yes_or_no}" in
800 "y" | "Y" | "yes" | "Yes" | "YES" ) add_args makepkg "--clean" ;;
806 add_args "makepkg" "-sf"
809 "${makepkg_command}" "${makepkg_args}"
816 "${makepkg_command}" --printsrcinfo > "${_build_dir}/.SRCINFO"
818 local _pkgnames=($(get_srcinfo_data "${_build_dir}/.SRCINFO" ".packages | keys[]" | sed 's/ //g'))
819 local _pkgver="$(get_srcinfo_data "${_build_dir}/.SRCINFO" ".pkgver" | sed 's/ //g')"
820 local _pkgrel="$(get_srcinfo_data "${_build_dir}/.SRCINFO" ".pkgrel" | sed 's/ //g')"
821 local _arch_array=($(get_srcinfo_data "${_build_dir}/.SRCINFO" ".arch[]"))
823 if [[ "${_arch_array[*]}" = "any" ]]; then
829 source "${makepkg_config}"
832 local _pkgfilelist=()
833 for _pkgname in ${_pkgnames[@]}; do
834 _pkgfilelist+=("${_build_dir}/${_pkgname}-${_pkgver}-${_pkgrel}-${_arch}${_PKGEXT}")
838 run_pacman -U --noconfirm ${_pkgfilelist[@]}
842 search_aur_package() {
843 local _package="${1}"
844 local _aur_json=$(curl -sL "https://aur.archlinux.org/rpc/?v=5&type=search&by=name-desc&arg=${_package}" | jq -r )
845 local _found_result_count="$(echo "${_aur_json}" | jq -r ".resultcount")"
846 if (( "${_found_result_count}" == 0 )); then
847 msg_error "Could not find all required packages:\n ${_package}"
850 _aur_json=$(echo "${_aur_json}" | jq -r ".results[]")
854 local _found_pkgname=($(echo "${_aur_json}" | jq -r ".Name" ))
859 local _found_package __pkgver __popularity __vote __pkgdesc
860 for _found_package in ${_found_pkgname[@]}; do
861 _found_json="$(echo ${_aur_json} | jq "select(.Name == \"${_found_package}\")")"
862 __pkgver="$(echo "${_found_json}" | jq -r ".Version" )"
863 __popularity="$(echo "${_found_json}" | jq -r ".Popularity" )"
864 __vote="$(echo "${_found_json}" | jq -r ".NumVotes" )"
865 __pkgdesc="$(echo "${_found_json}" | jq -r ".Description" )"
867 echo "aur/${_found_package} ${__pkgver} (+${__vote} ${__popularity})"
869 unset __pkgver __popularity __vote __pkgdesc _found_json
874 operation_version() {
875 # Pyalpmからlibalpmの値を取得
876 # 参考: https://pyalpm.readthedocs.io/en/latest/pyalpm/pyalpm.html
877 local _libalpm_version="$(python3 -c 'import pyalpm; print(pyalpm.alpmversion())')"
878 local _pacman_version="$("${pacman_command}" -Q pacman | cut -d ' ' -f 2)"
879 echo "wfa v${wfa_version} - pacman v${_pacman_version} - libalpm v${_libalpm_version}"
883 run_pacman ${pacman_args} "${specified_packages[@]}"
886 # Usage: install_package <package1> <package2>...
889 for _package in ${@}; do
890 if ! check_installed_package "${_package}"; then
891 if ! check_aur_package "${_package}"; then
892 # 公式パッケージなのでpacmanでそのままインストール
893 run_pacman ${pacman_args} "${_package}"
896 install_aur_package "${_package}"
897 #msg_error "Getting the AUR package has not been implemented yet.
904 upgrade_aur_package() {
905 msg_error "This is a feature that has not been implemented yet"
910 if [[ "${sync_search}" = true ]]; then
911 for _package in ${specified_packages[@]}; do
912 search_aur_package "${_package}"
914 "${pacman_command}" ${pacman_args} ${specified_packages[@]}
916 if [[ "${sync_upgrade}" = true ]]; then
918 run_pacman ${pacman_args} --sysupgrade
920 for _package in ${specified_packages[@]}; do
921 if ! check_aur_package "${_package}" && [[ "${force_aur}" = false ]]; then
922 # 公式パッケージなのでpacmanでそのままインストール
923 run_pacman ${pacman_args} "${_package}"
926 install_aur_package "${_package}"
927 #msg_error "Getting the AUR package has not been implemented yet."
937 _opt_short="QRShVdb:aysu"
938 _opt_long="query,remove,sync,help,version,debug,dbpath:,aururl,aur,noconfirm,config:,makepkg:,mflags:,pacman:,git:,gitflags:,gpg:,gpgflags:,makepkgconf:,nomakepkgconf,nodeps,refresh,bash-debug,msg-debug,sysupgrade"
940 OPT=$(getopt -o ${_opt_short} -l ${_opt_long} -- ${ARGUMENT})
941 [[ ${?} != 0 ]] && exit 1
942 unset _opt_short _opt_long
945 msg_debug "Argument: ${OPT}"
950 set_operation "query"
954 set_operation "remove"
962 set_operation "version"
981 msg_debug "Assume targets are from the AUR"
986 add_args pacman "--debug"
991 add_args pacman "--nodeps"
995 add_args pacman "--dbpath '${2}'"
999 option_y_count=$(( option_y_count + 1 ))
1003 add_args pacman "--search"
1016 add_args pacman "--noconfirm"
1021 pacman_config="${2}"
1022 add_args pacman "--config \"${2}\""
1026 makepkg_command="${2}"
1034 pacman_command="${2}"
1046 if [[ "${nomakepkgconf}" = false ]]; then
1047 makepkg_config="${2}"
1049 msg_warn "--nomakepkgconf is specified.\n--makepkgconf has been ignored."
1054 makepkg_config="/etc/makepkg.conf"
1082 specified_packages=(${@})
1084 # Run database update
1085 if (( "${option_y_count}" == 1 )); then
1087 elif (( "${option_y_count}" >= 2 )); then
1091 case "${operation}" in
1105 msg_error "Undefined operation"