CPPFLAGS    = -I/usr/include/ncurses
TARGET_ARCH = -m486
LDLIBS      = -lncurses
CFLAGS      = -pipe -g -O2 -fstrength-reduce -Wall
LDFLAGS     = -pipe
ASFLAGS     = -m486

OBJS        = tv.o tvcontrol.o gui.o osd.o terminal.o logdos.o rc.o rclex.o \
	      rcyacc.tab.o

##############################################################################

ifeq (.depend,$(wildcard .depend))
all: tv
include .depend
else
all: depend
	$(MAKE) all
endif

##############################################################################

tv: $(OBJS)

##############################################################################

clean:
	$(RM) *~ *.o *.dvi *.log *.aux *yacc.tab.[ch] *yacc.output *lex.[co] \
              .*use-inittab .depend .tmp_depend

##############################################################################

depend: rclex.c rcyacc.tab.c
	for i in *.c;do $(CPP) $(CPPFLAGS) -MM $$i;done >.tmp_depend
	mv .tmp_depend .depend

##############################################################################

rclex.c: rclex.l rcyacc.tab.h
	flex -i -p -p -v -B -Cem -Prc -t $< >$@

rcyacc.tab.h: rcyacc.tab.c rcyacc.y
rcyacc.tab.c: rcyacc.y rc.h tvcontrol.h
	bison -p rc -b rcyacc -d -v $<

##############################################################################

install:
	$(MAKE) -s _install

install-no-inittab:
	$(MAKE) -s _install-no-inittab

install-inittab:
	$(MAKE) -s _install-inittab

install-start:
	$(MAKE) -s _install-start

install-stop:
	$(MAKE) -s _install-stop

##############################################################################

_install: _root _vt12 _tvrc _tvexists _strip _avoidinittab _askuser \
	 _inittab-disable _copy-binary _inittab-enable

_install-no-inittab: _root _usenoinittab _install

_install-inittab: _root _useinittab _install

_install-start: _root _avoidinittab _inittab-enable

_install-stop: _root _avoidinittab _inittab-disable

##############################################################################

_root:
	if [ xroot \!= x`whoami` ] ; then \
	  echo;echo; \
	  echo "You have to be root to do \`make install'" 1>&2; \
	  echo ; \
	  exit 1; \
	fi

_tvrc: _home-tvrc _root-tvrc

_home-tvrc:
	if [ -f ~/.tvrc -a -O ~/.tvrc ] ; then \
	  for i in /usr/local /usr/local/etc; do \
	    if [ \! -d $i ] ; then \
	      mkdir $i; \
	      chown root:root $i; \
	      chmod 755 $i; \
	    fi; \
	  done; \
	  chown root:root ~/.tvrc; \
	  chmod 644 ~/.tvrc; \
	  mv ~/.tvrc /usr/local/etc/system.tvrc; \
	fi

_root-tvrc:
	if [ -f /.tvrc -a -O /.tvrc ] ; then \
	  for i in /usr/local /usr/local/etc; do \
	    if [ \! -d $i ] ; then \
	      mkdir $i; \
	      chown root:root $i; \
	      chmod 755 $i; \
	    fi; \
	  done; \
	  chown root:root /.tvrc; \
	  chmod 644 /.tvrc; \
	  mv /.tvrc /usr/local/etc/system.tvrc; \
	fi

_vt12:
	if [ \! -c /dev/tty12 ] ; then \
	  mknod /dev/tty12 c 4 12 ; \
	  echo;echo; \
          echo "I had to create a new node for \`/dev/tty12'" ; \
	  echo ; \
	fi

_tvexists:
	if [ \! -x tv ] ; then \
	  echo;echo; \
	  echo "There is no file \`./tv' in the current directory" ; \
 	  echo "Maybe you have do to \`make clean; make all' first" ; \
	  echo ; \
	  exit 1; \
	fi

_strip:
	strip tv; \
	chown root:root tv; \
	chmod 750 tv

_avoidinittab:
	if [ -f .do-not-use-inittab ] ; then \
	  echo;echo; \
          echo "Not touching \`/etc/inittab'" ; \
	  echo ; \
          mv tv /usr/bin; \
          echo "\`make' will exit with errorcode 1; this is normal"; \
	  echo ; \
	  exit 1; \
        fi

_askuser:
	echo;echo; \
	echo "I will now copy \`./tv' to \`/usr/bin/tv'; in order to"; \
	echo "do so, any copy of \`/usr/bin/tv' that is currently"; \
	echo "active has to be terminated. Assuming that \`tv' is to be"; \
        echo "launched from \`/etc/inittab', I will patch your \`inittab'"; \
	echo "as needed and I will restart \`init' (1. for stopping any"; \
	echo "active copy of \`tv', 2. for restarting the new copy)"; \
	echo ; \
	if [ \! -f .use-inittab ] ; then \
	  echo "If you do not want to have \`/etc/inittab' changed," ; \
          echo "press CTRL-C now and then do \`make install-no-inittab'"; \
	  echo "Otherwise, press <ENTER>; if you ever want to be asked" ; \
          echo "again, call \`make clean' first" ; \
          read ; \
	  touch .use-inittab; \
	  chown `find tv -maxdepth 0 -type f -printf %u:%g` .use-inittab ;\
        fi

_inittab-disable:
	echo "Please wait! Installation in progress " ; \
	echo " --- this might take a little while"; \
	echo ; \
	sed -e "/\# *tv:[0-9]*:/b" -e "/tv:[0-9]*:/s//\#&/1" /etc/inittab >/etc/inittab.new; \
	mv /etc/inittab.new /etc/inittab; \
	/sbin/telinit -t 3 q

_copy-binary:
	mv tv /usr/bin

_inittab-enable:
	echo "When you see \`tv's screen, press ALT_PTRSCREEN or ALT-F??";\
        echo "in order to switch back to the current virtual console!" ; \
	echo ; \
	if grep -q tv:[0-9]*: /etc/inittab;then \
	  sed -e "/\# *tv:/s//tv:/" /etc/inittab >/etc/inittab.new; \
	  mv /etc/inittab.new /etc/inittab; \
	else \
	  echo -e "\n# TV receiver\\ntv:456:respawn:/usr/bin/tv --force --vt 12" >>/etc/inittab; \
	fi; \
	/sbin/telinit -t 3 q

_usenoinittab:
	$(RM) .*use-inittab
	touch .do-not-use-inittab
	if [ -x tv ] ; then \
	  chown `find tv -maxdepth 0 -type f -printf %u:%g` \
	        .do-not-use-inittab ; \
	else if [ -f tv.c ] ; then \
	  chown `find tv.c -maxdepth 0 -type f -printf %u:%g` \
                .do-not-use-inittab ; \
	fi; fi

_useinittab:
	$(RM) .*use-inittab
	touch .use-inittab
	if [ -x tv ] ; then \
	  chown `find tv -maxdepth 0 -type f -printf %u:%g` .use-inittab ; \
	else if [ -f tv.c ] ; then \
	  chown `find tv.c -maxdepth 0 -type f -printf %u:%g` .use-inittab ; \
	fi; fi

##############################################################################
