diff --git a/modules/browser-home-profile/init.zsh b/modules/browser-home-profile/init.zsh index 47f927bc..ea5dd53c 100644 --- a/modules/browser-home-profile/init.zsh +++ b/modules/browser-home-profile/init.zsh @@ -1,83 +1,204 @@ # -# Firefox-Home-Profile (fhp) maintains profile & associated +# Browser-Home-Profile (bhp) maintains profile & associated # cache directory in a tmpfs (or zram backed) filesystem # -# $Header: firefox-home-profile/init.zsh Exp $ +# $Header: browser-home-profile/init.zsh Exp $ # $Aythor: (c) 2012-2015 -tclover Exp $ # $License: MIT (or 2-clause/new/simplified BSD) Exp $ -# $Version: 4.0 2015/05/05 21:09:26 Exp $ +# $Version: 1.0 2015/05/14 21:09:26 Exp $ # # -# Initialize the temporary directory with an anonymous function +# @FUNCTION: Print error message to stderr # -function { - local compressor profile tmpdir zsh_hook - zstyle -s ':prezto:module:FHP' profile 'profile' - zstyle -s ':prezto:module:FHP' compressor 'compressor' - zstyle -b ':prezto:module:FHP' zsh-hook 'zsh_hook' - zstyle -s ':prezto:module:FHP' tmpdir 'tmpdir' - - # Define a little helper to handle fatal errors - function die { - local ret=$? - print -P " %F{red}%2x: %F{yellow}%U%I%u:%f $argv" >&2 - return $ret +pr-error() +{ + local PFX=${name:+" %F{magenta}${name}:"} + print -P${PR_EOL:+n} "${PR_EOL:+\n} %B%F{red}*${PFX}%b%f ${@}" >&2 +} + +# +# @FUNCTION: Print info message to stdout +# +pr-info() +{ + local PFX=${name:+" %F{yellow}${name}:"} + print -P${PR_EOL:+n} "${PR_EOL:+\n} %B%F{blue}*${PFX}%b%f ${@}" +} + +# +# @FUNCTION: Print warn message to stdout +# +pr-warn() +{ + local PFX=${name:+" %F{red}${name}:"} + print -P${PR_EOL:+n} "${PR_EOL:+\n} %B%F{yellow}*${CLR_RST}${PFX}%f%b ${@}" +} + +# +# @FUNCTION: Print begin message to stdout +# +pr-begin() +{ + case ${PR_EOL} { + (0) echo;; } +: ${PR_EOL=0} + local PFX=${name:+"%B%F{magenta}[%f %F{blue}${name}%f: %F{magenta}]%f%b"} + print -P " ${PFX} ${@}" +} - if (( $+compressor )) { - : ${compressor:=lz4 -1 -} - zstyle ':prezto:module:FHP' compressor "$compressor" +# +# @FUNCTION: Print end message to stdout +# +pr-end() +{ + local SFX + case ${1:-0} { + (0) SFX="%F{blue}[%f %F{green}Ok%f %F{blue}]%f";; + (*) SFX="%F{yellow}[%f %F{red}No%f %F{yellow}]%f";; } + shift + print -P " ${@} %B${SFX}%b" + PR_EOL= +} + +# +# @FUNCTION: YES or NO helper +# +yesno() +{ + case ${1:-NO} { + (0|[Dd][Ii][Ss][Aa][Bb][Ll][Ee]|[Oo][Ff][Ff]|[Ff][Aa][Ll][Ss][Ee]|[Nn][Oo]) + return 1;; + (1|[Ee][Nn][Aa][Bb][Ll][Ee]|[Oo][Nn]|[Tt][Rr][Uu][Ee]|[Yy][Ee][Ss]) + return 0;; + (*) + return 2;; + } +} + +# +# Set up (terminal) colors +# +if [ -t 1 ] && yesno ${COLOR:-Yes}; then + autoload colors zsh/terminfo + if (( ${terminfo[colors]} >= 8 )) { colors } +fi + +# +# Initialize the temporary directory with an anonymous function +# +function { + local PROFILE browser name=bhp compressor profile tmpdir zsh_hook + + zstyle -s ':prezto:module:BHP' browser 'browser' + zstyle -s ':prezto:module:BHP' profile 'profile' + zstyle -s ':prezto:module:BHP' compressor 'compressor' + zstyle -b ':prezto:module:BHP' zsh-hook 'zsh_hook' + zstyle -s ':prezto:module:BHP' tmpdir 'tmpdir' + setopt LOCAL_OPTIONS EXTENDED_GLOB -: ${profile:=$(print $HOME/.mozilla/firefox/*.default(/N:t))} - if [[ -z $profile ]] { - die "Null firefox home profile" - return 1 + + # + # Set up web-browser if any + # + function { + local BROWSERS MOZ_BROWSERS set brs dir + MOZ_BROWSERS='aurora firefox icecat seamonkey' + BROWSERS='conkeror chrom epiphany midory opera otter qupzilla vivaldi' + + case ${1} { + (*aurora|firefox*|icecat|seamonkey) + BROWSER=${1} PROFILE=mozilla/${1}; return;; + (conkeror*|*chrom*|epiphany|midory|opera*|otter*|qupzilla|vivaldi*) + BROWSER=${1} PROFILE=config/${1} ; return;; + } + + for set ("mozilla:${MOZ_BROWSERS}" "config:${BROWSERS}") + for brs (${=set#*:}) { + set=${set%:*} + for dir (${HOME}/.${set}/*${brs}*(N/)) + if [[ -d ${dir} ]] { + BROWSER=${brs} PROFILE=${set}/${brs} + return + } + } + return 111 + } "${browser:-$BROWSER}" + + if (( ${?} != 0 )) { + pr-error "No web-browser found." + return 112 } - case $profile { - (*.default) ;; - (*) profile+=.default;; + # + # Handle (Mozilla) specific profiles + # + case ${PROFILE} { + (mozilla*) + function { + if [[ -n ${1} ]] && [[ -d ${HOME}/.${PROFILE}/${1} ]] { + PROFILE=${PROFILE}/${1} + return + } + PROFILE="${PROFILE}/$(sed -nre "s|^[Pp]ath=(.*$)|\1|p" \ + ${HOME}/.${PROFILE}/profiles.ini)" + [[ -n ${PROFILE} ]] && [[ -d ${HOME}/.${PROFILE} ]] + } "${profile}" + + if (( ${?} != 0 )) { + pr-error "No firefox profile directory found" + return 113 + } + ;; } - zstyle ':prezto:module:FHP' profile "$profile" - local ext=.tar.$compressor[(w)1] + +: ${compressor:=lz4 -1 -} +: ${profile:=${PROFILE:t}} : ${tmpdir:=${TMPDIR:-/tmp/$USER}} + local ext=.tar.${compressor[(w)1]} + zstyle ':prezto:module:BHP' compressor ${compressor} + zstyle ':prezto:module:BHP' browser ${browser} + zstyle ':prezto:module:BHP' PROFILE ${PROFILE} - if (( $zsh_hook )) { + if (( ${zsh_hook} )) { autoload -Uz add-zsh-hook add-zsh-hook zshexit fhp } - [[ -d "$tmpdir" ]] || mkdir -p -m 1700 "$TMPDIR" || - { die "no suitable directory found"; return 2; } + [[ -d ${tmpdir} ]] || mkdir -p -m 1700 ${tmpdir} || + { pr-error "No suitable directory found"; return 114; } + + local char dir DIR + for dir (${HOME}/.${PROFILE} ${HOME}/.cache/${PROFILE#config/}) { + [[ -d ${dir} ]] || continue + grep -q ${dir} /proc/mounts && continue + pr-begin "Setting up directory..." - local c dir mktmp=checkpath DIR - for dir ("$HOME"/.{,cache/}mozilla/firefox/$profile) { - grep -q "$dir" /proc/mounts && continue - pushd -q "$dir:h" || continue - if [[ ! -f "$profile$ext" ]] || [[ ! -f "$profile.old$ext" ]] { - tar -Ocp $profile | $=compressor $profile$ext || - { die "failed to pack a new tarball"; continue; } + pushd -q ${dir:h} || continue + if [[ ! -f ${profile}${ext} ]] || [[ ! -f ${profile}.old${ext} ]] { + tar -Ocp ${profile} | ${=compressor} ${profile}${ext} || + { pr-error "Failed to pack a tarball"; continue; } } popd -q - case "$dir" { - (*.cache/*) c=c;; - (*) c=p;; + case ${dir} { + (*.cache/*) char=c;; + (*) char=p;; } - (( $+commands[mktemp] )) && mktmp=$commands[mktemp] - DIR=$($mktmp -p "$tmpdir" -d fh${c}-XXXXXX) - sudo mount --bind "$DIR" "$dir" || - { die "failed to mount $DIR"; continue; } + DIR=$(mktmp -p ${tmpdir} -d bh${char}-XXXXXX) + sudo mount --bind ${DIR} ${dir} || + pr-error "Failed to mount ${DIR}" + pr-end ${?} } # - # Finaly, start the firefox home profile + # Finaly, decompress the browser-home-profile # - #(( $+functions[fhp] )) || autoload -Uz fhp - if zstyle -t ':prezto:module:FHP' decompress; then - fhp + #(( ${+functions[fhp]} )) || autoload -Uz fhp + if zstyle -t ':prezto:module:BHP' decompress; then + bhp fi }