<commit_before>#!/bin/zsh
#
# $Id: fhp.zsh,v 2.0 2014/08/31 21:09:26 -tclover Exp $
# $License: MIT (or 2-clause/new/simplified BSD)  Exp $
#
# @DESCRIPTION: set firefox profile dir to tmpfs or zram backed fs
# @USAGE: [OPTIONS] [profile]
# @OPTIONS: [-h|--help] [-c|--comp 'gzip -1']
# @DESCRIPTION: set compressor, default to lz4
#
# And maybe something like: */30 * * * * $USER ~/scr/fhp.zsh
# in cron job to keep track of changes is necessary.
# lz4 compressed is required, or else, use -c|-comp 'lzop -1'

# @ENV_VARIABLE: FHP
# @DESCRIPTION: Firefox profile dir to handle
# @EXEMPLE: FHP=abc123
# which correspond to '~/.mozilla/firefox/$FHP.default' profile

# @ENV_VARIABLE: ZRAMDIR:=/mnt/zram
# @DESCRIPTION: Zram block device backed FS to use for firefox profile

# @ENV_VARIABLE: TMPDIR:-/tmp/.private/$USER
# @DESCRIPTION: tmpfs directory to use instead of zram fs backed fs
# you should have this one already, just put it to tmpfs with something like:
# /etc/fstab: tmp	/tmp	tmpfs	mode=1777,size=256M,noatime	0 0

function fhp {

# define a little helper to handle errors
function die {
	local ret=$?
	print -P "%F{red}*%f ${(%):-%1x}: $@" >&2
	return $ret
}

# use an anonymous function to initialize
function {
	comp="lz4 -1 -"
	while [[ $# > 0 ]]
	case $1 in
		(-h|--help)
			print "usage: fhp [-c|--comp 'lzop -1'] [profile]"
			return;;
		(-c|--comp)
			comp=${2:-$comp}
			shift 2;;
		(*) break;;
	esac

	local fhp=${1:-$FHP}
:	${fhp:=${$(print ~/.mozzila/firefox/*.default(/) 2>/dev/null):t}}
	[[ $fhp ]] || die "no firefox profile dir found"
	[[ ${fhp%.default} == $fhp ]] && fhp+=.default
	local ext=${comp[(w)1]}

:	${FHPDIR:=~/.mozilla/firefox/$fhp}
:	${TMPDIR:=/tmp/.private/$USER}
	[[ $ZRAMDIR]] || [[ -d $TMPDIR ]] || mkdir -p -m1700 $TMPDIR

	grep -q $FHPDIR /proc/mounts && return
	
	if [[ ! -f $FHPDIR.tar.$ext ]] || [[ ! -f $FHPDIR.old.tar.$ext ]] {
		pushd -q $FHPDIR:h || die
		tar -Ocp $fhp | ${=comp} $fhp.tar.$ext  ||
		die "failed to pack a new tarball"
		popd -q
	}

	local mnt
	[[ -n $ZRAMDIR ]] && mnt=$(mktemp -d $ZRAMDIR/fhp-XXXXXX) ||
        mnt=$(mktemp -d $TMPDIR/fhp-XXXXXX)
	sudo mount --bind $FHPDIR $mnt || die "failed to mount $mnt"
 } $@
 
 	# and finaly maintain firefox home profile
 	local dir=$FHPDIR:h ext=$comp[(w)1] fhp=$FHPDIR:t
	local tbl=$fhp.tar.$ext otb=$fhp.old.tar.$ext
	
	pushd -q $dir
	if [[ -f $fhp/.unpacked ]] {
		mv -f $tbl $otb || die "failed to override the old tarball"
		tar -X $fhp/.unpacked -Ocp $fhp | ${=comp} $tbl ||
		die "failed to repack a new tarball"
	} else {
		local decomp=$comp[(w)1]
		if [[ -f $tbl ]] {
			$decomp -cd $tbl | tar -xp && touch $fhp/.unpacked ||
			die "failed to unpack the profile"
		} elif [[ -f $otb ]] {
			$decomp -cd $otb | tar -xp && touch $fhp/.unpacked ||
			die "failed to unpack the profile"
		} else { die "no tarball found" }
	}
	popd -q
}

fhp $@

# vim:fenc=utf-8:ft=zsh:ci:pi:sts=0:sw=4:ts=4:
<commit_msg>fhp.zsh: update: cleaned up the code with the previous die merge<commit_after>#!/bin/zsh
#
# $Id: fhp.zsh,v 2.0 2014/08/31 21:09:26 -tclover Exp $
# $License: MIT (or 2-clause/new/simplified BSD)  Exp $
#
# @DESCRIPTION: set firefox profile dir to tmpfs or zram backed fs
# @USAGE: [OPTIONS] [profile]
# @OPTIONS: [-h|--help] [-c|--comp 'gzip -1'] [-z|--zshexit-hook]
# @DESCRIPTION: set compressor, default to lz4
#
# And maybe something like: */30 * * * * $USER /path/to/fhp.zsh
# in cron job to keep track of changes is necessary.
# lz4 compressor is required, or else, use -c|--comp 'lzop -1'

# @ENV_VARIABLE: FHP
# @DESCRIPTION: Firefox profile dir to handle
# @EXEMPLE: FHP=abc123
# which correspond to '~/.mozilla/firefox/$FHP.default' profile

# @ENV_VARIABLE: ZRAMDIR
# @DESCRIPTION: Zram block device backed FS to use for firefox profile

# @ENV_VARIABLE: TMPDIR:-/tmp/.private/$USER
# @DESCRIPTION: tmpfs directory to use instead of zram fs backed fs
# you should have this one already, just put it to tmpfs with something like:
# /etc/fstab: tmp	/tmp	tmpfs	mode=1777,size=256M,noatime	0 0

function fhp {
	local comp ext fhp FHPDIR
# define a little helper to handle errors
function die {
	local ret=$?
	print -P "%F{red}*%f ${(%):-%1x}: $@" >&2
	return $ret
}

# use an anonymous function to initialize
function {
	local usage='cat <<-EHO
usage: fhp [options] [firefox-profile]
  -c|--comp "lzop -1"  set lzop comprssor instead of lz4
  -z|--zshexit-hook    add fhp function to zshexit hook
  -h|--help            print this help message and exit
EOH
return'

	while [[ $# > 0 ]]
	case $1 in
		(-h|--help)
			${=usage};;
		(-c|--comp)
			comp=$2
			shift 2;;
		(-z|--zshexit-hook)
			autoload -Uz add-zsh-hook
			add-zsh-hook zshexit fhp;;
		(*) break;;
	esac

:	${comp:="lz4 -1"}
:	${ext=$comp[(w)1]}
:	${fhp:=${1:-${FHP:-${$(print ~/.mozzila/firefox/*.default(/) 2>/dev/null):t}}}}
	[[ $fhp ]] || die "no firefox profile dir found"
	[[ ${fhp%.default} == $fhp ]] && fhp+=.default
:	${FHPDIR:=~/.mozilla/firefox/$fhp}
:	${TMPDIR:=/tmp/.private/$USER}

	[[ $ZRAMDIR ]] || [[ -d $TMPDIR ]] || ( mkdir -p -m1700 $TMPDIR || die )

	grep -q $FHPDIR /proc/mounts && return
	
	if [[ ! -f $FHPDIR.tar.$ext ]] || [[ ! -f $FHPDIR.old.tar.$ext ]] {
		pushd -q $FHPDIR:h || die
		tar -Ocp $fhp | ${=comp} $fhp.tar.$ext  ||
		die "failed to pack a new tarball"
		popd -q
	}

	local mnt
	[[ -n $ZRAMDIR ]] && mnt=$(mktemp -d $ZRAMDIR/fhp-XXXXXX) ||
        mnt=$(mktemp -d $TMPDIR/fhp-XXXXXX)
	sudo mount --bind $FHPDIR $mnt || die "failed to mount $mnt"
 } $@
 
 	# and finaly maintain firefox home profile
 	local dir=$FHPDIR:h tbl=$fhp.tar.$ext otb=$fhp.old.tar.$ext
	
	pushd -q $dir
	if [[ -f $fhp/.unpacked ]] {
		mv -f $tbl $otb || die "failed to override the old tarball"
		tar -X $fhp/.unpacked -Ocp $fhp | ${=comp} $tbl ||
		die "failed to repack a new tarball"
	} else {
		local decomp=$comp[(w)1]
		if [[ -f $tbl ]] {
			$decomp -cd $tbl | tar -xp && touch $fhp/.unpacked ||
			die "failed to unpack the profile"
		} elif [[ -f $otb ]] {
			$decomp -cd $otb | tar -xp && touch $fhp/.unpacked ||
			die "failed to unpack the profile"
		} else { die "no tarball found" }
	}
	popd -q
}

fhp $@

# vim:fenc=utf-8:ft=zsh:ci:pi:sts=0:sw=4:ts=4:

{'fraction_non_alphanumeric': 0.18773264282246319, 'fraction_numerical': 0.01844958731186276, 'mean_word_length': 3.247422680412371, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '167256', 'n_tokens_mistral': 2723, 'n_tokens_neox': 2530, 'n_words': 721}