browser-home-profile/init.zsh: Converted specific browser to generic browser profile

This commit is contained in:
tokiclover 2015-05-14 22:35:49 +02:00
parent 8fa34532c2
commit 522c723356

View file

@ -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 # 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 <tokiclover@gmail.com> Exp $ # $Aythor: (c) 2012-2015 -tclover <tokiclover@gmail.com> Exp $
# $License: MIT (or 2-clause/new/simplified BSD) 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 $
# #
#
# @FUNCTION: Print error message to stderr
#
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} ${@}"
}
#
# @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 # Initialize the temporary directory with an anonymous function
# #
function { function {
local compressor profile tmpdir zsh_hook local PROFILE browser name=bhp 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 zstyle -s ':prezto:module:BHP' browser 'browser'
function die { zstyle -s ':prezto:module:BHP' profile 'profile'
local ret=$? zstyle -s ':prezto:module:BHP' compressor 'compressor'
print -P " %F{red}%2x: %F{yellow}%U%I%u:%f $argv" >&2 zstyle -b ':prezto:module:BHP' zsh-hook 'zsh_hook'
return $ret zstyle -s ':prezto:module:BHP' tmpdir 'tmpdir'
}
if (( $+compressor )) {
: ${compressor:=lz4 -1 -}
zstyle ':prezto:module:FHP' compressor "$compressor"
}
setopt LOCAL_OPTIONS EXTENDED_GLOB setopt LOCAL_OPTIONS EXTENDED_GLOB
: ${profile:=$(print $HOME/.mozilla/firefox/*.default(/N:t))}
if [[ -z $profile ]] { #
die "Null firefox home profile" # Set up web-browser if any
return 1 #
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;;
} }
case $profile { for set ("mozilla:${MOZ_BROWSERS}" "config:${BROWSERS}")
(*.default) ;; for brs (${=set#*:}) {
(*) profile+=.default;; set=${set%:*}
for dir (${HOME}/.${set}/*${brs}*(N/))
if [[ -d ${dir} ]] {
BROWSER=${brs} PROFILE=${set}/${brs}
return
} }
zstyle ':prezto:module:FHP' profile "$profile" }
local ext=.tar.$compressor[(w)1] return 111
} "${browser:-$BROWSER}"
if (( ${?} != 0 )) {
pr-error "No web-browser found."
return 112
}
#
# 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
}
;;
}
: ${compressor:=lz4 -1 -}
: ${profile:=${PROFILE:t}}
: ${tmpdir:=${TMPDIR:-/tmp/$USER}} : ${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 autoload -Uz add-zsh-hook
add-zsh-hook zshexit fhp add-zsh-hook zshexit fhp
} }
[[ -d "$tmpdir" ]] || mkdir -p -m 1700 "$TMPDIR" || [[ -d ${tmpdir} ]] || mkdir -p -m 1700 ${tmpdir} ||
{ die "no suitable directory found"; return 2; } { pr-error "No suitable directory found"; return 114; }
local c dir mktmp=checkpath DIR local char dir DIR
for dir ("$HOME"/.{,cache/}mozilla/firefox/$profile) { for dir (${HOME}/.${PROFILE} ${HOME}/.cache/${PROFILE#config/}) {
grep -q "$dir" /proc/mounts && continue [[ -d ${dir} ]] || continue
pushd -q "$dir:h" || continue grep -q ${dir} /proc/mounts && continue
if [[ ! -f "$profile$ext" ]] || [[ ! -f "$profile.old$ext" ]] { pr-begin "Setting up directory..."
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 popd -q
case "$dir" { case ${dir} {
(*.cache/*) c=c;; (*.cache/*) char=c;;
(*) c=p;; (*) char=p;;
} }
(( $+commands[mktemp] )) && mktmp=$commands[mktemp] DIR=$(mktmp -p ${tmpdir} -d bh${char}-XXXXXX)
DIR=$($mktmp -p "$tmpdir" -d fh${c}-XXXXXX) sudo mount --bind ${DIR} ${dir} ||
sudo mount --bind "$DIR" "$dir" || pr-error "Failed to mount ${DIR}"
{ die "failed to mount $DIR"; continue; } pr-end ${?}
} }
# #
# Finaly, start the firefox home profile # Finaly, decompress the browser-home-profile
# #
#(( $+functions[fhp] )) || autoload -Uz fhp #(( ${+functions[fhp]} )) || autoload -Uz fhp
if zstyle -t ':prezto:module:FHP' decompress; then if zstyle -t ':prezto:module:BHP' decompress; then
fhp bhp
fi fi
} }