
all: compile

include Makefile_common

DIR_TMP=/tmp
DIR_SRC_TPCTL=tpctl-$(VER_TPCTL)
FILE_SRCTAR=tpctl-$(VER_TPCTL).src.tar

compile:
	@for dir in drivers lib tpctl ntpctl; do if ! make -C $$dir all; then exit 1; fi; done
	@echo Compilation complete.

install: uninstall uninstall_legacy install_setup install_modules install_binaries
	depmod -a
	mknod --mode=664 /dev/thinkpad c 10 170
	install -m 644 -o 0 -g 0 tpctl.1 $(DIR_MAN)/man1/tpctl.1
	ln -s $(DIR_MAN)/man1/tpctl.1 $(DIR_MAN)/man1/ntpctl.1

install_setup:
	utils/tpctlify $(DIR_MOD) $(DIR_LIB)

install_modules:
	if [ ! -d $(DIR_MOD)/thinkpad ]; then mkdir -p -m 755 $(DIR_MOD)/thinkpad ; fi
	install -m 444 -o 0 -g 0 drivers/thinkpad.o $(DIR_MOD)/thinkpad/thinkpad.o
	install -m 444 -o 0 -g 0 drivers/smapi.o $(DIR_MOD)/thinkpad/smapi.o
	install -m 444 -o 0 -g 0 drivers/superio.o $(DIR_MOD)/thinkpad/superio.o
	install -m 444 -o 0 -g 0 drivers/rtcmosram.o $(DIR_MOD)/thinkpad/rtcmosram.o

install_binaries:
	if [ ! -d $(DIR_LIB) ]; then mkdir -p -m 755 $(DIR_LIB) ; fi
	install -m 755 -o 0 -g 0 lib/libsmapidev-$(VER_TPCTL).so $(DIR_LIB)/libsmapidev-$(VER_TPCTL).so
	ln -sf $(DIR_LIB)/libsmapidev-$(VER_TPCTL).so $(DIR_LIB)/libsmapidev.so
	if [ ! -d $(DIR_BIN) ]; then mkdir -p -m 755 $(DIR_BIN) ; fi
	install -m 755 -o 0 -g 0 tpctl/tpctl $(DIR_BIN)/tpctl
	install -m 755 -o 0 -g 0 ntpctl/ntpctl $(DIR_BIN)/ntpctl

uninstall: 
	modprobe -r smapi
	modprobe -r superio
	modprobe -r rtcmosram
	modprobe -r thinkpad
	rm -vrf $(DIR_MOD)/thinkpad
	rm -vf $(DIR_LIB)/libsmapidev.so
	rm -vf $(DIR_LIB)/libsmapidev-*.so
	rm -vf $(DIR_BIN)/tpctl
	rm -vf $(DIR_BIN)/ntpctl
	rm -vf /dev/thinkpad
	rm -vf $(DIR_MAN)/man1/tpctl.1
	rm -vf $(DIR_MAN)/man1/ntpctl.1

uninstall_legacy:
	rm -vrf /lib/modules/thinkpad
	rm -vf /usr/bin/tpctl
	rm -vf /usr/local/bin/smapify_modulesconf
	rm -vf /usr/local/bin/tpctlify_modulesconf
	rm -vf /usr/local/bin/tpctlify
	rm -vf /usr/local/sbin/tpctlify
	rm -vf /dev/tp600
	rm -vf /dev/smapi

depend:

clean:
	@for dir in drivers lib tpctl ntpctl other_tools/* ; do make -C $$dir clean ; done
	@echo Cleaning complete.

tarball: clean
	rm -vrf $(DIR_TMP)/$(DIR_SRC_TPCTL)
	mkdir $(DIR_TMP)/$(DIR_SRC_TPCTL)
	cp -vrf AUTHORS README TODO TROUBLESHOOTING VGA_MODES ChangeLog Makefile* drivers lib tpctl ntpctl include utils other_tools tpctl.1 $(DIR_TMP)/$(DIR_SRC_TPCTL)
	rm -vf $(DIR_TMP)/$(FILE_SRCTAR).gz
	sh -c "cd $(DIR_TMP) ; /bin/tar -zcvf $(FILE_SRCTAR).gz $(DIR_SRC_TPCTL)"
	mv -vf $(DIR_TMP)/$(FILE_SRCTAR).gz /usr/src/redhat/SOURCES/tpctl-$(VER_TPCTL).src.tar.gz
	rm -vrf $(DIR_TMP)/$(DIR_SRC_TPCTL)
	@echo Tarball /usr/src/redhat/SOURCES/tpctl-$(VER_TPCTL).src.tar.gz has been created.

