Makefile
author Oleksandr Gavenko <gavenkoa@gmail.com>
Mon, 01 Jan 2024 20:53:49 +0200
changeset 1039 78cdb4a057e3
parent 1032 9cf008356bdf
permissions -rw-r--r--
Create symlink from ~/.bash_profile for login shell to activate my ~/.bashrc.


################################################################
# Standard GNU Makefile settings.

SHELL = /bin/bash
.SHELLFLAGS = -xeuc
export PATH := /bin:/usr/bin:${PATH}

# Disable built in pattern rules.
MAKEFLAGS += -r
# Disable built in variables.
MAKEFLAGS += -R
# Disable built in suffix rules.
.SUFFIXES:
# Delete target file if command fails.
.DELETE_ON_ERROR:
# Default target.
.DEFAULT_GOAL = help

################################################################
# Config.

CFG_FILE := Makefile.cfg
include $(CFG_FILE)

# Override defaults.
CFG_LOCAL_FILE := Makefile.cfg.override
-include $(CFG_LOCAL_FILE)

################################################################
# Platform definition.

ifeq '' '$(HOME)'
  $(error HOME env var not set!)
endif

uname-s := $(shell uname -s)

host_os = linux
ifneq '' '$(filter CYGWIN%,$(uname-s))'
  host_os = cygwin
endif

################################################################
# Build tool definition/switches.

INSTALL_DATA := install -m 640 -D

RST2HTML = rst2html
ifeq '$(host_os)' 'cygwin'
  RST2HTML = rst2html.py
endif

################################################################
# Project dirs/files.

OVERRIDDEN_ITEMS := \
    .local/share/applications/mimeapps.list \
    .xxkbrc .xmodmaprc .stalonetrayrc \
    .inputrc .Xdefaults .xinitrc .xserverrc \
    .screenrc .tmux.conf \
    .dircolors .colordiffrc \
  .bashrc .env.bash .bash_completion .bash_completion.d .local/share/bash-completion/completions .zshrc \
  .vimrc .ssh \
    .pylintrc .pystartup .tclshrc .guile \
    .gradle/init.d/springColorLogging.gradle \
    .sqshrc .psqlrc \
    .signature .muttrc .tidy \
    .dictrc \
    .hgrc .hgignore .hgstyle .bazaar .gitignore .cvsrc .quiltrc .quiltrc-dpkg \
    .gnupg \
    .ackrc .grc \
    .mplayer \
    .glhackrc
MANUALINSTALL_ITEMS = .mc
IFNONEXIST_ITEMS = .wgetrc .subversion

RST_FILES = $(wildcard *.rst)
HTML_FILES = $(RST_FILES:.rst=.html)

# Might include my credentials.
TARBALL_MINI := skel-mini.tar.gz
# Without personal info.
TARBALL_MICRO := skel-micro.tar.gz

################################################################
# Build targets.

.PHONY: all
all:

.PHONY: dist-mini
dist-mini: $(TARBALL_MINI)

define TARBALL_MINI_DEPS
.bashrc .env.bash .bash_completion \
.inputrc .dircolors \
.gitconfig .gitignore .hgrc .hgignore .hgstyle
endef

$(TARBALL_MINI): $(TARBALL_MINI_DEPS)
	tar zcf $(TARBALL_MINI) $(TARBALL_MINI_DEPS)

.PHONY: dist-micro
dist-micro: $(TARBALL_MICRO)

define TARBALL_MICRO_DEPS
.bashrc .env.bash .bash_completion \
.gitconfig \
.inputrc .dircolors
endef

$(TARBALL_MICRO): $(TARBALL_MICRO_DEPS)
	tar zcf $(TARBALL_MICRO) $(TARBALL_MICRO_DEPS)

################################################################
# Install/uninstall targets.

# Essential rc files.
.PHONY: install-mini
.ONESHELL:
install-mini:
	$(INSTALL_DATA) -t $(HOME) ${TARBALL_MINI_DEPS}
	ln -s -f $(HOME)/.bashrc $(HOME)/.bash_profile

# Without personal info.
.PHONY: install-micro
.ONESHELL:
install-micro:
	$(INSTALL_DATA) -t $(HOME) ${TARBALL_MICRO_DEPS}
	ln -s -f $(HOME)/.bashrc $(HOME)/.bash_profile

.PHONY: install
.ONESHELL:
install:
	declare -a files
	for item in $(OVERRIDDEN_ITEMS); do
		if [[ -f $$item ]]; then
			case "$$item" in
				*/*) $(INSTALL_DATA) $$item $(HOME)/$$item ;;
				*) files+=($$item) ;;
			esac
		elif [[ -d $$item ]]; then
			find $$item -type f -exec $(INSTALL_DATA) {} $(HOME)/{} ';'
		fi
	done
	cat .gitconfig .gitconfig.extra >$(HOME)/.gitconfig
	$(INSTALL_DATA) -t $(HOME) "$${files[@]}"
	chmod 700 $(HOME)/.gnupg/
	if command -v fvwm; then
		cp -r .fvwm $(HOME)
		chmod a+x $(HOME)/.fvwm/FvwmKbdd.pl $(HOME)/.fvwm/xinit
		for f in /etc/xdg/menus/*.menu; do
			[[ -f "$$f" ]] || continue
			fname=$${f%.menu}
			fname=$${fname##*/}
			python .fvwm/xdg2fvwm.py Menu-$$fname $$f >$(HOME)/.fvwm/$$fname.hook
			echo "Read $$[HOME]/.fvwm/$$fname.hook" >>$(HOME)/.fvwm/config
			echo "AddToMenu MenuMyRoot $$fname Popup Menu-$$fname" >>$(HOME)/.fvwm/config
		done
	fi
	mkdir -p $(HOME)/.screenshot
	for item in $(IFNONEXIST_ITEMS); do
		if [[ -f $$item ]] && ! [[ -f $(HOME)/$$item ]]; then
			$(INSTALL_DATA) $$item $(HOME)/$$item
		fi
		if [[ -d $$item ]]; then
			for file in `find $$item -type f`; do
				if [[ ! -f $(HOME)/$$file ]]; then
					$(INSTALL_DATA) $$file $(HOME)/$$file
				fi
			done
		fi
	done
	chmod a+x $(HOME)/.xinitrc $(HOME)/.xserverrc
	ln -s -f $(HOME)/.xinitrc $(HOME)/.xsession
	ln -s -f $(HOME)/.bashrc $(HOME)/.bash_profile
	mkdir -p $(HOME)/.config/mc $(HOME)/.local/share/mc/
	umask 0117
	ln -s -f $(HOME)/.bashrc $(HOME)/.local/share/mc/bashrc
	[[ -f /etc/mc/mc.ext ]] \
		&& cat .config/mc/mc.ext /etc/mc/mc.ext > $(HOME)/.config/mc/mc.ext \
		|| $(INSTALL_DATA) .config/mc/mc.ext $(HOME)/.config/mc/mc.ext
ifneq '' '$(filter $(host_os),cygwin linux)'
	if command -v file 1>/dev/null; then
		$(INSTALL_DATA) -t $(HOME) .magic
		( cd $(HOME); file --compile -m .magic )
	fi
endif
	sed -e 's=@CFG_FONT_SIZE@=$(CFG_FONT_SIZE)=' <.minttyrc >$(HOME)/.minttyrc

.PHONY: uninstall
.ONESHELL:
uninstall:
	for item in $(OVERRIDDEN_ITEMS); do
		if [ -f $$item ]; then
			[ -f $(HOME)/$$file ] && rm -f $(HOME)/$$item
		fi
		if [ -d $$item ]; then
			for file in `find $$item -depth`; do
				if [ -d $$file ]; then
					rmdir $(HOME)/$$file || :
					continue
				fi
				[ -f $(HOME)/$$file ] && rm $(HOME)/$$file
			done
		fi
	done
	rm -f $(HOME)/.mc/bashrc $(HOME)/.mc/ini $(HOME)/.mc/bindings
	rmdir $(HOME)/.mc || :

################################################################
# Docs targets.

.PHONY: html
html: $(HTML_FILES)

# --stylesheet=rst.css
$(HTML_FILES): %.html: %.rst
	$(RST2HTML) $*.rst $@

################################################################
# Clean targets.

.PHONY: clean
clean:
	rm -f $(HTML_FILES) $(TARBALL)

.PHONY: distclean
distclean: clean

################################################################
# Helper target.

.PHONY: help
help:
	@echo
	@echo Supported targets:
	@sed -n -e '/^[[:alnum:]_-]*:/{s=^\(.*\):.*=  \1=;p;}' $(MAKEFILE_LIST)