#!/bin/bash

# Copyright (c) 2002
# John Stalker
# Department of Mathematics
# Princeton University

CONFIG_FILE=${HOME}/.mkebuild
if [ -e $CONFIG_FILE ]
then
        source $CONFIG_FILE
else
        echo This appears to be the first time you have used mkebuild.
        echo I am going to make some guesses.  If any of these are wrong
        echo you should edit the file ${CONFIG_FILE}.
        echo
        MY_NAME=`awk -F":" '/^'${USER}:'/ { print $5 }' /etc/passwd`
        echo Your name is ${MY_NAME}.
        echo 'MY_NAME="'${MY_NAME}'"' >${CONFIG_FILE}
        COPYRIGHT="Gentoo Technologies, Inc."
        echo You wish to asign copyright to ${COPYRIGHT}
        echo 'COPYRIGHT="'${COPYRIGHT}'"' >>${CONFIG_FILE}
        MY_EMAIL=${USER}"@"${HOSTNAME}
        echo Your email address is ${MY_EMAIL}.
        echo 'MY_EMAIL='${MY_EMAIL} >>$CONFIG_FILE
        LICENSE="the GNU General Public License, v2"
        echo Your preferred license is ${LICENSE}.
        echo 'LICENSE="'${LICENSE}'"' >>${CONFIG_FILE}
        LOCAL_SOURCE=${HOME}
        echo You download sources to ${LOCAL_SOURCE}.
        echo LOCAL_SOURCE=${LOCAL_SOURCE} >>${CONFIG_FILE}
        BUILD_DIRECTORY=${HOME}
        echo You build packages in ${BUILD_DIRECTORY}.
        echo 'BUILD_DIRECTORY='${BUILD_DIRECTORY} >>${CONFIG_FILE}
        echo
fi
FILE_NAME=`basename $1`
SOURCE_LOCATION=`dirname $1`
PACKAGE_NAME=${FILE_NAME%.*}
FILE_EXTENSION=${FILE_NAME##*.}
if [ "${PACKAGE_NAME##*.}" = "tar" ]
then
        FILE_EXTENSION=tar.${FILE_EXTENSION}
        PACKAGE_NAME=${PACKAGE_NAME%.tar}
fi
EBUILD_FILE=${PWD}/${PACKAGE_NAME}.ebuild
echo "# Copyright" `date +"%Y"` ${COPYRIGHT} >${EBUILD_FILE}
echo "# Distributed under the terms of" ${LICENSE} >>${EBUILD_FILE}
#echo "# Author" ${MY_NAME} '<'${MY_EMAIL}'>' >>${EBUILD_FILE}
echo "# \$Header: /var/cvsroot/perl/Gentoo-Probe/t/sandbox/usr/portage/app-portage/gentoolkit/files/scripts/mkebuild,v 1.1 2004/06/20 09:09:34 linguist Exp $" >>${EBUILD_FILE}
echo >>${EBUILD_FILE}
echo 'DESCRIPTION=""' >>${EBUILD_FILE}
echo 'SRC_URI="'${SOURCE_LOCATION}'/${P}.'${FILE_EXTENSION}'"' >>${EBUILD_FILE}
echo 'HOMEPAGE="'${SOURCE_LOCATION}'/"' >>${EBUILD_FILE}
echo 'LICENSE=""' >>${EBUILD_FILE}
echo 'SLOT="0"' >>${EBUILD_FILE}
echo 'KEYWORDS="~x86"' >>${EBUILD_FILE}
echo 'IUSE=""' >>${EBUILD_FILE}
echo >>${EBUILD_FILE}
echo 'DEPEND=""' >>${EBUILD_FILE}
echo >>${EBUILD_FILE}
echo '#RDEPEND=""' >>${EBUILD_FILE}
echo 'S=${WORKDIR}/${P}' >>${EBUILD_FILE}
echo >>${EBUILD_FILE}
echo 'src_unpack() {' >>${EBUILD_FILE}
echo >>${EBUILD_FILE}
echo -e "\t"'unpack ${A}' >>${EBUILD_FILE}
echo -e "\t"'cd ${S}' >>${EBUILD_FILE}
echo >>${EBUILD_FILE}
echo '}' >>${EBUILD_FILE}
if [ -e ${LOCAL_SOURCE}/${FILE_NAME} ]
then
        echo I am assuming that $1 really 
        echo exists and that ${LOCAL_SOURCE}/${FILE_NAME}
        echo is a faithful copy.
else
        echo I didn\'t find ${LOCAL_SOURCE}/${FILE_NAME} so I will fetch
        echo $1.
        cd ${LOCAL_SOURCE}
        wget $1
fi
if [ -e ${BUILD_DIRECTORY}/${PACKAGE_NAME} ]
then
        echo
        echo I am assuming that ${BUILD_DIRECTORY}/${PACKAGE_NAME} \
                is the unpacked
        echo version of ${LOCAL_SOURCE}/${FILE_NAME}.
else
        cd ${BUILD_DIRECTORY}
        if [ $? -ne 0 ]
        then
                echo
                echo I was unable to enter the directory ${BUILD_DIRECTORY}.
                exit 1
        fi
        case "${FILE_EXTENSION}" in
                tar.gz|tgz)
                        tar xzf ${LOCAL_SOURCE}/${FILE_NAME}
                        ;;
                tar.bz2|tbz2)
                        tar xjf ${LOCAL_SOURCE}/${FILE_NAME}
                        ;;
                tar.Z|tar.z)
                        unzip ${LOCAL_SOURCE}/${FILE_NAME}
                        tar xf ${PACKAGE_NAME}.tar
                        ;;
                *)
                        echo
                        echo I can\'t figure out how to uncompress
                        echo ${LOCAL_SOURCE}/${FILE_NAME}
                        exit 1
        esac
        if [ $? -ne 0 ]
        then
                echo
                echo I was unable to uncompress ${LOCAL_SOURCE}/${FILE_NAME}.
                exit 1
        fi
fi
cd ${BUILD_DIRECTORY}/${PACKAGE_NAME}
if [ $? -ne 0 ]
then
        echo
        echo I could not change directory to ${BUILD_DIRECTORY}/${PACKAGE_NAME}
        exit 1
fi
echo
echo You might want to look at the following files for configuration and
echo dependency information:
find ${PWD} -name README -print
find ${PWD} -name README.txt -print
find ${PWD} -name INSTALL -print
find ${PWD} -name INSTALL.txt -print
find ${PWD} -name Changes -print
find ${PWD} -name CHANGES.txt -print
find ${PWD} -name FAQ.txt
find ${PWD} -name ChangeLog -print
find ${PWD} -name NEWS -print
echo >>${EBUILD_FILE}
echo 'src_compile() {' >>${EBUILD_FILE}
echo >>${EBUILD_FILE}
echo
if [ -e Imakefile ]
then
        echo I found an Imakefile.  I am assuming that we should use xmkmf.
        echo I don\'t really understand xmkmf so you will have to configure
        echo things on your own.
        echo
        echo -e "\t"'xmkmf || die' >>${EBUILD_FILE}
        echo -e "\tmake Makefiles" >>${EBUILD_FILE}
        echo -e "\tmake includes" >>${EBUILD_FILE}
        echo -e "\tmake depend" >>${EBUILD_FILES}
elif [ -e configure ]
then
        echo I found a configure file.  I am assuming that we are using autoconf
.
        echo I will take care of setting the most commonly used options.  I am
        echo including a list of all the available options to confiigure, commen
ted
        echo out.  You might want to look it over to see if I have missed anythi
ng.
        echo
        ./configure --help >.config.options
        echo -e "\t./configure \\" >>${EBUILD_FILE}
        awk '/--prefix/ { print "\t\t--prefix=/usr \\"}' \
                .config.options >>${EBUILD_FILE}
        awk '/--infodir/ { print "\t\t--infodir=/usr/share/info \\"}' \
                .config.options >>${EBUILD_FILE}
        awk '/--mandir/ { print "\t\t--mandir=/usr/share/man \\"}' \
                .config.options >>${EBUILD_FILE}
        echo -e "\t\t"'|| die "./configure failed"' >>${EBUILD_FILE}
        echo -e "#\tAvailable options to configure:" >>${EBUILD_FILE}
        awk '/--/ { print "#" $0 }' .config.options >>${EBUILD_FILE}
        echo -e "\t"'emake || die' >>${EBUILD_FILE}
elif [ -e Makefile ]
then
        echo I found a Makefile.  You should look at it and possibly prepare
        echo a patch.
        echo
        echo -e "\temake || die" >>${EBUILD_FILE}
else
        echo I couldn\'t find a Imakefile, configure script, or Makefile for
        echo this package.  You will have to figure out what to do on your
        echo own.
        echo
fi
echo '}' >>${EBUILD_FILE}
echo >>${EBUILD_FILE}
echo 'src_install () {' >>${EBUILD_FILE}
echo >>${EBUILD_FILE}
echo -e "\t"'make DESTDIR=${D} install || die' >>${EBUILD_FILE}
echo '}' >>${EBUILD_FILE}
echo >>${EBUILD_FILE}
echo >>${EBUILD_FILE}
echo >>${EBUILD_FILE}
echo I couldn\'t supply a package description for the ebuild file
echo because I don\'t know what ${PACKAGE_NAME} does.
echo
echo I am assume the hompage for this package is ${SOURCE_LOCATION}/.
echo If that is not correct you will need to edit that portion of
echo the ebuild file as well.
echo
echo I don\'t understand dependencies yet.  You will have to add any
echo dependencies you know of by hand.  Then try your ebuild script
echo out to see if there are any dependencies you don\'t know of.
echo
echo I am assuming that this package comes with a well-behaved Makefile
echo which does not install anything outside of '${DESTDIR}'.  You will
echo need to check this by looking at the portion of the Makefile
echo beginning with the line '"install:"'.

