1
0
Fork 0

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

pull/890/head
tokiclover 10 years ago
parent 8fa34532c2
commit 522c723356

@ -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 $
# #
# #
# Initialize the temporary directory with an anonymous function # @FUNCTION: Print error message to stderr
# #
function { pr-error()
local compressor profile tmpdir zsh_hook {
zstyle -s ':prezto:module:FHP' profile 'profile' local PFX=${name:+" %F{magenta}${name}:"}
zstyle -s ':prezto:module:FHP' compressor 'compressor' print -P${PR_EOL:+n} "${PR_EOL:+\n} %B%F{red}*${PFX}%b%f ${@}" >&2
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: Print info message to stdout
function die { #
local ret=$? pr-info()
print -P " %F{red}%2x: %F{yellow}%U%I%u:%f $argv" >&2 {
return $ret 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 -} # @FUNCTION: Print end message to stdout
zstyle ':prezto:module:FHP' compressor "$compressor" #
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 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;;
}
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) ;; # Handle (Mozilla) specific profiles
(*) profile+=.default;; #
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}} : ${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 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 pushd -q ${dir:h} || continue
for dir ("$HOME"/.{,cache/}mozilla/firefox/$profile) { if [[ ! -f ${profile}${ext} ]] || [[ ! -f ${profile}.old${ext} ]] {
grep -q "$dir" /proc/mounts && continue tar -Ocp ${profile} | ${=compressor} ${profile}${ext} ||
pushd -q "$dir:h" || continue { pr-error "Failed to pack a tarball"; continue; }
if [[ ! -f "$profile$ext" ]] || [[ ! -f "$profile.old$ext" ]] {
tar -Ocp $profile | $=compressor $profile$ext ||
{ die "failed to pack a new 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
} }

Loading…
Cancel
Save