# mrxvt is no longer in Mint 19 repo...
# deb_packages=xclip mrxvt zenity graphviz
deb_packages=curl xclip zenity graphviz gnome-terminal mate-terminal konsole
rpm_packages=${deb_packages}
brew_packages=pygobject3 graphviz dockutil wezterm
# wezterm is a funny one, but it's the only terminal emulator I could find for macOS that supports $SHELL.
# It's written in Rust, and seems pretty fast.
# It's not in homebrew, 2021-06-04, so download it and add it to applications

# default: lint go2 run
default: install

mac-app-py2app:
	# Note that this is a hack that gets things going for now, but does not produce a redistributable hcm.app
	/usr/local/bin/python3 setup.py py2app
	# DYLD_LIBRARY_PATH=/Applications/hcm.app/Contents/Frameworks /Applications/hcm.app/Contents/MacOS/hcm --gui
	mkdir -p /Applications/hcm.app/Contents/MacOS
	cp hcm-hack /Applications/hcm.app/Contents/MacOS/.
	cd /Applications/hcm.app/Contents/MacOS && ln -sf hcm-hack hcm

mac-app-pyinstaller:
	python3 -m pip install pyinstaller
	pyinstaller -y hcm.py
	(cd dist && tar cf - hcm.app) | (cd /Applications && tar xfp -)
	cp hcm-hack /Applications/hcm.app/Contents/MacOS/.
	# (cd /Applications/hcm.app/Contents/MacOS && ln -sf hcm-hack hcm)
	# /Applications/hcm.app/Contents/MacOS/hcm --gui

go3:
	./hcm-term --host-path zareason2

go2:
	./hcm.py --dump-host-groups
	./hcm.py --dump-command-groups
	./hcm.py --list-host-group "Dan's from Dan's"
	./hcm.py --list-host-groups

lint:
	# We ignore "too many lines in module", because: I want this relatively self-contained, and pylint's C0302 wasn't overriding it
	# We ignore "Wrong hanging indentation before block" because I don't know how to make both pylint and pymode happy
	#    at the same time.
	./this-pylint \
		--which-3 /usr/local/bin/pylint \
		--which-2 None \
		--ignore-message '.*Too many lines in module' \
		--ignore-message '.*Wrong hanging indentation before block' \
		--to-pylint hcm.py

run:
	# ./hcm --dump-host-group "Dan's from Dan's"
	./hcm.py --gui

install:
	# Linux - deb-based and rpm-based
	if [ $$(uname -s) = Linux ]; then make linux-install; fi
	# macOS
	if [ $$(uname -s) = Darwin ]; then make darwin-install; fi

linux-install: linux-icon
	./install-package --deb-packages "${deb_packages}" --rpm-packages "${rpm_packages}"
	mkdir -p ~/bin
	./install-file --file bashquote.py --directory /usr/local/lib/.
	cd terminal-window && make install
	cp s hcm hcm.py hcm-term hcm-cron hcm-populate-commands logging-stamp pypty script-replay deep-ssh alert-cmd xname ~/bin/.
	./hcm-populate-commands
	./hcm-sample-hosts
	if ! which wezterm > /dev/null 2>&1 && which apt > /dev/null 2>&1; then sudo ./install-wezterm; fi
	./install-file --file wezterm-wrapper --directory /usr/local/bin/.
	cp .wezterm.lua ~/.
	make summary

darwin-install: darwin-install-by-appify
	./install-package --brew-packages "${brew_packages}"
	mkdir -p ~/bin
	cd terminal-window && make install
	./install-file --file bashquote.py --directory /usr/local/lib/.
	cp s hcm hcm.py hcm-term hcm-cron hcm-populate-commands logging-stamp pypty script-replay deep-ssh alert-cmd xname wezterm-wrapper /usr/local/bin/.
	cp .wezterm.lua ~/.
	./hcm-populate-commands
	./hcm-sample-hosts

darwin-install-by-appify: darwin-icon
	cd mactools && make install
	rm -rf HCM.app
	appify --script hcm --name HCM --icons icon/hcm-icon.icns
	rm -rf /Applications/HCM.app
	tar cf - HCM.app | (cd /Applications && tar xfp -)
	
darwin-install-by-pyinstaller: darwin-icon
	make mac-app-pyinstaller
	make summary

linux-icon:
	./install-package --brew-packages "${brew_packages}"
	cd icon && make linux-install

darwin-icon:
	./install-package --brew-packages "${brew_packages}"
	cd icon && make darwin-install

summary:
	@echo
	@echo
	@echo "You now have a bunch of commands, and (at least) 2 hosts: localhost and $$(hostname)." | fmt
	@echo
	@echo "See ./hcm-sample-hosts for how to add more host groups and hosts." | fmt
	@echo
	@( \
	echo "You can also use 'hcm-term --host-path remote.host.com' to get multitab ssh with logging,"; \
	echo "but without first needing to predefine a host - nice when you're in a hurry."; \
	) | fmt
	@echo
	@echo "See ./hcm-populate-commands for how to add more command groups and commands." | fmt
	@echo
	@( \
	echo "Your tty logs, if you choose to use 'terminal-window: fancy' (or other fancy), should appear"; \
	echo "in ~/.hcm/logs/<year>/<month>/<day>/* ."; \
	) | fmt

install-desktop:
	if ! fgrep -q '[ "$$DRS_BASHRC_RUN" = "" ] && source ~/drs-bashrc' ~/.bashrc; then echo '[ "$$DRS_BASHRC_RUN" = "" ] && source ~/drs-bashrc' >> ~/.bashrc; fi

clean:
	rm -f *.pyc
	rm -rf __pycache__ .ropeproject build dist
	cd icon && make clean
	rm -rf HCM.app
	rm -rf hcm-icon.icns
	rm -rf hcm-icon.iconset
	rm -rf icon/hcm-icon.icns
	rm -rf icon/hcm-icon.iconset
	rm -rf .eggs
	rm -f hcm.spec