##############################################
# All OS procedures, used in other Makefiles #
##############################################

# All text files variable->value replacer
SEDREPLACE = sed -e 's|PREFIX|$(PREFIX)|g'

# Link command
OFWTKLINK = @OFWTKLINKi() { echo $(CC) $$@ $(LDFLAGS) -o $@;$(CC) $$@ $(LDFLAGS) -o $@; } && OFWTKLINKi
OFWTKAR = ar rcv $@ $? && $(RANLIB) $@

# Mode for all binaries
BINMODE = 755

.SUFFIXES: .8.in .8 .3.in .3 .5.in .5

.8.in.8 .3.in.3 .5.in.5:
	$(SEDREPLACE) $@.in > $@

all:

clean:
	rm -rf $(ALLOBJS) core


install-bin: $(BINOBJS)
	@for CBIN in $(BINOBJS) $(PCBOBJS) ""; do				\
	    if [ "$$CBIN" = "" ]; then continue; fi;				\
	    echo "Installing BIN: $$CBIN -> $(PREFIX)/bin with $(BINMODE) mode";\
	    cp $$CBIN $(PREFIX)/bin;						\
	    chmod $(BINMODE) $(PREFIX)/bin/$$CBIN;				\
	done

install-etc: $(ETCOBJS)
	@for CETC in $(ETCOBJS) ""; do						\
	    if [ "$$CETC" = "" ]; then continue; fi;				\
	    if [ -f $(PREFIX)/etc/$$CETC ]; then				\
	        echo "NOT installing $(PREFIX)/$$CETC: exists, skipped";	\
	    else								\
	        echo "Installing ETC: $$CETC -> $(PREFIX)/etc";			\
		cp $$CETC $(PREFIX)/etc;					\
	    fi;									\
	done;									\

install-man: $(MANOBJS)
	@for CMAN in $(MANOBJS) ""; do						\
	    if [ "$$CMAN" = "" ]; then continue; fi;				\
	    CMANDIR=$$(echo $$CMAN|rev|cut -d '.' -f 1|rev);			\
	    CMANFIL=$$(echo $$CMAN|rev|cut -d '/' -f 1|rev);			\
	    echo "Installing MAN: $$CMANFIL -> $(PREFIX)/man/man$$CMANDIR";	\
	    cp $$CMAN $(PREFIX)/man/man$$CMANDIR;				\
	done

install-common:
	@for CDIR in $(PREFIX)/bin $(PREFIX)/etc $(PREFIX)/man/man3		\
	    $(PREFIX)/man/man5 $(PREFIX)/man/man8; do				\
	    echo "Creating/updating directory: $$CDIR";				\
	    mkdir -p $$CDIR;							\
	done;									\
	echo "Installing OpenFWTK base path: OFWTKPATH -> /etc/openfwtk.conf";	\
	echo "OFWTKPATH=\"$(PREFIX)\"" > /etc/openfwtk.conf

install: all install-common install-bin install-man install-etc
	$(INSTALLADD)
