dip

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

commit 935fcdfb9e8008900308f3dcfe1c300e095c1491
parent c5b282b4f4558ca1f0df492a1f4078fcdc8b5d6a
Author: rsiddharth <s@ricketyspace.net>
Date:   Mon,  6 Aug 2018 03:32:59 +0000

usr/local/bin: Add tball.

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

diff --git a/usr/local/bin/tball b/usr/local/bin/tball @@ -0,0 +1,111 @@ +#!/bin/bash +# +# License: CC0-1.0 +# +# Usage: +# +# $ tball /path/to/tball-info.txt +# +# + +parse_opts() { + export TBFILE=$1 + if [ -z $TBFILE ]; then + echo 'Info file not passed! Exiting.' + exit 1 + fi + if [ ! -f $TBFILE ]; then + echo $TBFILE 'does not exist! Exiting.' + exit 1 + fi +} + +parse_src() { + if [ ! -d $1 ] && [ ! -f $1 ]; then + echo 'Source "'$1'" does not exists' + exit 1 + fi + + return 0 +} + +parse_bdest() { + if [ ! -d $1 ]; then + echo 'Making '$1' ...' + mkdir -p $1 + [[ $? == 1 ]] && exit 1 + fi + + return 0 +} + +setup_dest() { + export DEST=$1/$2 + + if [ ! -d $DEST ]; then + echo 'Making '$DEST' ...' + mkdir -p $DEST + [[ $? == 1 ]] && exit 1 + fi +} + +clean_tmp() { + echo 'Cleaning up... '$1 + rm -rf $1 +} + +tb() { + SRC=${1/#\~/$HOME} + BDEST=${2/#\~/$HOME} + + parse_src $SRC + parse_bdest $BDEST + + # Get basename. + BN=$(basename $SRC) + + # Setup destination dir. + setup_dest $BDEST $BN + + # SUFFIX (current unix time) + SUFFIX=$(date '+%s') + + # Make temp dir. + TMP_DIR=$(mktemp -d --suffix=-tball) + [[ $? == 1 ]] && exit 1 + + # Make ball name + BALL=$TMP_DIR/$BN.$SUFFIX.tar.xz + + # Make ball. + echo 'Making '$BALL'...' + if [ ${SRC: -6} == 'tar.xz' ]; then + cp $SRC $BALL # SRC is already a tar ball + else + tar cJvf $BALL $SRC + fi + [[ $? == 1 ]] && clean_tmp $TMP_DIR && exit 1 + + # Enchive ball. + echo 'Enchiving '$BALL'...' + enchive archive $BALL + [[ $? == 1 ]] && clean_tmp $TMP_DIR && exit 1 + + # Drop ball in destination. + echo 'rsyncing '$BN'.enchive to '$DEST'...' + rsync $BALL.enchive $DEST + [[ $? == 1 ]] && clean_tmp $TMP_DIR && exit 1 + + # Store suffix + echo $SUFFIX >> $DEST/.suffixes + + # Clean up + clean_tmp $TMP_DIR +} + +parse_opts $1 + +# Tarball 'em all up +while read -r line; do + tb $line +done <$TBFILE