put /local/pup/OFS ?/tmpasm/OFS
put /tmpasm/OFS {\n}

put /local/pup/src {
 os_dep.c
 os_dep_fs.c
 error.c
 libs.c
 puplug.c
 libs_bu.c
 libs_dep.c
 util.c
}

# headers to install
put /local/pup/hdr {
 config.h
 error.h
 libs.h
 libs_dep_parse.h
 os_dep.h
 os_dep_fs.h
 puplug.h
 scconfig_hooks.h
 util.h
}


uniq /local/pup/src
put /local/pup/obj /local/pup/src
gsub /local/pup/obj {.c[ \t\r\n]} {$O }

put /tmpasm/OFS { }


put /local/pup/cflags ?cc/argstd/Wall
if /local/pup/debug then
	append /local/pup/cflags [@-g @/host/cc/argstd/ansi@ @/host/cc/cflags@ @]
end

put /local/pup/ldflags [@ @/host/cc/ldflags@ @?/target/cc/rdynamic@ @]

if ?/local/pup/disable_dynlib
then
	put /local/pup/dyntarget {}
else
	put /local/pup/dyntarget {$(PUPLUG_DYN) $(PUPLUG_DYN_VER_X) $(PUPLUG_DYN_VER_XY) $(PUPLUG_DYN_VER_XYZ)}
end

switch ?/target/cc/soname
	case {^$}
		put /local/pup/soname {}
		end
	default
		put /local/pup/soname [@@/target/cc/soname@$(PUPLUG_DYN_VER_X)@]
		end
end

switch ?/local/pup/sccbox
	case {^$} put /local/pup/sccbox {../scconfig/sccbox}; end
end

print [~# Generated by scconfig. Do not edit.
O = $(PUP_O_PREFIX).o
A = $(PUP_O_PREFIX).a
SCCBOX = ~/local/pup/sccbox~

PREFIX=/usr
INCDIR=$(install_root)$(DESTDIR)$(PREFIX)/include/puplug
LIBDIR=$(install_root)$(DESTDIR)$(PREFIX)/lib
CFLAGS = -I.. ~/local/pup/cflags~ ~/target/cc/fpic~ $(PUPLUG_CFLAGS)
LDFLAGS = ~/local/pup/ldflags~ ~/target/cc/fpic~ $(PUPLUG_LDFLAGS)
OBJS = ~/local/pup/obj~

PUPLUG_DYN = libpuplug~/target/sys/ext_dynlib~
PUPLUG_DYN_VER_X = $(PUPLUG_DYN).~/local/pup/ver1~
PUPLUG_DYN_VER_XY = $(PUPLUG_DYN_VER_X).~/local/pup/ver2~
PUPLUG_DYN_VER_XYZ = $(PUPLUG_DYN_VER_XY).~/local/pup/ver3~
PUPLUG_EXE=util/puplug

all: $(PUPLUG_EXE) libpuplug$A ~/local/pup/dyntarget~

libpuplug$A: $(OBJS)
	@$(SCCBOX) rm -f libpuplug$A
	@~/host/fstools/ar~ r libpuplug$A $(OBJS)
	-~/host/fstools/ranlib~ libpuplug$A

$(PUPLUG_DYN_VER_XYZ): $(OBJS)
	$(CC)  $(OBJS) -o $(PUPLUG_DYN_VER_XYZ) $(LDFLAGS) ~?/target/cc/ldflags_dynlib~ ~?/local/pup/soname~

$(PUPLUG_DYN_VER_XY): $(PUPLUG_DYN_VER_XYZ)
	$(SCCBOX) ln -f $(PUPLUG_DYN_VER_XYZ) $(PUPLUG_DYN_VER_XY)

$(PUPLUG_DYN_VER_X): $(PUPLUG_DYN_VER_XYZ)
	$(SCCBOX) ln -f $(PUPLUG_DYN_VER_XYZ) $(PUPLUG_DYN_VER_X)

$(PUPLUG_DYN): $(PUPLUG_DYN_VER_XYZ)
	$(SCCBOX) ln -f $(PUPLUG_DYN_VER_XYZ) $(PUPLUG_DYN)

$(PUPLUG_EXE): util/puplug$O os_dep_fs$O
	$(CC) $(LDFLAGS) util/puplug$O os_dep_fs$O -o $(PUPLUG_EXE)

util/puplug$O: util/puplug.c
	$(CC) -c $(CFLAGS) -o util/puplug$O util/puplug.c

include Makefile.dep

clean:
	$(SCCBOX) rm -f $(OBJS) libpuplug$A $(PUPLUG_EXE) util/puplug$O $(PUPLUG_DYN) $(PUPLUG_DYN_VER_X) $(PUPLUG_DYN_VER_XY) $(PUPLUG_DYN_VER_XYZ)

distclean: clean
	rm Makefile config.h

# Universal install rule (can uninstall as well)

install_hdr_:
	$(MKDR) $(INCDIR)
~]

foreach /local/pup/h in /local/pup/hdr
	sub /local/pup/h {^ } {}
	print [~	$(INST) ~/local/pup/h~ $(INCDIR)/~/local/pup/h~~] {\n}
end

print [~

install_: install_hdr_
	$(MKDR) $(LIBDIR)
	$(INST) $(PUPLUG_DYN_VER_XYZ) $(LIBDIR)/$(PUPLUG_DYN_VER_XYZ)
	$(INST) libpuplug$A $(LIBDIR)/libpuplug$A
	$(SCCBOX) ln -f $(PUPLUG_DYN_VER_XYZ) $(LIBDIR)/$(PUPLUG_DYN)
	$(SCCBOX) ln -f $(PUPLUG_DYN_VER_XYZ) $(LIBDIR)/$(PUPLUG_DYN_VER_X)
	$(SCCBOX) ln -f $(PUPLUG_DYN_VER_XYZ) $(LIBDIR)/$(PUPLUG_DYN_VER_XY)

install:
	$(MAKE) install_ "INST=$(SCCBOX) install -i" "MKDR=$(SCCBOX) mkdir -p -i"

linstall:
	$(MAKE) install_ "INST=$(SCCBOX) install -l -f -a" "MKDR=$(SCCBOX) mkdir -p -i"

uninstall:
	$(MAKE) install_ "INST=$(SCCBOX) install -u -f" "MKDR=$(SCCBOX) mkdir -u"

dep:
	echo "### Generated file, do not edit, run make dep ###" > Makefile.dep
	echo "" >> Makefile.dep
~]

foreach /local/pup/c in /local/pup/src
	print [~	@gcc -MM ~/local/pup/c~ -I. >> Makefile.dep
~]
end

put /tmpasm/OFS /local/pup/OFS

print {\n### Rules for compiling objects ###\n}
foreach /local/pup/c in /local/pup/src
	put /local/pup/o /local/pup/c
	sub /local/pup/o {.c$} {$O}
	print [~
~/local/pup/o~: ~/local/pup/c~
	$(CC) -c $(CFLAGS) -o ~/local/pup/o~ ~/local/pup/c~
~]
end


