dip

old school recipes for janitorial snafu.
git clone git://git.ricketyspace.net/dip.git
Log | Files | Refs

commit bd866992495869ffb8ec67decaca60223ee3df5d
parent b575696294db56788cf5ca385da6bd9a3a10fddb
Author: rsiddharth <s@ricketyspace.net>
Date:   Tue, 11 Sep 2018 00:58:46 +0000

usr:  tball: Add clean_ol_tbs.

* usr/local/bin/tball (clean_ol_tbs): New function.

Diffstat:
usr/local/bin/tball | 60++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 60 insertions(+), 0 deletions(-)

diff --git a/usr/local/bin/tball b/usr/local/bin/tball @@ -99,6 +99,66 @@ should_tb() { fi } + +clean_ol_tbs() { + local D=$1 + local S=$1/.suffixes + local MAX=$2 # TODO: check MAX's value. + + # Check if suffixes file exists. + if [ ! -f $S ]; then + echo 'Cannot remove old tballs at '$1 \ + ': .suffixes not found' + return 0 + fi + + # Get total timestamps. + local TOTT=$(wc -l $S | cut -f 1 -d ' ') + + if [ $TOTT -lt $MAX ]; then + echo 'No old tballs to clean up [0]' + return 0 + fi + + # Get number of timestamps to remove. + local RM=$(($TOTT-$MAX)) + if [ $RM -lt 1 ]; then + echo 'No old tballs to clean up [1]' + return 0 + fi + + # Make temp dir. + local TMP_DIR=$(mktemp -d --suffix=-clean_tb) + [[ $? == 1 ]] && exit 1 + + # Get timestamps to remove. + local RMT=$TMP_DIR/rmt + $(head -n $RM $S > $RMT) + [[ $? == 1 ]] && exit 1 + + # Get timestamps to keep. + local KT=$TMP_DIR/kt + $(tail -n $MAX $S > $KT) + [[ $? == 1 ]] && exit 1 + + # Get basename. + local BN=$(basename $D) + + echo 'Removing old tballs...' + while read -r ts; do + rm_file $D/$BN.$ts.tar.xz.enchive + done <$RMT + + # Update suffixes + mv $KT $S + [[ $? == 1 ]] && exit 1 + + # Clean up tmp dir. + rm -rf $TMP_DIR + + return 0 +} + tb() { local SRC=${1/#\~/$HOME} local BDEST=${2/#\~/$HOME}