#!/bin/ksh

if [[ "$1" != "" ]] ; then
  print This is gnutopkg
  print @(#) gnutopkg 1.8@(#)
  print ""
  print "What this does is take ANY properly written gnu-type autoconf+automake"
  print "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)
		break
	    ;;
	    *)
	    	print Quitting
		exit 0
	    ;;
	esac
}

ARCH=`uname -p`
BASEDIR=`sed -n  's/^prefix[ 	]*=[ ]*\(.*\)/\1/p' Makefile`
NEWPKGDIR=/tmp/newpkg
TMP_PKGBASE=/tmp/pkgbuild

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

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..."
	while true ; do
	  print 'Please give a pkgname (8 letters or less) for this package'
	  read PKG

	  if [[ "$PKG" = "" ]] ; then
	  	print "Input blank. Quitting".
		exit 0
	  fi

	  if [[ ${#PKG} -gt 8 ]] ; then
		print "Sorry, too long. Try again"
	  else
		break
	  fi
	done

	while true ; do
	  print 'Please give a short (40 chars) description for this package'
	  print 'EG:  "programname - what it does"'
	  read NAME
	  if [[ "$NAME" = "" ]] ; then
	  	print "Input blank. Quitting".
		exit 0
	  fi
	  if [[ ${#NAME} -gt 40 ]] ; then
		print "Sorry, too long. Try again"
	  else
		break
	  fi
	done

	print 'Please give a category for this software'
	print '(usually "system" or "application")'
	read CATEGORY
	if [[ "$CATEGORY" = "" ]] ; then
	  	print "Input blank. Quitting".
		exit 0
	fi

	print 'If you want a VENDOR line, input it now'
	read VENDOR

	print 'If you want an EMAIL line, input it now'
	read EMAIL

	print 'If you want a VERSION line, input it now'
	read VERSION

# could optionally ask for VERSION, DESC, ... .?


	print "PKG=$PKG" 		>pkginfo
	print "NAME=$NAME" 		>>pkginfo
	print "BASEDIR=$BASEDIR"	>>pkginfo
	print "CATEGORY=$CATEGORY"	>>pkginfo
	print "ARCH=$ARCH"		>>pkginfo
	if [[ "$VERSION" != "" ]] ; then
		print "VERSION=$VERSION"	>>pkginfo
	fi
	if [[ "$VENDOR" != "" ]] ; then
		print "VENDOR=$VENDOR"		>>pkginfo
	fi
	if [[ "$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 install'
	print 'You must either create a "prototype" file by hand, or fix'
	print ' ===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..."

  (echo 'i pkginfo'; 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
