# ----------------------------------------------------------------------------
#                                   X-BONE
#
#                USC Information Sciences Institute (USC/ISI)
#                   Marina del Rey, California 90292, USA
#                          Copyright (c) 1998-2000
#
#			     $Revision: 1.37 $
#			$Date: 2000/10/04 22:40:51 $
# ----------------------------------------------------------------------------


PREFIX ?=	/usr/local
WWW?=		${PREFIX}/www
APACHE_CONF?=	${PREFIX}/etc/apache
SRC ?=		${CURDIR}/xbone
LOG?=		/tmp/xbone-install.log
OLD_CERT_DIR?=	${PREFIX}/xbone/om/cert		${PREFIX}/xbone/rd/cert \
		${WWW}/server-cert		${WWW}/cert

NAMEDB_PATH =	/etc/namedb
SETKEY?=	setkey
CERT?=		/etc/xbone/cert
CACERT?=	${PREFIX}/xbone/lib/cert
V6?=		${PREFIX}/v6
PERL5?=		`which perl`

.SILENT: 	rd omgui sys-check copyfile patch-omgui patch-rd www \
		xb-conf gui_msg rd_msg dns-server dns-client \
		clean-www clean-xbone clean-all \
		mdir export trim tball cleanup toweb

.PHONY:		rd omgui sys-check copyfile patch-omgui patch-rd www \
		xb-conf gui_msg rd_msg dns-server dns-client \
		clean-www clean-xbone clean-all \
		mdir export trim tball cleanup toweb

ch-gmake:
	@echo ">>> Verify if \"gmake\" is used:"
	@if( test "${CURDIR}" = "") then { \
	  echo "    ! [Error]: You need to use \"gmake\", not \"make\"."; \
	  exit 1; \
	} else { \
	  echo "    - gmake checked ok."; \
	} fi

ch-SRC:
	@echo ">>> Verify if ${SRC} exists:"
	@if( test -d ${SRC} ) then { \
	  echo "    - ${SRC} exists."; \
	} else { \
	  echo "    ! ${SRC} doesn't exist.";\
	  echo "    ! You need to specify SRC=/usr/local/xbone in make."; \
	  exit 1; \
	} fi

#- List of targets ------------------------------------------------------------
#  - sys-check:		check platform, setkey (KAME) syntax, & Perl path
#  - copyfile:		copies xbone files & man pages to ${PREFIX}
#  - patch-omgui/rd:	patch XB_Defs.pm, DNS conf, Apache-SSL conf, GUI files
#  - www:		copies web files (html & CGI scripts) to ${PREFIX}/www
#  - xb-conf:		generates the X-Bone host config file
#  - dns-server:	configure named.conf & xbone zone files
#  - dns-client:	configure resolv.conf
#  - rd:		meta-target for installing X-Bone Resource Daemon
#  - omgui:		meta-target for installing X-Bone GUI & Overlay Manager
#------------------------------------------------------------------------------

rd:	sys-check patch-rd        copyfile xb-conf dns-client rd_msg

omgui:	sys-check patch-omgui www copyfile xb-conf dns-server gui_msg

rpm-rd: sys-check patch-rd        xb-conf dns-client rd_msg

rpm-om: ch-SRC sys-check patch-omgui www xb-conf dns-server gui_msg

sys-check:	ch-gmake
	date "+>>> XBone: %Y-%m-%d TIME: %H:%M:%S" | tee -a ${LOG}
	echo ">>> System check:" | tee -a ${LOG}
	${PERL5} ${CURDIR}/xb-Host-check.pl
	if( test -f ${CURDIR}/replace_setkey) then { \
	  echo "    - Replace setkey(8) for Kame." | tee -a ${LOG}; \
	  if( test -f ${V6}/sbin/setkey ) then { \
	    echo "    - old setkey => ${V6}/sbin/setkey.OLD" | tee -a ${LOG};\
	    mv ${V6}/sbin/setkey ${V6}/sbin/setkey.OLD; \
	  } else { \
	    echo "    - Can't find setkey. Install new one at ${V6}." | \
	    tee -a ${LOG}; \
	  } fi ; \
	  tar xvfz ${CURDIR}/${SETKEY}.tar.gz; \
	  cd ${CURDIR}/kame/${SETKEY}; make all; \
	  cd ${CURDIR}/kame/${SETKEY}; make install PREFIX=${V6}; \
	  cd ${CURDIR}/kame/${SETKEY}; make clean; \
	  mv ${CURDIR}/replace_setkey ${CURDIR}/setkey_replaced; \
	} fi
	echo "    - Check /usr/local/bin/perl & /usr/local/bin/perl5" | \
	  tee -a ${LOG}
	if ( test ! -x /usr/local/bin/perl) then { \
	  ln -s /usr/bin/perl /usr/local/bin/perl; \
	} fi
	if ( test ! -x /usr/local/bin/perl5) then { \
	  ln -s /usr/bin/perl5 /usr/local/bin/perl5; \
	} fi
	echo "<<< System check completes." | tee -a ${LOG}
	echo ""
	  
copyfile:	ch-gmake
	echo ">>> Installing X-Bone files into ${PREFIX}/xbone:"| tee -a ${LOG}
	if( test ! -d ${CERT} ) then { \
	  mkdir -p ${CERT}; \
	} fi
	if( test -d ${PREFIX}/xbone ) then { \
	  echo "    - Process old certificates." | \
	    tee -a ${LOG}; \
	  /usr/bin/dialog --yesno "${PREFIX}/xbone exists. Do you want to keep\
	    the old certificates on this host? (Answer <NO> will DELETE ALL\
	    EXISTING CERTIFICATES.)" 8 50 && \
	  for p in ${OLD_CERT_DIR}; do \
	    if(test -d $$p) then { \
	      cp -fR $$p/* ${CERT}/ ;\
	    } fi ;\
	  done;\
	  echo "    - ${PREFIX}/xbone exists. Moved to xbone-OLD" \
	    | tee -a ${LOG}; \
	  if( test -d ${PREFIX}/xbone-OLD) then {\
	    echo "    - delete old xbone-OLD" | tee -a ${LOG};\
	    rm -rf ${PREFIX}/xbone-OLD; \
	  } fi;\
	  mv ${PREFIX}/xbone ${PREFIX}/xbone-OLD; \
	} fi
	if( test -f /etc/Xbone_daemon.conf) then { \
	  echo "    - old daemon conf file found, move to /etc/xbone" | \
	    tee -a ${LOG}; \
	  perl -pe 's/=cert/=\/etc\/xbone\/cert/g' < /etc/Xbone_daemon.conf \
	    > /etc/xbone/Xbone_daemon.conf; \
	  rm -f /etc/Xbone_daemon.conf;\
	} fi 
	if( test -f /etc/Xbone_daemon.state) then { \
	  /usr/bin/dialog --yesno "Old state file /etc/Xbone_daemon.state\
	  exists. Do you want to keep it?" 8 50 && \
	  mv -f /etc/Xbone_daemon.state* /etc/xbone/ || \
	  rm -f /etc/Xbone_daemon.state*; \
	} fi 
	cp -R ${SRC} ${PREFIX}/xbone
	cp -R ${SRC}/man ${PREFIX}
	cp -R ${CURDIR} ${PREFIX}/xbone/install
	rm -r ${PREFIX}/xbone/install/xbone
	cp -f ${CACERT}/CAcert* ${CERT}
	find ${PREFIX}/xbone -exec chmod ugo+r {} \;
	find ${PREFIX}/xbone -type f -name '*.pl' -exec chmod ugo+x {} \;
	find ${PREFIX}/xbone -type d -exec chmod ugo+x {} \;
	ln -fs ${PREFIX}/xbone/om/xb-overlay-manager.pl \
	      ${PREFIX}/bin/xb-overlay-manager
	ln -fs ${PREFIX}/xbone/rd/xb-daemon.pl ${PREFIX}/bin/xb-daemon
	ln -fs ${PREFIX}/xbone/rd/apps/xb-config.pl ${PREFIX}/bin/xb-config
	ln -fs ${PREFIX}/xbone/rd/apps/xb-pick ${PREFIX}/bin/xb-pick
	echo "<<< File installation completes."  | tee -a ${LOG}

patch-omgui:	ch-gmake
	echo ">>> Gathering info & patching files ..." | tee -a ${LOG}
	${PERL5} ${CURDIR}/xb-Source-patch.pl gui
	echo "<<< Patching completes."  | tee -a ${LOG}

patch-rd:	ch-gmake
	echo ">>> Gathering info & patching files ..." | tee -a ${LOG}
	${PERL5} ${CURDIR}/xb-Source-patch.pl nogui
	echo "<<< Patching completes." | tee -a ${LOG}

www:		ch-gmake 
	echo ">>> Installing GUI files into ${WWW}" | tee -a ${LOG}
	if(test -d ${WWW}) then {\
	  if(test ! -d ${WWW}-OLD) then {\
	    echo "    - old web files => [${WWW}-OLD]" | tee -a ${LOG};\
	    mv ${WWW} ${WWW}-OLD; \
	  } else {\
	    rm -r ${WWW};\
	  } fi;\
	} fi
	if(test -d ${APACHE_CONF}) then { \
	  if(test ! -d ${APACHE_CONF}-OLD) then { \
	    echo "    - old apache config files => [${APACHE_CONF}-OLD]" \
	      | tee -a ${LOG}; \
	    mv ${APACHE_CONF} ${APACHE_CONF}-OLD; \
	  } else {\
	    rm -r ${APACHE_CONF};\
	  } fi;\
	} fi
	cp -R ${SRC}/www ${WWW}
	cp -R ${SRC}/lib ${WWW}/lib
	cp -R ${CURDIR}/apache_conf_sample ${APACHE_CONF}
	find ${WWW} -exec chmod ugo+r {} \; 
	find ${WWW} -type f -name '*.pl' -exec chmod ugo+x {} \; 
	find ${WWW} -type d -exec chmod ugo+x {} \; 
	if (test ! -f ${APACHE_CONF}/httpd.conf) then { \
	  ln -s ${APACHE_CONF}/httpsd.conf ${APACHE_CONF}/httpd.conf; \
	} fi
	echo "<<< GUI installation completes." | tee -a ${LOG}

xb-conf:
	echo ">>> Check /etc/xbone/Xbone_daemon.conf." | tee -a ${LOG}
	if (test -f /etc/xbone/Xbone_daemon.conf) then { \
	  /usr/bin/dialog --yesno "/etc/xbone/Xbone_daemon.conf exists.\
	  Do you want to overwrite it with a new one?" 8 50 && \
	  ${PERL5} ${PREFIX}/xbone/rd/apps/xb-config.pl && \
	  echo "<<< New /etc/xbone/Xbone_daemon.conf created."|tee -a ${LOG} ||\
	  echo "<<< Old Xbone_daemon.conf is preserved." | tee -a ${LOG};\
	} else {\
	  ${PERL5} ${PREFIX}/xbone/rd/apps/xb-config.pl; \
	  echo "<<< /etc/Xbone_daemon.conf created." | tee -a ${LOG};\
	} fi

gui_msg:
	echo ">>> X-Bone GUI installation completes." | tee -a ${LOG}
	echo ""
	echo ">>> Procedures to run the X-Bone GUI"
	echo "(0) Obtain signed Host Certificate for the web server, overlay"
	echo "    manager, and the DNS resource daemon. (It's the same one"
	echo "    of all three by default.) And copy them to /etc/xbone/cert/."
	echo "    ************************************************************"
	echo "    # WARNING - make sure your keys are sufficiently protected.#"
	echo "    #  *cert.* files are public keys, 644 is sufficient        #"
	echo "    #  *key.* files are private keys, 600 is recommended       #"
	echo "    #                                                          #"
	echo "    # These must be set when the keys are installed; they are  #"
	echo "    # NOT set by the X-Bone installation package.              #"
	echo "    ************************************************************"
	echo "(1) Restart the name server (named). You can use kill -HUP."
	echo "(2) Start the web server:           % httpsdctl start"
	echo "(3) Start the DNS resource daemon:  % xb-daemon"
	echo "(4) Start the overlay manager:      % xb-overlay manager"
	echo ""
	echo "    Email to <xbone@isi.edu> if you have any questions."
	echo "<<< Enjoy! "
	echo ""

rd_msg:
	echo ">>> X-Bone RD installation completes." | tee -a ${LOG}
	echo ""
	echo ">>> Procedures to run the X-Bone RD"
	echo "(0) Obtain signed Host Certificates for the resource daemon"
	echo "    and copy the cert-key pair to /etc/xbone/cert/."
	echo "    ************************************************************"
	echo "    # WARNING - make sure your keys are sufficiently protected.#"
	echo "    #  *cert.* files are public keys, 644 is sufficient        #"
	echo "    #  *key.* files are private keys, 600 is recommended       #"
	echo "    #                                                          #"
	echo "    # These must be set when the keys are installed; they are  #" 
	echo "    # NOT set by the X-Bone installation package.              #"
	echo "    ************************************************************"
	echo "(1) Start the resource daemon:     % xb-daemon "
	echo ""
	echo "    Email to <xbone@isi.edu> if you have any questions."
	echo "<<< Enjoy!"
	echo ""

dns-server:	ch-gmake
	echo ">>> Patching DNS files for overlay DNS server." | tee -a ${LOG}
	${PERL5} ${CURDIR}/xb-DNS-config.pl server
	echo "<<< DNS server configuration completes." | tee -a ${LOG}

dns-client:	ch-gmake
	echo ">>> Patching DNS files for overlay DNS client." | tee -a ${LOG}
	${PERL5} ${CURDIR}/xb-DNS-config.pl client
	echo "<<< DNS client configuration completes." | tee -a ${LOG}

#==============================================================================
# [ISI-INTERNAL] Utility Targets
#==============================================================================
clean-www:
	if (test -d /usr/local/www) then { \
	  rm -r /usr/local/www; \
	  if (test -d /usr/local/www-OLD) then { \
	    mv /usr/local/www-OLD /usr/local/www; \
	  } fi; \
	} fi
	if (test -d /usr/local/etc/apache) then { \
	  rm -r /usr/local/etc/apache; \
	  if (test -d /usr/local/etc/apache-OLD) then { \
	    mv /usr/local/etc/apache-OLD /usr/local/etc/apache; \
	  } fi; \
	} fi

clean-xbone:
	if (test -d /usr/local/xbone) then {\
	  rm -r /usr/local/xbone; \
	  if (test -d /usr/local/xbone-OLD) then { \
	    mv /usr/local/xbone-OLD /usr/local/xbone; \
	  } fi ;\
	} fi

clean-all:	clean-www clean-xbone

CERT-DIR?=	/usr/local/cert
RD-CERT?=	/usr/local/xbone/rd/cert
OM-CERT?=	/usr/local/xbone/om/cert
GUI-CERT?=	/usr/local/www/server-cert

cert-key:
	cp -f ${CERT-DIR}/* ${RD-CERT}/
	cp -f ${CERT-DIR}/* ${OM-CERT}/
	cp -f ${CERT-DIR}/* ${GUI-CERT}/

#==============================================================================
# [ISI-INTERNAL] Section for generating X-Bone source tarball
#==============================================================================

CVSROOT?=	/home/xbone/CVSROOT
MODULE?=	xbone
APACHE-CONF?=	apache-conf
APACHE-DIR?=	apache_conf_sample
VERSION?=	1.3
DIR?=		XBone-${VERSION}
PORTNAME?=	xbone
CVSOPTIONS?=	-n
TARBALL?=	XBone-${VERSION}

# add new things to prune to the list below (appending a * to a directory
# name will remove all files there but keep the directory)
TRIM-FILES?=	xbone/action-items xbone/install xbone/isi xbone/lib/test \
		xbone/om/cert xbone/om/conf xbone/om/test xbone/rd/test \
		xbone/rd/cert xbone/v6 \
		linux .cvsignore pkg-list release_checklist 

WEB?=		/home/xbone/public_html/software/xbone/

tarball:	mdir export trim tball cleanup

mdir:		ch-gmake
	echo ">>> Check ${DIR}; make one if not exists."
	if ( test -d ${CURDIR}/${DIR} ) then { \
	  echo "    ! Temp dir ${CURDIR}/${DIR} exists!"; \
	  echo "    ! Remove it or use another directory name (DIR=...)."; \
	  exit 0; \
	} else { \
	  mkdir ${CURDIR}/${DIR}; \
	} fi

export:		ch-gmake
	echo ">>> Exporting a copy of the tree from CVSROOT:"
	echo "    - CVS export: xbone"; 
	if ( test -n "${DATE}" ) then { \
	  echo "      - Date =  ${DATE} "; \
	  cd ${CURDIR}/${DIR}; \
	  cvs -Q -d ${CVSROOT} export ${CVSOPTIONS} -D ${DATE} -d . \
	    ${MODULE}/install; \
	  cvs -Q -d ${CVSROOT} export ${CVSOPTIONS} -D ${DATE} ${MODULE}; \
	} elif ( test -n "${REV}" ) then { \
	  echo "      - Revision = ${REV} "; \
	  cd ${CURDIR}/${DIR}; \
	  cvs -Q -d ${CVSROOT} export ${CVSOPTIONS} -r ${REV} -d . \
	    ${MODULE}/install; \
	  cvs -Q -d ${CVSROOT} export ${CVSOPTIONS} -r ${REV} ${MODULE}; \
	} else { \
	  echo "      - Did not specify date or tag, use \"now\"."; \
	  cd ${CURDIR}/${DIR}; \
	  cvs -Q -d ${CVSROOT} export ${CVSOPTIONS} -D now -d . \
	    ${MODULE}/install; \
	  cvs -Q -d ${CVSROOT} export ${CVSOPTIONS} -D now ${MODULE}; \
	} fi;  \
	echo "    - CVS export: apache-conf"; \
	cd ${CURDIR}/${DIR}/; \
	cvs -Q -d ${CVSROOT} export ${CVSOPTIONS} -D now -d ${APACHE-DIR} \
	  ${APACHE-CONF}; \

trim:		ch-gmake
	echo ">>> Delete files for ISI internal use:"
	for d in ${TRIM-FILES}; do \
	  echo "    - Pruning $$d...";\
	  rm -r ${CURDIR}/${DIR}/$$d 2> /dev/null || \
	    echo "    ! Could not remove $$d from source tree."; \
	done

tball:		ch-gmake
	echo ">>> Make tarball:"
	tar cfz ${CURDIR}/${TARBALL}.tar.gz ${TARBALL} 2> /dev/null 

cleanup:	ch-gmake 
	echo ">>> Clean up."
	if ( test -d ${CURDIR}/${DIR} ) then  {\
	  rm -rf ${CURDIR}/${DIR}; \
	} fi

toweb:
	echo "==> Copy the source & port tarballs to the web directory..."
	chmod g+w *.gz
	cp ${TARBALL}.tar.gz ${WEB}

