#!/usr/bin/env bash
SETX=false
#SETX=true

# cobble 0.92
# This is pre-release stuff, don't redistribute.

# all-too-many bourne shells don't grok shell functions, so we use this
# big case instead.  Install on your path as "cobble", then:
# "rm -f make-links; ln -s cobble make-links; ./make-links" to set up the
# links.  Disadvantage: slowish, slightly harder to debug, global namespace.
# Advantage: everything needed for cobble, that comes With cobble, is in
# one place - and most of it's building blocks are useful in their own
# right anway.

# BTW, set your tab stops to every third column...

# BTW^2, this breaks if you try to source these entry points, noteably
# in bash, which passes "-bash" as $0.  It's fine under bash if you just
# run it, tho.
case "$0" in
	install-dirs|*/install-dirs)
		# this thing's output is to be eval'd

		# Configuration (for a simple configuration, you can make all of these
		# variables point to the same hierarchy).  Most sites would use /usr/local
		# for one or more of these variables.

		INSTALL_ROOT=/usr/local
		INSTALL_PACKAGES=/usr/local

		# for little things - they are all to be lumped together
		echo "INSTALL_ROOT=$INSTALL_ROOT"

		# for larger things, maybe emacs, X11...
		echo "INSTALL_PACKAGES=$INSTALL_PACKAGES"

		# for things that are the same from one architecture to another; cuts
		# down on disk space, and especially, maintanence costs, if used carefully
		echo 'INSTALL_ALLSYS=/dcslib/allsys'

		# for personal things.  Mostly here for testing, or if you have something
		# you want to use, but don't want to end up supporting.
		echo "INSTALL_PERSONAL=$HOME/trees/`systype`"

		echo 'export INSTALL_ROOT INSTALL_PACKAGES'
		echo 'export INSTALL_ALLSYS INSTALL_PERSONAL'

		if [ "$INSTALL_ROOT" != "$INSTALL_PACKAGES" ]
		then
			echo "INSTALL_ATHENA=$INSTALL_PACKAGES/athena"
			echo "INSTALL_INFO=$INSTALL_PACKAGES/info"
			echo "INSTALL_TEX=$INSTALL_PACKAGES/tex"
			echo "INSTALL_AF=$INSTALL_PACKAGES/AF"
			echo "INSTALL_INFOSYS=$INSTALL_PACKAGES/infosys"
			echo "INSTALL_TRN=$INSTALL_PACKAGES/trn-3.2"
			echo "INSTALL_DECPUBLISHING=$INSTALL_PACKAGES/DECpublishing"
			echo "INSTALL_BMDP=$INSTALL_PACKAGES/bmdp90"
			echo "INSTALL_INTERVIEWS=$INSTALL_PACKAGES/interviews"
			echo "INSTALL_NCSA=$INSTALL_PACKAGES/NCSA"
			echo "INSTALL_DQS=$INSTALL_PACKAGES/dqs"
			echo "INSTALL_KHOROS=$INSTALL_PACKAGES/khoros"
			echo "INSTALL_X=$INSTALL_PACKAGES/X"
			echo "INSTALL_EETOOLS=$INSTALL_PACKAGES/eetools"
			echo "INSTALL_EMACS=$INSTALL_PACKAGES/emacs-18.57"
			echo "INSTALL_GNU=$INSTALL_PACKAGES"
			echo "INSTALL_NCARG=$INSTALL_PACKAGES/ncarg-3.1"
			echo "INSTALL_DP=$INSTALL_PACKAGES/dp"
		else
			echo "INSTALL_ATHENA=$INSTALL_ROOT"
			echo "INSTALL_INFO=$INSTALL_ROOT"
			echo "INSTALL_TEX=$INSTALL_ROOT"
			echo "INSTALL_AF=$INSTALL_ROOT"
			echo "INSTALL_INFOSYS=$INSTALL_ROOT"
			echo "INSTALL_TRN=$INSTALL_ROOT"
			echo "INSTALL_DECPUBLISHING=$INSTALL_ROOT"
			echo "INSTALL_BMDP=$INSTALL_ROOT"
			echo "INSTALL_INTERVIEWS=$INSTALL_ROOT"
			echo "INSTALL_NCSA=$INSTALL_ROOT"
			echo "INSTALL_DQS=$INSTALL_ROOT"
			echo "INSTALL_KHOROS=$INSTALL_ROOT"
			echo "INSTALL_EETOOLS=$INSTALL_ROOT"
			echo "INSTALL_EMACS=$INSTALL_ROOT"
			echo "INSTALL_GNU=$INSTALL_ROOT"
			echo "INSTALL_NCARG=$INSTALL_ROOT"
			echo "INSTALL_DP=$INSTALL_ROOT"
		fi

		# just export the above
		echo "export INSTALL_ATHENA INSTALL_INFO INSTALL_TEX"
		echo "export INSTALL_AF INSTALL_INFOSYS INSTALL_TRN"
		echo "export INSTALL_DECPUBLISHING INSTALL_BMDP INSTALL_INTERVIEWS"
		echo "export INSTALL_NCSA INSTALL_DQS INSTALL_KHOROS"
		echo "export INSTALL_X INSTALL_EETOOLS INSTALL_EMACS"
		echo "export INSTALL_GNU INSTALL_NCARG INSTALL_DP"
		;;
	make-links|*/make-links)
		LINKS="shell-speak systype locatable subst-vars make-links \
			temp-dir subst-file link-tree install-dirs do-inst archive-name"
		if [ -f cobble ]; then
			for i in $LINKS
			do
				echo Removing $i...
				rm -f $i
				echo Creating $i...
				ln -s cobble $i
			done
		else
			echo cobble not in the current directory
			exit 1
		fi
		;;
	cobble|*/cobble)
		if [ "$SETX" = true ]
		then
			set -x
		fi

		# misc renaming.  I started liking dashes.  :)
		[ -d internal.patches ] && mv internal.patches internal-patches
		[ -d exportable.patches ] && mv exportable.patches exportable-patches
		[ -d net.patches ] && mv net.patches net-patches
		[ -d old.patches ] && mv old.patches old-patches
		[ -f cobble.info ] && mv cobble.info cobble-info
		[ -f local.script ] && mv local.script local-script
		[ -f last.archives ] && mv last.archives last-archives

		if [ -d local.patches ] || [ -d internal.patches ] || \
			[ -d exportable.patches ] || [ -d net.patches ] || \
			[ -f cobble.info ] || [ -f local.script ]
		then
			echo Old setup found.  Please update this, and
			echo start again.
			exit 1
		fi

		base="`pwd`"
		export base

		eval "`install-dirs`"

		if [ "$#" = 0 ]
		then
			echo .
		else
			echo $* | tr ' ' '\012'
		fi | while read dir
			do

				cd $base/$dir
				export dir

				echo 'Searching for an archive to extract...'
				eval "`archive-name`"
				echo "Found $archive..."

				echo package=$package vers=$vers
				export package vers

				touch last-archives
				echo "`date` `systype` $archive" >> last-archives
				tail -20 last-archives > tmp && mv tmp last-archives

				if [ -f cobble-info ] && grep '^systype$' cobble-info > /dev/null
				then
					WHICH_DIR="`systype`"
				else
					WHICH_DIR=src
				fi

				if [ -r "$WHICH_DIR" ]
				then
					echo Removing old sources... 1>&2
					if rm -rf $WHICH_DIR
					then
						:
					else
						echo Could not rm $WHICH_DIR directory 1>&2
						echo Skipping directory $dir 1>&2
						exit 1
					fi
				fi

				mkdir "$WHICH_DIR"
				cd "$WHICH_DIR"
				# geeze!
				extract="`echo \"$extract\" | sed 's#@#'"$base/$dir/$archive"'#'`"
				eval "$extract"
				[ -f UNSHAR.HDR ] && mv UNSHAR.HDR ..

				# If there are more than one file in this directory...  (yech!)
				t=`for i in *
				do
					if [ -f $i ] || [ -d $i ]
					then
						echo extant
					fi
				done | tr '\012' ' '`
				if echo $t | grep 'extant extant' > /dev/null
				then
					: multiple files are here, everything is in src dir
				else
					filename=`ls -d *`
					if [ -d $filename ]
					then
						echo "mv'ing $filename as $WHICH_DIR..."
						# only a single file here, we have a single subdir under
						# src dir
						mv * ../src2
						cd ..
						rmdir $WHICH_DIR
						mv src2 $WHICH_DIR
						cd $WHICH_DIR
					else
						: single file, but not a directory!
					fi
				fi
				# at this point, source files should be in current directory,
				# "src", which is a new child of the parent in which we started

				if [ -f ../before-patches ]
				then
					echo 'Using local before-patches file'
					# Make shell-speak spit out bourne shell commands to be eval'd.
					# You -can- just write local-script's to be /bin/sh commands,
					# but then you lose interactive source-ability for non-sh
					# people.
					eval "`shell-speak sh <  ../before-patches`"
				fi

				# allow for a collection of patches.
				for dir2 in net-patches exportable-patches internal-patches \
					`systype`-patches
				do
					if [ -d ../$dir2 ]
					then
						for file in ../$dir2/*
						do
							echo "	"$file..." " | tr -d '\012'
							# find out how many dir levels to strip - unfortunately,
							# we uncompress twice this way.  Alg: find the
							# first line starting with '--- ', grab the filename
							# that follows it (stripping off the tab and date that
							# follow), then see how many levels we need to strip,
							# before being able to stat the file.  Phew.  This is
							# fairly heuristic, bound break on something someday
							dash_line="`case $file in
								*.Z) zcat $file ;;
								*.z|*.gz) gunzip -c < $file ;;
								*.bz2) bunzip2 -c < $file ;;
								*.xz|*.lzma) xz -d -c < $file ;;
								*) cat $file ;;
							esac | grep '^--- ' | head -1 | \
								sed 's/^--- \([^	]*\).*$/\1/'`"
							if [ "$dash_line" = "" ]
							then
								echo bogus patch!
								exit 1
							fi
							strip_levels=0
							export strip_levels
							# this is getting incredible: (I mean the quoting)
							# to count the number of slashes in a line, turn
							# slashes into newlines, and count the number of lines
							slashes=`echo $dash_line | \
								tr '/' '\012' | wc -l`
							#echo found $slashes slashes 1>&2
							#pwd 1>&2
							echo "$dash_line " | tr -d '\012'
							strip_levels=`while [ $strip_levels -lt $slashes ]
								do
									#echo trying to stat $dash_line 1>&2
									if [ -f $dash_line ]
									then
										#echo succeeded 1>&2
										echo $strip_levels
										exit 0
									else
										#echo failed 1>&2
										echo "$strip_levels " | tr -d '\012' 1>&2
										strip_levels=\`expr $strip_levels + 1\`
										dash_line=\`echo $dash_line | sed 's#^[^/]*/##'\`
									fi
								done
								echo 0`
							echo $strip_levels
							case $file in
								*.Z) zcat $file ;;
								*.z|*.gz) gunzip -c < $file ;;
								*.bz2) bunzip2 -c < $file ;;
								*.xz) xz -d -c < $file ;;
								*) cat $file ;;
							esac | subst-vars | ( \
								read line
								export line
								if echo "$line" | grep '^strip levels: ' > /dev/null
								then
									strip_levels="`echo $line | \
										sed 's/^strip levels: \(.*\)$/\1/'`"
								fi
#								cd $WHICH_DIR
								(echo "$line"; cat) | patch -s -p$strip_levels
#							   cd ..
							)
						done
					fi
				done

				cd ..
#				# if a link tree (for separate compilation) has been requested, 
#				# make one, and use it instead.
#				if [ -f cobble-info ] && grep '^link tree$' cobble-info > /dev/null
#				then
#					echo "Link tree requested, building..."
#					link-tree
#				fi
				cd $WHICH_DIR

				# allow for something more general than make - especially, like
				# setting some env vars, and then doing a make.  Note that
				# shell speak will just 
				if [ -f ../local-script ]
				then
					echo 'Using local make procedure'
					# Make shell-speak spit out bourne shell commands to be eval'd.
					# You -can- just write local-script's to be /bin/sh commands,
					# but then you lose interactive source-ability for non-sh
					# people.
					eval "`shell-speak sh <  ../local-script`"
#				elif [ -f debian.rules ] && [ `systype` = linux ]
#				then
#					echo "Debian package"
#					make -f debian.rules build
				else
					# Preparation for doing a make (preference intentionally
					# given to gnu autoconf!)
					if [ -f Configure ]
					then
						echo "Autoconf (probably GNU) stuff"
						echo "   ...will Configure and make with prefix $INSTALL_GNU"
						./Configure --prefix $INSTALL_GNU
					elif [ -f configure ]
					then
						echo "Autoconf (probably GNU) stuff"
						echo "   ...will Configure and make with prefix $INSTALL_GNU"
						./configure --prefix $INSTALL_GNU
					elif [ -f Imakefile ]
					then
						echo 'X package - will xmkmf and make'
						xmkmf -a
					elif [ -f setup ]
					then
						echo 'Found setup script - will run and make'
						eval "`shell-speak sh < setup`"
					elif [ -f Makefile ] || [ -f makefile ]
					then
						echo 'Plain old [Mm]akefile found'
					else
						echo "I don't know how to build the sources" \
							"in $dir yet..." 1>&2
						exit 1
					fi
					make
				fi

				echo
				echo 'done'
				echo

			done
		;;
	shell-speak|*/shell-speak)
		# translate a neutral shell-file, into bourne or csh syntax.
		# sh/ksh/bash are supported with minimal sh syntax
		# csh/tcsh are supported with minimal csh syntax
		# from what I recall, rc should be doable, and zsh should be way trivial

		# At first such a translation seems pointless, until you start thinking
		# about passing environment variables into make for source code
		# configuration.  Then you have a problem: "I can't just use my favorite
		# shell syntax, because then many of the heretics who don't prefer My
		# shell :), won't be able to conveniently source my config files".  More
		# diplomatically, this facilitates interchange of make-config-scripts-
		# by-env-var.  I suppose it may also prove nice for maintaining
		# startup scripts for multiple shell families, from a common set of
		# sources.

		# Does assignment, procedure invocation, if/then/else (no elsif - I'm
		# still debating if there -should- be, given the "backends" at hand),
		# and while loops

		# Input is line oriented, despite the relative variability of the output
		# formats.  Avoid the temptation to use ;'s in conjunction with the
		# metasyntax...

		# IF and WHILE accept general shell commands.  If you use this, pls be
		# sure to use "test" and not "[" / "]".  Also, don't use a hard path on
		# "test", like "/bin/test" - so folks can use /bin/test, /usr/5bin/test,
		# $HOME/bin/test, or whatever they prefer.  All /bin/test's are
		# definitely not created equal.

		# ELSE doesn't really need to be translated for *sh/*csh, but we may
		# add another someday that doesn't use else, no?

		# Everything else is just passed thru.  Write carefully, paying special
		# attention to the different quoting rules.  If you're getting
		# into the periphery of quoting, you're getting away from this
		# translation's intent.

		if [ "$1" = "" ]
		then
			:
		else
			SHELL="$1"
			export SHELL
		fi

		sed '/^#shell-speak$/d' | \
		case $SHELL in
			sh|*/sh|ksh|*/ksh|bash|*/bash|bash2|*/bash2)
				echo "#!/usr/local/bin/bash"
				sed \
					-e 's/SET \([^ ][^ ]*\) \(.*\)$/\1=\2; export \1/' \
					-e 's/IF \(.*\)$/if \1; then/' \
					-e 's/ELSE$/else/' \
					-e 's/ENDIF$/fi/' \
					-e 's/WHILE \(.*\)$/while \1; do/' \
					-e 's/ENDWHILE$/done/' \
					-e 's/CASE \(.*\)$/case \1 in/' \
					-e 's/OPTION \(.*\)$/\1)/' \
					-e 's/ENDOPT$/;;/' \
					-e 's/DEFAULT$/*)/' \
					-e 's/ENDCASE$/esac/' \
					-e 's/TRUE/:/' \
					-e 's/SOURCE /. /' \
					-e 's/FOR \([^ ]*\) \(.*\)$/for \1 in \2; do/' \
					-e 's/DONE/done/'
				;;
			csh|*/csh|tcsh|*/tcsh)
				echo "#!/bin/csh"
				sed \
					-e 's/SET \([^ ][^ ]*\) \(.*\)$/setenv \1 \2/' \
					-e 's/IF \(.*\)$/if { \1 } then/' \
					-e 's/ELSE$/else/' \
					-e 's/ENDIF$/endif/' \
					-e 's/WHILE \(.*\)$/while { \1 }/' \
					-e 's/ENDWHILE$/end/' \
					-e 's/CASE \(.*\)$/switch (\1)/' \
					-e 's/OPTION \(.*\)$/case \1:/' \
					-e 's/ENDOPT$/breaksw/' \
					-e 's/DEFAULT$/default:/' \
					-e 's/ENDCASE$/endsw/' \
					-e 's/TRUE/(1)/' \
					-e 's/SOURCE /source /' \
					-e 's/FOR \([^ ]*\) \(.*\)$/for \1 in \2/' \
					-e 's/DONE/end/'
				;;
			*)
				echo "Minor apologies.  I don't grok $SHELL at this point."
				exit 1
				;;
		esac

		#!/bin/sh
		#fred=value; export fred
		#if test -f /tmp/notthere; then
		#	echo hello
		#else
		#	echo goodbye
		#fi
		#case string in
		#	str1)
		#		...
		#		;;
		#	*)
		#		...
		#		;;
		#esac
		#while true; do
		#	echo hello
		#done

		#!/bin/csh
		#setenv fred value
		#if { test -f /tmp/notthere } then
		#	echo hello
		#else
		#	echo goodbye
		#endif
		#switch (string)
		#	case str1:
		#		...
		#		breaksw
		#	default:
		#		...
		#		breaksw
		#endsw
		#while { true }
		#	echo hello
		#end
		;;
	systype|*/systype)
		# unique identifier for the current OS.  Try to avoid being overspecific,
		# so we don't get symlink-itis
		if locatable uname; then
			case "`uname -s`%`uname -r`%`uname -m`" in
				SunOS%5*%sun*)
					echo sun4-5
					;;
				SunOS%4*%sun4*)
					echo sun4-41
					;;
				SunOS%4*%sun3*)
					echo sun3-41
					;;
				ULTRIX%4.[23]%RISC)
					echo decmips
					;;
				IRIX*%4.*%IP*)
					echo sgi
					;;
				IRIX*%5.*%IP*)
					echo sgi-5
					;;
				IRIX*%6.*%IP*)
					echo sgi-5
					;;
				OSF1%*%alpha)
					echo decalpha
					;;
				Linux%*%i*86)
					if grep 'release 6' /etc/redhat-release > /dev/null 2>&1
					then
						echo linux-6
					else
						if ls /lib/libc-2* 2> /dev/null | \
							grep '/lib/libc-2' > /dev/null
						then
							echo linux-5
						else
							echo linux
						fi
					fi
					;;
				AIX%*)
					echo aix-5
					;;
				*)
					echo unknown
			esac
		elif locatable machine; then
			case "`machine`" in
				ca)
					echo romp-mach26
					;;
				*)
					echo unknown
					;;
			esac
		elif locatable mach; then
			case "`mach`" in
				i386)
					echo sun386-40
					;;
				*)
					echo unknown
					;;
			esac
		else
			echo unknown
		fi
		;;
	locatable|*/locatable)
		# This IFS stuff does indeed ignore "::" on the path - but that is
		# how I want it.  In other words, I don't mind a bit if we don't
		# look in the current directory, though it's fine if we do too - which
		# we will if "." is -explicitly- on the path.
		IFS="$IFS:"
		# the root directory should always be "executable" (else the system
		# has problems large enough to prevent cobble from running anyway!),
		# so we "test -x" it to see if "-x" is supported
		if test -x / > /dev/null 2>&1
		then
			for dir in $PATH
			do
				if test -x $dir/$1
				then
					exit 0
				fi
			done
		else
			# ultrix' /bin/sh5 has a builtin [ that groks -x, though it's
			# /bin/test does not.   Avoiding systype, because systype calls
			# locatable...  creating a nonterminated recursion.  This is
			# marginally more flexible anyway
			for dir in $PATH
			do
				if /bin/sh5 -c "[ -x $dir/$1 ]"
				then
					exit 0
				fi
			done
		fi
		exit 1
		;;
	temp-dir|*/temp-dir)
		if [ -w /var/tmp ]; then
			echo /var/tmp
		elif [ -w /usr/tmp ]; then
			echo /usr/tmp
		else
			echo /tmp
		fi
		;;
	subst-file|*/subst-file)
		TMPFILE=`temp-dir`/subst-file.$$
		for i in "$@"
		do
			# cp first preserves permissions
			cp $i $TMPFILE
			subst-vars < $TMPFILE > "$i"
		done
		;;
	subst-vars|*/subst-vars)
		eval "`install-dirs`"
		sed \
			-e 's@\@INSTALL_ROOT\@@'"$INSTALL_ROOT"'@g' \
			-e 's@\@INSTALL_PACKAGES\@@'"$INSTALL_PACKAGES"'@g' \
			-e 's@\@INSTALL_ALLSYS\@@'"$INSTALL_ALLSYS"'@g' \
			-e 's@\@INSTALL_PERSONAL\@@'"$INSTALL_PERSONAL"'@g' \
			-e 's@\@INSTALL_ATHENA\@@'"$INSTALL_ATHENA"'@g' \
			-e 's@\@INSTALL_INFO\@@'"$INSTALL_INFO"'@g' \
			-e 's@\@INSTALL_TEX\@@'"$INSTALL_TEX"'@g' \
			-e 's@\@INSTALL_AF\@@'"$INSTALL_AF"'@g' \
			-e 's@\@INSTALL_INFOSYS\@@'"$INSTALL_INFOSYS"'@g' \
			-e 's@\@INSTALL_TRN\@@'"$INSTALL_TRN"'@g' \
			-e 's@\@INSTALL_DECPUBLISHING\@@'"$INSTALL_DECPUBLISHING"'@g' \
			-e 's@\@INSTALL_BMDP\@@'"$INSTALL_BMDP"'@g' \
			-e 's@\@INSTALL_INTERVIEWS\@@'"$INSTALL_INTERVIEWS"'@g' \
			-e 's@\@INSTALL_NCSA\@@'"$INSTALL_NCSA"'@g' \
			-e 's@\@INSTALL_DQS\@@'"$INSTALL_DQS"'@g' \
			-e 's@\@INSTALL_KHOROS\@@'"$INSTALL_KHOROS"'@g' \
			-e 's@\@INSTALL_X\@@'"$INSTALL_X"'@g' \
			-e 's@\@INSTALL_EETOOLS\@@'"$INSTALL_EETOOLS"'@g' \
			-e 's@\@INSTALL_EMACS\@@'"$INSTALL_EMACS"'@g' \
			-e 's@\@INSTALL_GNU\@@'"$INSTALL_GNU"'@g' \
			-e 's@\@INSTALL_DP\@@'"$INSTALL_DP"'@g' \
			-e 's@\@INSTALL_NCARG\@@'"$INSTALL_NCARG"'@g'
		;;
	do-inst|*/do-inst)
		if [ -f last-archives ]
		then
			eval "`archive-name`"
			echo package=$package vers=$vers
			export package vers

			# we're in a cobble-ized directory right now... hop into the
			# src directory and continue.
			if [ -f cobble-info ] && grep '^systype$' cobble-info > /dev/null
			then
				WHICH_DIR="`systype`"
			else
				WHICH_DIR=src
			fi
			if [ "$1" = "-n" ]
			then
				echo cd "$WHICH_DIR"
			fi
			if cd "$WHICH_DIR"
			then
				:
			else
				echo "This looks like a cobble-directory, but I can't cd"
				echo into a \"$WHICH_DIR\" directory...  are you sure this is
				echo ready for installing\?
				exit 1
			fi
		else
			# same test as before, but also test if we're in a src directory
			if [ -f ../last-archives ]
			then
				# we're already beneath a cobble-ized directory, just continue;
				# no change needed
				:
			else
				echo "Sorry, I don't know how to install cobble stuff based"
				echo "on what I see here...  I want to see a last-archives"
				echo "file to get my bearings..."
				exit 1
			fi
		fi

		if [ -f ../inst ]
		then
			# this isn't always necessary, but it doesn't really hurt,
			# and checking for executability fails on ultrix with /bin/sh
			#[ -w ../inst ] && chmod 755 ../inst
			#../inst || sh ../inst
			eval "`install-dirs`"
			if [ "$1" = "-n" ]
			then
				echo 'cat ../inst | shell-speak | bash'
			else
				cat ../inst | shell-speak | bash
			fi
		else
			if [ "$1" = "-n" ]
			then
				make -n install
			else
				make install
			fi
		fi
		;;
	archive-name|*/archive-name)
				archive=""

				if [ -f cobble-info ]
				then
					if grep "^archive name: " cobble-info > /dev/null
					then
						t=`grep "^archive name: " cobble-info | head -1`
						archive=`echo $t | sed 's/^archive name: *//'`
					fi
				fi

				if [ -f extract ]
				then
					archive="'extract script'"
				fi

				if [ "$archive" = "" ]
				then
					# no archive name found - take a guess
					# old style gzip, could be removed someday, perhaps soon...
					for typ in .tar.gz .tgz .tar.z .tar.Z .tar .shar.gz .shar.z \
						.shar.Z .shar .tar.bz2 .tar.xz .tar.lzma
					do
						t=`for i in *$typ
						do
							if [ -f $i ]
							then
								echo extant
							fi
						done`
						if echo $t | grep extant > /dev/null 2>&1
						then
							if [ "$archive" = "" ]
							then
								archive="`ls *$typ`"
							else
								archive="$archive `ls *$typ`"
							fi
						fi
					done

					if echo $archive | grep ' ' > /dev/null
					then
						echo echo "'Too many archives in `pwd` - skipping directory $dir' 1>&2"
						exit 1
					fi

					if [ "$archive" = "" ]
					then
						if [ -f part01 ]
						then
							archive="part*"
						elif [ -f part01.z ]
						then
							archive="part*.z"
						elif [ -f part01.gz ]
						then
							archive="part*.gz"
						elif [ -f part01.xz ]
						then
							archive="part*.gz"
						elif [ -f part01.Z ]
						then
							archive="part*.Z"
						else
							echo echo "'No archives found' 1>&2"
							exit 1
						fi
					fi

				fi

				echo archive=$archive

				package=""

				case $archive in
					*.tar.xz)
						extract="'xz -d -c @ | tar xvf -'"
						package="`echo $archive | sed -e 's/\.tar\.xz//'`"
						;;
					*.tar.bz2)
						extract="'bunzip2 -c @ | tar xvf -'"
						package="`echo $archive | sed -e 's/\.tar\.bz2//'`"
						;;
					*.tar.gz|*.tar.z|*.tgz)
						# not everyone has the gzip version of zcat first on
						# their path
						extract="'gunzip -c @ | tar xvf -'"
						case $archive in
							*.tar.gz)
								package="`echo $archive | sed -e 's/\.tar\.gz//'`"
							;;	
							*.tar.z)
								package="`echo $archive | sed -e 's/\.tar\.z//'`"
							;;	
							*.tgz)
								package="`echo $archive | sed -e 's/\.tgz//'`"
							;;	
						esac
						;;
					*.tar.Z)
						extract="'zcat @ | tar xvf -'"
						package="`echo $archive | sed -e 's/\.tar\.Z//'`"
						;;
					*.tar)
						extract="'tar xvf @'"
						package="`echo $archive | sed -e 's/\.tar//'`"
						;;
					*.shar.gz|*.shar.z|*.shar.xz)
						extract="'gunzip -c < @ | unshar'"
						case $archive in
							*.shar.xz)
								package="`echo $archive | sed -e 's/\.shar\.xz//'`"
							;;	
							*.shar.gz)
								package="`echo $archive | sed -e 's/\.shar\.gz//'`"
							;;	
							*.shar.z)
								package="`echo $archive | sed -e 's/\.shar\.z//'`"
							;;	
						esac
						;;
					*.shar.Z)
						extract="'zcat @ | unshar'"
						package="`echo $archive | sed -e 's/\.shar\.Z//'`"
						;;
					*.shar)
						extract="'unshar < @'"
						package="`echo $archive | sed -e 's/\.shar//'`"
						;;
					part\*.z)
						extract="'for i in @;do echo \$i;gunzip -c < \$i|unshar; done'"
						package="`echo $archive | sed -e 's/\.z//'`"
						;;
					part\*.xz)
						extract="'for i in @;do echo \$i;xz -d -c < \$i|unshar; done'"
						package="`echo $archive | sed -e 's/\.xz//'`"
						;;
					part\*.gz)
						extract="'for i in @;do echo \$i;gunzip -c < \$i|unshar; done'"
						package="`echo $archive | sed -e 's/\.gz//'`"
						;;
					part\*.Z)
						extract="'for i in @;do echo \$i;zcat < \$i|unshar; done'"
						package="`echo $archive | sed -e 's/\.Z//'`"
						;;
					part\*)
						extract="'for i in @;do echo \$i; unshar < \$i; done'"
						package=$archive
						;;
					"'extract script'")
						extract="'`cat extract`'"
						if [ -f cobble-info ]
						then
							if grep "^package name: " cobble-info > /dev/null
							then
								t=`grep "^package name: " cobble-info | head -1`
								package=`echo $t | sed -e 's/^package name: *//'`
							fi
						fi
						;;
					*)
						echo echo "'How does one extract $archive\?  Tell cobble...'"
						exit 1
						;;
				esac

				echo extract=$extract
				echo package=$package

				# This assumes the package name is xyz-1.2.3
				# extract script will be handled separatly
				#   for now put "package name: xyz-1.2.3" in cobble-info
				vers=""
				if [ "$package" != "" ]
				then
					#vers="`echo $package | awk -F- '{print $NF}'`"
					vers="`echo $package | sed 's/^[^-]*[-_]\(.*\)$/\1/'`"
				fi

				echo vers=$vers

		;;
	link-tree|*/link-tree)
		if [ -d src ]
		then
			SYSTYPE="`systype`"
			if rm -rf "$SYSTYPE"
			then
				:
			else
				echo Could not remove old $SYSTYPE directory
				exit 1
			fi
			mkdir $SYSTYPE
			cd $SYSTYPE
			dir="`pwd`"
			cd ../src
			find . -type f -print | cpio -pdvl "$dir"
		else
			echo No src directory found.  Check your working directory.
			exit 2
		fi
		;;
	*)
		echo "Excuse me?  What did you call me?  I'm cobble, with symlinks"
		echo "Check out make-links in cobble."
		exit 1
		;;
esac