#!/bin/sh
#
# Optionally install a supfile and start cvsup on it.
#
# Written Jan 8th, 1998.  Jordan Hubbard <jkh@FreeBSD.org>
# Last modification date: April 5th, 2000.

# Failsafe - if the environment is spammed somehow, pick a reasonable
# prefix.
if [ -z "${PREFIX}" ]; then
    PREFIX=/usr/local
fi

# We get run multiple times, so only run once for the post-install pass.
if [ "x$2" != "xPOST-INSTALL" ]; then
    exit 0;
fi

CVSUP_CMD=${PREFIX}/bin/cvsup
CVSUP_ARGS="-g -L 2"

SUPFILE=/etc/cvsupfile
C_HOST=cvsup.FreeBSD.org
C_BASE=/usr
C_PREFIX=/usr
C_RELEASE=cvs
C_RELEASETAG="."
C_SRC_TARGETS=src-all
C_OPTS="delete use-rel-suffix"

######################### misc utility functions #######################

# Usage: yesno message text ...
yesno() {
    dialog --yesno "$*" -1 -1
}

# Usage: getval varname message text ...
getval() {
    var=$1; shift
    if dialog --inputbox "$*" -1 -1 `eval echo \\${$var}` 2> /tmp/input.$$; then
	eval ${var}=\"`cat /tmp/input.$$`\"
	rm -f /tmp/input.$$
	return 0
    else
	rm -f /tmp/input.$$
	return 1
    fi
}

# Usage: message message text ...
message() {
	dialog --infobox "$*" -1 -1
}

# Usage: infomsg message text ...
infomsg() {
	dialog --msgbox "$*" -1 -1
}

# Usage runprog command args ...
runprog() {
	dialog --prgbox "$*" 20 72
}

############################## end of general functions #######################

getsrcs() {
    if dialog --title "Source selection menu" --checklist \
"
Please specify which components of /usr/src you are interested in,
or simply press return for src-all.  If you would like to build
just the kernel, for example, then select src-sys.
" -1 -1 10 \
    "src-base"		"Misc files directly under /usr/src" No \
    "src-bin"		"Sources for things in /bin" No \
    "src-contrib"	"Sources for contributed packages" No \
    "src-crypto" 	"Non-U.S. export portions of ssh/Kerberos" No \
    "src-etc"		"Sources for things in /etc" No \
    "src-games"		"Sources for things in /usr/games" No \
    "src-gnu"		"Sources for GNU utils (requires src-contrib)" No \
    "src-include"	"Sources for things in /usr/include" No \
    "src-kerberosIV"	"Sources for KerberosIV (exportable)" No \
    "src-kerberos5"	"Sources for Kerberos5 (exportable)" No \
    "src-lib"		"Sources for things in /usr/lib" No \
    "src-libexec"	"Sources for things in /usr/libexec" No \
    "src-release"	"Sources for release building tools" No \
    "src-sbin"		"Sources for things in /sbin" No \
    "src-secure" 	"Non-U.S. export DES software" No \
    "src-share"		"Sources for things in /usr/share" No \
    "src-sys"		"Sources for the kernel (/usr/src/sys)" No \
    "src-tools"		"Sources for miscellaneous tools" No \
    "src-usrbin"	"Sources for things in /usr/bin" No \
    "src-usrsbin"	"Sources for things in /usr/sbin" No \
    2>/tmp/menu.src.$$; then
	C_SRC_TARGETS=`cat /tmp/menu.src.$$`
	rm -f /tmp/menu.src.$$
	_VAL=0
    else
	rm -f /tmp/menu.src.$$
	C_SRC_TARGETS=src-all
	_VAL=1
    fi
    if [ -z "${C_SRC_TARGETS}" ]; then
	C_SRC_TARGETS=src-all
    fi
    return ${_VAL};
}

getsrctag() {
    if dialog --title "Branch selection menu" --menu \
"
Please specify which branch of FreeBSD (as designated by branch tag)
you would like to update your sources to.  If you want 2.2.x then
this would typically be the RELENG_2_2 tag.  If you're running a 4.0
system then RELENG_4 is the tag you want.  To follow -current, that
being 5.0 right now, you want the HEAD (or .) tag.  If you want to be
able to check out or compare sources from arbitrary branches at will,
select the CVS repository tag (note: you will still need to use CVS
in manually checking out and maintaining a /usr/src tree if you
choose to go this route).
" -1 -1 5 \
    "." "The 5.0-current branch (. = HEAD)" \
    "RELENG_4" "The 4.0-stable branch" \
    "RELENG_3" "The 3.0-stable branch" \
    "RELENG_2_2" "The 2.2-stable branch" \
    "cvs" "The CVS repository (all branches)" \
    "none" "Skip the updating of /usr/src" 2>/tmp/menu.src.$$; then
	C_RELEASETAG=`cat /tmp/menu.src.$$`
	rm -f /tmp/menu.src.$$
	_VAL=0
    else
	rm -f /tmp/menu.src.$$
	_VAL=1
    fi
    if [ "${C_RELEASETAG}" = "none" ]; then
	C_SRC_TARGETS=""
	return ${_VAL}
    fi

    if [ "${_VAL}" = "0" ]; then
	if yesno "Are you within the United States or Canada?"; then
	    EXPORT=YES
	fi
    fi
    return ${_VAL}
}

gethost() {
    if dialog --title "Host selection menu" --menu \
"
These are the currently known CVSup servers at the time that
this package was created (in alphabetical order by domain name).
" -1 -1 11 \
    "other" "Specify your own cvsup server." \
    "cvsup.FreeBSD.org" "U.S. server." \
    "cvsup2.FreeBSD.org" "U.S. server." \
    "cvsup3.FreeBSD.org" "U.S. server." \
    "cvsup4.FreeBSD.org" "U.S. server." \
    "cvsup5.FreeBSD.org" "U.S. server." \
    "cvsup6.FreeBSD.org" "U.S. server." \
    "cvsup7.FreeBSD.org" "U.S. server." \
    "cvsup8.FreeBSD.org" "U.S. server." \
    "cvsup.ar.FreeBSD.org" "Argentina server." \
    "cvsup.au.FreeBSD.org" "Australia server." \
    "cvsup.br.FreeBSD.org" "Brazil server." \
    "cvsup.cz.FreeBSD.org" "Czech server." \
    "cvsup.de.FreeBSD.org" "Germany server." \
    "cvsup.dk.FreeBSD.org" "Denmark server." \
    "cvsup.ee.FreeBSD.org" "Estonia server." \
    "cvsup.fi.FreeBSD.org" "Finland server." \
    "cvsup.fr.FreeBSD.org" "France server." \
    "cvsup.jp.FreeBSD.org" "Japan server." \
    "cvsup2.jp.FreeBSD.org" "Japan server." \
    "cvsup.kr.FreeBSD.org" "Korea server." \
    "cvsup.nl.FreeBSD.org" "Holland server." \
    "cvsup.pl.FreeBSD.org" "Poland server." \
    "cvsup.ru.FreeBSD.org" "Russia server." \
    "cvsup.se.FreeBSD.org" "Sweden server." \
    "cvsup.sk.FreeBSD.org" "Slovak Republic server." \
    "cvsup.si.FreeBSD.org" "Slovenia server." \
    "cvsup.tw.FreeBSD.org" "Taiwan server." \
    "cvsup2.tw.FreeBSD.org" "Taiwan server." \
    "cvsup.uk.FreeBSD.org" "UK server." \
    "cvsup2.uk.FreeBSD.org" "UK server." \
    "cvsup3.uk.FreeBSD.org" "UK server." \
    "cvsup.za.FreeBSD.org" "South Africa server." 2>/tmp/menu.src.$$; then
	C_HOST=`cat /tmp/menu.src.$$`;
	rm -f /tmp/menu.src.$$;
	if [ "${C_HOST}" = "other" ]; then
		C_HOST=""
		if ! getval C_HOST "Enter the name of the cvsup server you wish to use."; then
			C_HOST=cvsup.freebsd.org
		fi
	fi
	return 0
    else
	rm -f /tmp/menu.src.$$
	return 1
    fi
}

getbase() {
    getval C_BASE "Base directory for src, ports and docs subdirs?"
}

getopts() {
    getval C_OPTS "These are the options currently set for cvsup.

If you are on a slow link, you may wish to add the \"compress\" option,
otherwise you probably just want to leave these values the way they are."
}

getports() {
    if yesno "Would you like to track the FreeBSD ports collection?"; then
	PORTS=yes
	return 0
    else
	return 1
    fi
}

getdocs() {
    if yesno "Would you like to track the FreeBSD doc sources?"; then
	DOCS=yes
	return 0
    else
	return 1
    fi
}

writefile() {
    echo '*default ' "host=${C_HOST}" > ${SUPFILE}
    echo '*default ' "base=${C_BASE}" >> ${SUPFILE}
    echo '*default ' "prefix=${C_BASE}" >> ${SUPFILE}
    echo '*default ' "release=${C_RELEASE}" >> ${SUPFILE}
    if [ "${C_RELEASETAG}" != "cvs" ]; then
	echo '*default ' "tag=${C_RELEASETAG}" >> ${SUPFILE}
    fi
    echo '*default ' "${C_OPTS}" >> ${SUPFILE}

    # Serious kludge to do this here, but best place for it.
    mkdir -p ${C_BASE}

    echo >> ${SUPFILE}
    if [ "${C_SRC_TARGETS}" ]; then
	if [ -z "${EXPORT}" ]; then
	    echo '*default ' "host=cvsup.internat.freebsd.org" > ${SUPFILE}.intl
	    echo '*default ' "base=${C_BASE}" >> ${SUPFILE}.intl
	    echo '*default ' "prefix=${C_BASE}" >> ${SUPFILE}.intl
	    echo '*default ' "release=${C_RELEASE}" >> ${SUPFILE}.intl
	    if [ "${C_RELEASETAG}" != "cvs" ]; then
		echo '*default ' "tag=${C_RELEASETAG}" >> ${SUPFILE}.intl
	    fi
	    echo '*default ' "${C_OPTS}" >> ${SUPFILE}.intl
	    echo >> ${SUPFILE}.intl
	fi
	if [ "${C_SRC_TARGETS}" != "src-all" ]; then
	    for i in ${C_SRC_TARGETS}; do
		if [ "$i" = "\"src-crypto\"" -o "$i" = "\"src-secure\"" ]; then
		    if [ "${EXPORT}" ]; then
			eval echo $i >> ${SUPFILE}
		    else
			eval echo $i >> ${SUPFILE}.intl
		    fi
		else
		    eval echo $i >> ${SUPFILE}
		fi
	    done
	else
	    echo src-all >> ${SUPFILE}
	    if [ "${EXPORT}" ]; then
		echo src-crypto >> ${SUPFILE}
		echo src-secure >> ${SUPFILE}
	    else
		echo src-crypto >> ${SUPFILE}.intl
		echo src-secure >> ${SUPFILE}.intl
	    fi
	fi
    fi
    if [ "${C_RELEASETAG}" != "cvs" ]; then
	echo '*default tag=.' >> ${SUPFILE}
    fi
    if [ "${PORTS}" ]; then
	echo ports-all >> ${SUPFILE}
    fi
    if [ "${DOCS}" ]; then
	echo doc-all >> ${SUPFILE}
    fi
    return 0
}

########################################################################
# OK, here's where we start in on trying to actually do useful things. #
########################################################################

# Use an existing supfile?
if [ -r "${SUPFILE}" ]; then
    if ! yesno "You already have a cvsup file (${SUPFILE}). \
Would you like to use it?"; then
	mv ${SUPFILE} ${SUPFILE}.backup
    fi
fi

# They have no supfile or are ignoring it, create one from scratch.
if [ ! -f "${SUPFILE}" ]; then
    if ! getsrctag; then
	message "Aborting cvsup setup per user request."
	exit 1
    fi
    if [ "${C_RELEASETAG}" != "none" ]; then
	getsrcs
    fi
    getports
    getdocs
    if ! getbase; then
	message "Aborting cvsup setup per user request."
	exit 1
    fi
    if ! gethost; then
	message "Aborting cvsup setup per user request."
	exit 1
    fi
    getopts
    if ! writefile; then
	message "Write failure on ${SUPFILE}, aborting."
	exit 1
    fi
fi

if [ -r "${SUPFILE}" -a -x "${CVSUP_CMD}" ]; then
    if ! yesno "Do you wish to run the CVSup update now?"; then
	infomsg "OK, to update your system later simply type:
     \"${CVSUP_CMD} ${CVSUP_ARGS} ${SUPFILE}\"
or put the command in your /etc/daily.local file for automatic updates."
	if [ -z "${EXPORT}" ]; then
	    infomsg "Since you are outside the U.S/Canada, you should also
run this same command for ${SUPFILE}.intl"
	fi
    else
	if yesno "Are you behind a firewall?"; then
	    CVSUP_ARGS="${CVSUP_ARGS} -P -"
	fi
	if yesno "Do you want to use compression (56K or slower, yes)?"; then
	    CVSUP_ARGS="${CVSUP_ARGS} -z"
	fi
	message "Now running ${CVSUP_CMD} ${CVSUP_ARGS} ${SUPFILE}."
	runprog "${CVSUP_CMD} ${CVSUP_ARGS} ${SUPFILE}"
	if [ -z "${EXPORT}" -a -r ${SUPFILE}.intl ]; then
	    message "Now running ${CVSUP_CMD} ${CVSUP_ARGS} ${SUPFILE}.intl."
	    runprog "${CVSUP_CMD} ${CVSUP_ARGS} ${SUPFILE}.intl"
	fi
	infomsg "All finished!  To update your system again, simply type
	\"${CVSUP_CMD} ${CVSUP_ARGS} ${SUPFILE}\"
or put the command in your /etc/daily.local file for automatic updates."
	if [ -z "${EXPORT}" ]; then
	    infomsg "Since you are outside the U.S/Canada, you should also
run this same command for ${SUPFILE}.intl"
	fi
    fi
fi
