#!/usr/bin/ksh
# Copyright (C) 2005 by Philip Brown <phil at bolthole dot com>
# Portions Copyright (C) 2005 Shawn R. Walker <binarycrusader at gmail dot com>

if [[ -n "$1" ]] ; then
	print "This is gnutopkg"
	print @(#) gnutopkg 1.8@(#)
	print ""
	print "What this does is take ANY properly written gnu-type autoconf"
	print "+ automake program, and make a Solaris package out of it."
	print "This assumes that it also uses 'automake', or at least uses the same"
	print "tradition of using DESTDIR as a tree install base."
	print ""
	print "Simply cd to the make source directory, run this program,"
	print "answer a few simple questions, and you're all set."
	print "You need to run it as the user who would normally run 'make install'"
	exit 1
fi

if [[ ! -f configure ]] ; then
	print "Sorry, you need to be in the actual source directory"
	print "./configure not found"
	exit 1
fi

if [[ ! -f config.status ]] ; then
	print "Running configure"
	print "WARNING: prefix will be defaulted, presumably to /usr/local"
	print "If you want the prefix to be something else, stop now,"
	print "and run ./configure --prefix=whatever"
	sleep 1

	./configure
fi


readYes() {
	read ans
	case $ans in
		y|Y|yes|Yes|YES)
		break
		;;
		*)
			print Quitting
		exit 0
		;;
	esac
}

ARCH=`uname -p`


if [[ -f Makefile && -r Makefile ]]; then
	MAKEFILE="Makefile"
else
	if [[ -f makefile && -r makefile ]]; then
		MAKEFILE="makefile"
	else
		if [[ -z "$BASEDIR" ]]; then
			print "ERROR: BASEDIR must be set or Makefile must be "
			      "in the current directory."
			exit 1
		fi
	fi
fi

BASEDIR=`sed -n  's/^prefix[ 	]*=[ ]*\(.*\)/\1/p' $MAKEFILE`

if [[ -z "$BASEDIR" ]] ; then
	print "ERROR: could not determine prefix variable"
	exit 1
fi

NEWPKGDIR=/tmp/newpkg
TMP_PKGBASE=/tmp/pkgbuild


print BASEDIR is $BASEDIR
print PWD is $PWD


if [[ -f prototype ]] ; then
	print "ERROR: file \"prototype\" already exists. Overwrite?"
	readYes		
fi


gen_pkginfo() {

	print "Generating pkginfo file...\n"
		while true ; do
		print "Please enter an alphanumeric name to be used as the\n" \
			"abbreviated name (up to 32 characters) for this package.\n" \
			"The first four characters should be unique to your\n" \
			"company, such as your company's stock symbol. For example,\n" \
			"packages built by Sun MicrosystemsTM all have \"SUNW\" as\n" \
			"the first four characters of their package abbreviation.\n"
		read PKG?"PKG> "

		if [[ -z "$PKG" ]] ; then
			print 'Input blank. Quitting.'
			exit 0
		else
			if [[ -n `echo $PKG | awk '/^[0-9]/ { print $1 }'` ]] ; then
				print "The first character in PKG cannot be numeric.\n"
			else
				if [[ ${#PKG} -gt 32 ]] ; then
					print "PKG must be 32 characters or less.\n"
				else
					break
				fi
			fi
		fi
	done

	while true ; do
		print "\nPlease state the use for this package, and why a user\n" \
			"needs it using up to 256 characters."
		print "EG:  \"$PKG - Provides X functionality needed for Y.\"\n"
		read NAME?"NAME> "

		if [[ -z "$NAME" ]] ; then
			print "Input blank. Quitting."
			exit 0
		else
				if [[ ${#NAME} -gt 256 ]] ; then
					print "NAME must be 256 characters or less.\n"
				else
					break
				fi
		fi
	done

	while true ; do
		print "\nYou may also specify a VERSION, that has a maximum length\n" \
			"of 256 characters, and cannot begin with a left parenthesis.\n" \
			"The suggested format is:\n"
		print "<major_revision>.<minor_revision>[.<micro_revision>]\n"
		read VERSION?"VERSION> "

		if [[ -z "$VERSION" ]] ; then
			print "Skipping VERSION."
			break
		else
			if [[ -n `echo $VERSION | awk '/^[(]/ { print $1 }'` ]] ; then
				print "The first character in VERSION cannot be a left " \
					"parenthesis.\n"
			else
				if [[ ${#VERSION} -gt 256 ]] ; then
					print "VERSION must be 256 characters or less."
				else
					break
				fi
			fi
		fi
	done

	while true ; do
		print "\nPlease enter a description for this package, up to 256\n" \
			"characters. This parameter value is used to provide the\n" \
			"installer with a description of what the package contains\n" \
			"and should build on the value provided for NAME earlier.\n"
		read DESC?"DESC> "

		if [[ -z "$DESC" ]] ; then
			print "Skipping DESC."
			break
		else
			if [[ ${#DESC} -gt 256 ]] ; then
				print 'DESC must be 256 characters or less.'
			else
				break
			fi
		fi
	done

	while true ; do
		print "\nPlease enter a category for this package, up to 16 " \
			"characters."
		print '(usually "system" or "application")\n'
		read CATEGORY?"CATEGORY> "

		if [[ -z "$CATEGORY" ]] ; then
			print "Input blank. Quitting."
			exit 0
		else
			if [[ ${#CATEGORY} -gt 16 ]] ; then
				print 'CATEGORY must be 16 characters or less.'
			else
				break
			fi
		fi
	done

	while true ; do
		print "\nYou may also specify a VENDOR, that has a maximum length\n" \
			"of 256 characters. This should identify the entity that\n" \
			"holds the software copyright.\n"
		read VENDOR?"VENDOR> "

		if [[ -z "$VENDOR" ]] ; then
			print "Skipping VENDOR."
			break
		else
			if [[ ${#VENDOR} -gt 256 ]] ; then
				print "VENDOR must be 256 characters or less."
			else
				break
			fi
		fi
	done

	while true ; do
		print "\nYou may also specify an EMAIL, that has a maximum length\n" \
			"of 256 characters. This should be an electronic address\n" \
			"where further information is available or bugs can be\n" \
			"reported.\n"
		read EMAIL?"EMAIL> "

		if [[ -z "$EMAIL" ]] ; then
			print "Skipping EMAIL."
			break
		else
			if [[ ${#EMAIL} -gt 256 ]] ; then
				print "EMAIL must be 256 characters or less."
			else
				break
			fi
		fi
	done

	print "PKG=$PKG" 		>pkginfo
	print "NAME=$NAME" 		>>pkginfo
	if [[ -n "$DESC" ]] ; then
		print "DESC=$DESC"	>>pkginfo
	fi
	print "BASEDIR=$BASEDIR"	>>pkginfo
	print "CATEGORY=$CATEGORY"	>>pkginfo
	print "ARCH=$ARCH"		>>pkginfo
	if [[ -n "$VERSION" ]] ; then
		print "VERSION=$VERSION"	>>pkginfo
	fi
	if [[ -n "$VENDOR" ]] ; then
		print "VENDOR=$VENDOR"		>>pkginfo
	fi
	if [[ -n "$EMAIL" ]] ; then
		print "EMAIL=$EMAIL"		>>pkginfo
	fi
}

if [[ -f pkginfo ]] ; then
	print File '"pkginfo"' already exists. "Use it?"
	readYes
else
	gen_pkginfo	
fi

PKG=`sed -n  's/^PKG=\(.*\)/\1/p' pkginfo`

if [[ "$PKG" = "" ]] ; then
	print ERROR: Cannot determine PKG name.
	print Suggest either removing or fixing pkginfo file
	exit 1
fi

##################################################
# And now, the actual build-and-install part
#

if [[ -d $TMP_PKGBASE ]] ; then
	print "ERROR: dir $TMP_PKGBASE already exists. Please remove"
	if [[ -d $NEWPKGDIR ]] ; then
		print "ERROR: dir $NEWPKGDIR already exists. Please remove"
	fi
	exit 1
fi
if [[ -d $NEWPKGDIR ]] ; then
	print "ERROR: dir $NEWPKGDIR already exists. Please remove"
	exit 1
fi



mkdir $TMP_PKGBASE $NEWPKGDIR

if [[ ! -f prototype ]] ; then
	grep DESTDIR $MAKEFILE >/dev/null
	if [[ $? -ne 0 ]] ; then
		print 'ERROR: $MAKEFILE does not use "$(DESTDIR)" as a base offset for'
		print 'install. You must either create a "prototype" file by hand, or'
		print 'fix ===ALL=== Makefile s used in the source tree to use it.'
		print ' '
		print ' (eg:   "install prog $(DESTDIR)/$(bindir)" )'
		exit 1
	fi
	make AM_MAKEFLAGS="DESTDIR=$TMP_PKGBASE" DESTDIR=$TMP_PKGBASE install

	if [[ $? -ne 0 ]] ; then
		print "Sorry, make install failed. Cannot continue"
		exit 1
	fi

	print "Generating prototype file from installed tree..."

	PROTO="i pkginfo";
	if [[ -f copyright && -r copyright ]] ; then
			PROTO="$PROTO\ni copyright";
	fi

	if [[ -f depend && -r depend ]] ; then
			PROTO="$PROTO\ni depend";
	fi

	if [[ -f compver && -r compver ]] ; then
			PROTO="$PROTO\ni compver";
	fi

	(echo $PROTO; pkgproto $TMP_PKGBASE/$BASEDIR= )>prototype
fi



pkgmk -o -d $NEWPKGDIR

if [[ $? -ne 0 ]] ; then
	print ERROR generating initial package instance
	exit 1
fi

PKGFILE=${PKG}-${ARCH}.pkg

touch $PKGFILE

pkgtrans -s $NEWPKGDIR $PKGFILE $PKG


if [[ $? -ne 0 ]] ; then
	print ERROR generating final package file
	exit 1
fi

print "All done! Your new package file is $PKGFILE"

rm prototype
rm -rf $NEWPKGDIR $TMP_PKGBASE
