shithub: bin.rc

ref: 49c1ea83f5e42094b933e462eee157063a0ae690
dir: /bin/bag/

View raw version
#!/bin/rc -e
rfork en
fn usage {>[1=2] echo 'usage:' $usage && exit 'usage'}
usage='bag [-f] file ...'

fn bag {
	name = $1
	h = `{sha1sum $name | sed 's/(..)(....).*/\1 \2/'}
	b = $h(1)
	f = $b^/^$h(2)
	if(~ $force yes || ! test -e $bag/$f){
		mkdir -p $bag/$b
		fcp $name $bag/$f
	}
	if not
		>[1=2] echo already bagged
	echo $bagurl/$f
}

if(~ $#bag 0)
	bag = /n/bag
if(~ $#bagurl 0)
	bagurl = http://a-b.xyz
force = no
while(~ $1 -* && ! ~ $1 --){
	switch($1){
	case -f
		force = yes
	case *
		usage
	}
	shift
}
if(~ $1 --)
	shift
files = ($*)
if(~ $#files 0){
	ramfs -u
	cat /fd/0 >/tmp/bag.tmp
	files = (/tmp/bag.tmp)
}
9fs bag
for(f in $files)
	bag $f