diff options
author | portix <none@none> | 2012-09-01 02:08:52 +0200 |
---|---|---|
committer | portix <none@none> | 2012-09-01 02:08:52 +0200 |
commit | 1814d379f3ed4cc8675434b0e51832aa6bb4c43e (patch) | |
tree | af7cde235770ae7b7ff40fe655aff8f0ea5b198b /tools/dwb_em.in | |
parent | caa9a0ce81d300d70399535378f0a699aafab32d (diff) | |
download | dwb-1814d379f3ed4cc8675434b0e51832aa6bb4c43e.zip |
Forget dwb_em
Diffstat (limited to 'tools/dwb_em.in')
-rw-r--r-- | tools/dwb_em.in | 413 |
1 files changed, 0 insertions, 413 deletions
diff --git a/tools/dwb_em.in b/tools/dwb_em.in deleted file mode 100644 index ab22341b..00000000 --- a/tools/dwb_em.in +++ /dev/null @@ -1,413 +0,0 @@ -#!/bin/bash - -API_BASE="https://api.bitbucket.org/1.0/repositories/portix/dwb_extensions/src/tip/src/" -DWB_API_BASE="https://api.bitbucket.org/1.0/repositories/portix/dwb/src/tip/extensions/" -REPO_BASE="https://bitbucket.org/portix/dwb_extensions/raw/tip/src" -DATA_FORMAT="?format=yaml" - -SCRIPT_DIR="${XDG_CONFIG_HOME-${HOME}/.config}/dwb/userscripts" -LOADER="${SCRIPT_DIR}/extension_loader.js" -INST_DIR="${XDG_DATA_HOME-$HOME/.local/share}/dwb/extensions" -SYSTEM_DIR="@SYSTEM_DIR@" - -M_DIFF_VIEWER=${DIFF_VIEWER-vimdiff} -M_EDITOR=${EDITOR-vim} - -META_INFO="$INST_DIR/.metainfo" -META_INSTALLED="${INST_DIR}/.installed" - -WGET_FLAGS=-q -CURL_FLAGS=-s - -QUIET=0 -BIND=0 -UPDATED_METAINFO=0 - -GREEN="\033[32m" -RED="\033[31m" -BOLD="\033[1m" -NC="\033[0m" - -normalize() { - local prefix="$1" current="$2" tmp - local COLUMNS=$(tput cols) - local maxl=$(expr $COLUMNS - 4) - while :; do - if [ "${#current}" -ge "${maxl}" ]; then - tmp=${current:0:maxl} - tmp=${tmp% *} - printf "%b %b\n" "$prefix" "$tmp" - current="${current:$((${#tmp}+1))}" - else - printf "%b %b\n" "$prefix" "$current" - break; - fi - done -} -error() { - if [ ${QUIET} -eq 0 ]; then - normalize "${RED}==>${NC}" "$1" - fi -} - -die() { - error "$@" - exit 1 -} - -notify() { - if [ ${QUIET} -eq 0 ]; then - normalize "${GREEN}==>${NC}" "$1" - #while [ "$current" ]; do - # tmp=${current:0:76} - # tmp=${tmp% *} - # printf "${GREEN}==>${NC} %b\n" "$tmp" - # current="${current:$((${#tmp}+1))}" - #done - fi -} - -check_dir() { - if [ ! -d "$1" ]; then - notify "creating $1" - mkdir -p "$1" - fi -} - -update_metainfo () { - if [ "$UPDATED_METAINFO" -eq 0 ]; then - notify "syncing metadata" - curl "${CURL_FLAGS}" "${API_BASE}${DATA_FORMAT}" | \ - awk '/^\s*-\s*{path:/ { p=substr($3, index($3, "/") + 1); - print substr(p, 0, length(p)-1)" "substr($5, 0, length($5)-1) }' \ - > "${META_INFO}" - curl "${CURL_FLAGS}" "${DWB_API_BASE}${DATA_FORMAT}" | - awk '/^\s*-\s*{path:/ { p=substr($3, index($3, "/") + 1); - print substr(p, 0, length(p)-1)" "substr($5, 0, length($5)-1) }' \ - >> "${META_INFO}" - UPDATED_METAINFO=1 - fi -} - -list() { - local ext exts - if [ ! -f "${META_INFO}" ]; then - update_metainfo - fi - notify "available extensions:" - awk '{ print " * "$1 }' ${META_INFO} -} - -get_default_config() { - local l_file - if [ -f ${INST_DIR}/$1 ]; then - l_file=${INST_DIR}/$1 - elif [ -f ${SYSTEM_DIR}/$1 ]; then - l_file=${SYSTEM_DIR}/$1 - fi - if [ "${l_file}" ]; then - awk "/\/\/>DEFAULT_CONFIG/ { f=0 } f; - /\/\/<DEFAULT_CONFIG/ { f=1; next }" "${l_file}" - fi -} - -yes_no() { - local question="${GREEN}==>${NC} $1 (Y/n)?" - printf "${question}" - while read -p " "; do - case ${REPLY} in - ""|Y|y) return 0;; - n|N) return 1;; - *) error "try again" - printf "${question}";; - esac - done -} - -update_installed() { - notify "updating metadata" - if [ ! -f ${META_INSTALLED} ]; then - grep "^$1" "${META_INFO}" > "${META_INSTALLED}" - else - local tmpfile=$(mktemp) - mv "${META_INSTALLED}" "${tmpfile}" - awk -v ext="^$1" ' - NR==FNR { if ($0~ext) { meta=$0 } next } - $0!~ext; END { print meta } - ' ${META_INFO} ${tmpfile} > "${META_INSTALLED}" - rm "$tmpfile" - fi -} - -install_extension() { - local ret; - local extension="$1" - local output="${INST_DIR}/$extension" - check_dir "${INST_DIR}" - if [ "$1" ]; then - if [ -f ${SYSTEM_DIR}/$extension ]; then - notify "using ${SYSTEM_DIR}/$extension" - update_installed ${extension} - return 0; - elif grep -q "^$1" "${META_INFO}"; then - notify "downloading $extension" - wget "${WGET_FLAGS}" "${REPO_BASE}/$extension" -O "${output}" - case $? in - 7) die "wget error: network failure";; - 8) die "wget error: server responded an error";; - 0) update_installed "${extension}" - return 0;; - esac - else - die "extension $extension not found" - fi - else - show_help - fi - if [ -f ${output} ]; then - rm ${output} - fi - return 1 -} - -heredoc() { - read -r -d '' $1 -} - -edit_config() { - local extension="$1" - if grep -q "^//<$extension" "${LOADER}"; then - local tmpfile=$(mktemp /tmp/${1}_XXXX.js) - awk -v extb="^//<$extension" -v exte="^//>$extension" ' - $0~exte { f=0; next } - $0~extb { f=1; next } f;' ${LOADER} > "${tmpfile}" - $M_EDITOR ${tmpfile} - add_to_loader ${extension} "$(< "${tmpfile}")" - rm ${tmpfile} - else - error "$extension is not installed" - fi -} - -do_update() { - local tmploader="$(mktemp)" - local conf_orig="$(mktemp /tmp/XXXXX.orig.js)" - local conf_new="$(mktemp /tmp/XXXXX.new.js)" - while [ "$1" ]; do - extension="$1" - notify "updating ${extension}" - cp "${LOADER}" "${tmploader}" - do_install ${extension} - awk -v extb="^//<$1" -v exte="^//>$1" ' - $0~exte { f=0; next } - $0~extb { f=1; print "// THIS CONFIGURATION WILL BE SAVED"; next} f; - ' ${tmploader} > "${conf_orig}" - awk -v extb="^//<$1" -v exte="^//>$1" ' - $0~exte { f=0; next } - $0~extb { f=1; print "// THIS CONFIGURATION WILL BE DISCARDED"; next } f; - ' "${LOADER}" > "${conf_new}" - ${M_DIFF_VIEWER} "$conf_orig" "$conf_new" - add_to_loader "${extension}" "$(awk 'NR!=1' ${conf_orig})" - shift - done - rm "${tmploader}" "${conf_orig}" "${conf_new}" -} - -update() { - update_metainfo - local uptodate=1 - for ext in $(awk 'NR==FNR{a[$0]=$1; next} !a[$0] { print $1 }' "${META_INFO}" "${META_INSTALLED}"); do - if yes_no "Update ${ext}"; then - do_update ${ext} - fi - uptodate=0 - done - if [ "$uptodate" -eq 0 ]; then - notify "done" - else - notify "Up to date" - fi -} - -extensions_load() { - local l_text - heredoc l_text <<! -extensions.load("$1", { -$(get_default_config "$1") -}); -! - echo "$l_text" -} -extensions_bind() { - local l_text - heredoc l_text <<! -var config_$1 = { -$(get_default_config "$1") -}; - -extensions.bind("$1", "t${1:0:3}", { - load : true, - command : "toggle$1", - config : config_$1 -}); -! - echo "${l_text}" - -} - -add_to_loader() -{ - check_dir "$SCRIPT_DIR" - local l_config - local tmp_file=$(mktemp) - local extension="$1" - if [ "$2" ]; then - l_config="//<${extension}\n$2\n\n//>${extension}" - elif [ "$BIND" -eq 1 ]; then - l_config="//<${extension}\n$(extensions_bind $extension)\n\n//>${extension}" - else - l_config="//<${extension}\n\n$(extensions_load $extension)\n\n//>${extension}" - fi - if [ ! -f "${LOADER}" ]; then - printf "#!javascript\n\n" > "${tmp_file}" - printf "\n$l_config\n" >> "${tmp_file}" - else - awk -v extb="^//<$extension" -v exte="^//>$extension" -v config="$l_config" ' - BEGIN { f=0 } - $0~extb { f=2; print config; } - $0~exte { f=1; next } - f!=2 { print } - END { if (f==0) { print config } }' ${LOADER} > ${tmp_file} - fi - mv ${tmp_file} ${LOADER} -} - -do_install() { - install_extension "$1" - add_to_loader "$1" -} - -cl_uninstall() { - local extension="$1" - if ! grep -q "^//<$extension" "${LOADER}"; then - die "${extension} is not installed" - fi - local file="${INST_DIR}/$1" - local tmp_file=$(mktemp) - notify "uninstalling $extension" - if [ -f "${file}" ]; then - notify "removing ${file}" - rm ${file} - fi - notify "updating ${LOADER}" - awk -v extb="^//<$extension" -v exte="^//>$extension" ' - $0~exte { f=0; next } - $0~extb { f=1 } - !f; ' "$LOADER" > "${tmp_file}" - mv "${tmp_file}" "${LOADER}" - notify "updating metadata" - local l_minstall="$(mktemp)" - cp "${META_INSTALLED}" "${l_minstall}" - awk -v ext="^${extension}" '$0!~ext' "${l_minstall}" > "${META_INSTALLED}" - rm "${l_minstall}" - -} - -cl_install() { - update_metainfo - do_install "$1" - if yes_no "Edit config with ${EDITOR}"; then - edit_config "$1" - fi -} - -cl_update() { - if grep -q "^//<$1" "${LOADER}"; then - update_metainfo - do_update "$1" - elif yes_no "$1 is not installed, install it"; then - cl_install $1 - fi -} - -cl_bind() { - add_to_loader "$1" "$(extensions_bind $1)" - edit_config "$1" -} -cl_load() { - add_to_loader "$1" "$(extensions_load $1)" - edit_config "$1" -} - -show_installed() { - if [ -f "${META_INSTALLED}" ] && [ "$(awk 'END { print NR }' "${META_INSTALLED}")" -ge 0 ]; then - notify "installed extensions:" - awk '{ print " * "$1 }' "${META_INSTALLED}" - else - notify "no extensions installed" - fi -} - -show_help() -{ - cat <<! -usage: $0 [option] [argument] - -Options: - -b <extension> load extension with extensions.bind - -e <extension> edit configuration for <extension> - -i <extension> install <extension>, installing an already installed extension - will also overwrite the config for that extension - -I <extension> install <extension>, installing an already installed extension - will also overwrite the config for that extension, uses - extensions.bind instead of extensions.load - -h show this help and exit - -m update metadata - -q quiet - -l list extensions - -L <extension> load extension with extensions.load - -r <extension> uninstall <extension> - -s show installed extensions - -u update metainfo, passing -u twice will also update extensions - -U <extension> update <extension>, this will also redownload the extension - even if it doesn't need to be updated -! -} -parse_opts() { - local l_update=0; - while getopts "mL:b:i:I:r:e:uU:qlhs" opt; do - case $opt in - i) cl_install ${OPTARG};; - I) BIND=1; cl_install ${OPTARG};; - b) cl_bind ${OPTARG};; - L) cl_load ${OPTARG};; - r) cl_uninstall ${OPTARG};; - q) QUIET=1;; - u) update;; - U) cl_update ${OPTARG};; - m) update_metainfo;; - l) list;; - e) edit_config ${OPTARG};; - s) show_installed ${OPTARG};; - h) show_help;; - ?) show_help;; - esac - done -} - -check_requirements() -{ - if ! type -p "${M_EDITOR}" > /dev/null; then - die "Editor $EDITOR was not found, please set EDITOR environment variable to a appropriate value" - fi - if ! type -p "${M_DIFF_VIEWER}" > /dev/null; then - die "Diffviewer $M_DIFF_VIEWER was not found, please set DIFF_VIEWER environment variable to a appropriate value" - fi -} -if [ ! "$1" ]; then - error "missing argument" - show_help - exit 1 -fi -check_requirements -parse_opts "$@" |