JUNKFILES = .bash_history .irb_history .pip .ScanSnap .nbems .fldigi .cpan
JUNKFILES += .gdb_history .mysql_history .sqlite_history
BREWPACKAGES := mosh duplicity pv offlineimap wget nmap tor torsocks
YYYYMM := $(shell date +%Y%m)

HACKSREPO := ~/.paths/sneak-scratch/dev/hacks/

NO_COLOR = \033[0m
O1_COLOR = \033[0;01m
O2_COLOR = \033[32;01m

PREFIX = "$(O2_COLOR)==>$(O1_COLOR)"
SUFFIX = "$(NO_COLOR)"

default: routine

# FIXME make this do a local imap download too
routine: clean databackup

backup: clean mailoffsite databackup

dvbackup:
	@echo $(PREFIX) $@ $(SUFFIX)
	cd ~/Documents/datavibe/backup && make

imapbackup:
	offlineimap
	rsync -e "ssh -o Compression=no -x" \
		-avPhzy --delete-after sneak@datavibe.net:.maildir/ \
		$(HOME)/Documents/Archival/mail/sneak.datavibe.net.maildir/

mailoffsite: imapbackup
	rsync -e "ssh -o Compression=no -x" \
		-avPhzy --delete-after $(HOME)/Documents/Archival/mail/ \
		sneak@datavibe.net:.mailbackup/
	tar -xvf $(HOME)/Documents/Archival/mail/jp.eeqj.com | gzip > \
		$(HOME)/Documents/Dropbox/eeqj/archives/mail.tgz.new && \
		mv $(HOME)/Documents/Dropbox/eeqj/archives/mail.tgz.new \
			$(HOME)/Documents/Dropbox/eeqj/archives/mail.tgz

databackup:
	mkdir -p $(HOME)/Library/misc
	brew list > $(HOME)/Library/misc/brewinstalled.txt
	brew cask list > $(HOME)/Library/misc/brew-cask-installed.txt
	~/dev/hacks/bin/backup.command

cleanup:
	-mkdir -p $(HOME)/Documents/$(YYYYMM)
	-mv $(HOME)/Desktop/* $(HOME)/Documents/$(YYYYMM)

clean: cleanup
	@echo $(PREFIX) $@ $(SUFFIX)
	@-rm -rf ~/.tmp/*
	@-rm -rf ~/.Trash/*
	@-rm -rf $(JUNKFILES)

size:
	du -sh $(HOME)

lifeboat:
	mkdir -p $(HOME)/tmp/lifeboat.$(YYYYMM)
	rsync -avP --exclude='*.pkg' $(HOME)/Documents/Secure/ \
		$(HOME)/tmp/lifeboat.$(YYYYMM)/Secure/
	rsync -avP $(HOME)/Library/ApplicationSupport/Bitcoin/wallet.dat \
		$(HOME)/tmp/lifeboat.$(YYYYMM)/wallet.dat
	tar -c $(HOME)/tmp/lifeboat.$(YYYYMM) | bzip2 | \
		gpg --symmetric -a -o $(HOME)/lifeboat.$(YYYYMM).gpg
	rm -rf $(HOME)/tmp/lifeboat.$(YYYYMM)
	cp $(HOME)/lifeboat.$(YYYYMM).gpg \
		$(HOME)/dev/eeqjcdn/sneak.datavibe.net/lifeboat/lifeboat.gpg
	cd $(HOME)/dev/eeqjcdn && make
	mv $(HOME)/lifeboat.$(YYYYMM).gpg $(HOME)/Documents/Dropbox/Backups/

verify:
	duplicity verify --exclude-globbing-filelist \
		$(HOME)/.local/etc/duplicity.exclude \
		file:///Volumes/EXTUSB01/dup/ ~

remotebackup:
	RBACKUPDEST="scp://jp.eeqj.de/backup" $(HOME)/.local/bin/backup.command

packages:
		brew install $(BREWPACKAGES)

# this copies instead of linking because the 'hacks' repo is part of the
# sneak-sync shared folder which is synced with machines off-prem
# and they are not allowed impending RCE on workstations

update:
	cat $(HACKSREPO)/homedir.makefile/Makefile > Makefile
	brew upgrade

