#!/bin/bash
# (c) 2002-2005 Mathieu Roy <yeupou@gnu.org>
# This file is part of package_db_view
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#  take a look at http://gna.org/projects/pdbv
#  send comments at <pdbv-dev@gna.org>
#
#  $Id: configure,v 1.85 2006/11/18 12:07:35 yeupou Exp $

BINFILE=pdbv
CRONFILE=pdbv.cron
RCFILE=pdbvrc
ACCESSFILE=pdbv.htaccess
MAKEFILE=Makefile
MAKEFILE_PO=po/Makefile
MAKEFILE_MAN=man/Makefile
MAKEFILE_CSS=css/Makefile
PREFIX=/usr/local
CRONDIR=/etc/cron.d
RCDIR=/etc/pdbv
unset PDBV_TYPE LIBINFO LIBINFO_INCLUDED NO_EXPR

# List of perl modules.
# Spaces will be understood as line-breaks. Use __ to add a real space.
USE_LIST="strict__\"vars\" Locale::gettext POSIX Getopt::Long Term::ANSIColor__qw(:constants) File::stat Sys::Hostname Tie::IxHash"


# Get options
GETOPT_TEMP=`getopt -o hpbt --long help,prefix:,type:,build-only, \
     -n configure -- "$@"`
eval set -- "$GETOPT_TEMP"

while true ; do
    case "$1" in
	-p|--prefix) PREFIX="$2" ; shift 2 ;;
	-t|--type) FORCE_TYPE="$2" ; shift 2 ;;
	-b|--build-only) BUILD_ONLY=1 ; shift ;;
	-h|--help) HELP=1 ; break ;;
	*) break ;;
    esac
done


# show help
if [ $HELP ] ; then
        echo "options:"
        echo "  --prefix=\$PREFIX    installation prefix (default $PREFIX)"
        echo "  --type=\$TYPE        rpm or dpkg (default test)"
        exit
fi


# TESTS

item="package_db_view version"
  echo -n "checking for $item... "
  VERSION=`cat modules/initvars.pl | grep "sver = \"" | awk '{ print $4 }' | sed s/\"//g | sed s/\;//g`
  if [ "$VERSION" ]; then
      echo "$VERSION"
  else
      echo "not_understood"
  fi

# on Debian sid and woody, which doesnt support --skip-alias
# blocker
item=which
WHICH_PATH="/bin/which /usr/bin/which /usr/local/bin/which"
WHICH=""
  echo -n "checking for $item... "
  for WHICH_TEST in $WHICH_PATH; do
          if [ -x $WHICH_TEST ]; then
                  if $WHICH_TEST --version 2>&1 | grep -q "GNU which"; then
		      WHICH=$WHICH_TEST
		      which_option="--skip-alias"
		      echo "yes (GNU which)"
		      break
                  else
		      WHICH=$WHICH_TEST
		      echo "yes (non-gnu which)"
		      break
		  fi
          fi
  done  
  if [ ! $WHICH ]; then
          echo no
          echo "You are encouraged to download GNU which from ftp.gnu.org"
          exit
  fi

# If a blocker item is not found, we exit here.
BLOCKER_ITEM_LIST="perl install make gettext free uname awk"
for item in $BLOCKER_ITEM_LIST; do
  echo -n "checking for $item... "
  bin_item=`$WHICH $which_option $item 2>&1`
  if [ ! -x "$bin_item" ]; then 
      echo "no" 
      if [ ! $BUILD_ONLY ] ; then exit ; fi
  else 
      echo "yes"
  fi

done


item=gzip
  echo -n "checking for $item... "
  item=`$WHICH $which_option $item 2>&1`
  if [ ! -x "$item" ]; then echo "no" && NO_EXPR=1 ; else echo "yes" ; fi

# Database related
item=rpm
  echo -n "checking for $item... "
  item=`$WHICH $which_option $item 2>&1`
  if [ ! -x "$item" ]; then echo "no" ; else echo "yes" && PDBV_TYPE=rpm ; fi

item=dpkg
  echo -n "checking for $item... "
  item=`$WHICH $which_option $item 2>&1`
  if [ ! -x "$item" ]; then echo "no" ; else echo "yes" && PDBV_TYPE=dpkg ; fi
item=/var/lib/dpkg
  echo -n "checking whether $item exists... "
  if [ ! -e "$item" ]; then echo "no" ; else echo "yes" && DPKG_DIR=$item ; fi

# Blocker one more time - test issues from previous tests
if [ $PDBV_TYPE != "rpm" ] && [ $PDBV_TYPE != "dpkg" ]; then
  echo "Well, currently only RPM and dpkg are supported. Take a look at modules/rpm.sh, you maybe could do a similar file to support your package manager database :P"
  exit
fi
if [ $FORCE_TYPE ] ; then
    PDBV_TYPE=$FORCE_TYPE
fi
echo "selected package manager... $PDBV_TYPE"
if [ $PDBV_TYPE == "dpkg" ] && [ ! $DPKG_DIR ]; then
  echo "We have been unable to find informations about your database,"
  echo "usually found in /var/lib/dpkg"
  echo "Please, type the correct directory"
  read item
  echo -n "checking if $item exists... "
  if [ ! -e "$item" ]; then echo "no" && exit ; else echo "yes" && DPKG_DIR=$item ; fi
fi
    


# We create the "bin" file.
  echo -n "creating $BINFILE... "
  rm -f $BINFILE && touch $BINFILE

  # Headers.
  echo "#!`which perl`" > $BINFILE
  echo "# Copyright (c) 2002-2005 Mathieu Roy <yeupou@gnu.org>" >> $BINFILE
  echo "# This file is part of Package DataBase View" >> $BINFILE
  echo "# This file was generated `date`" >> $BINFILE
  echo "#" >> $BINFILE
  
  # Full authors list.
  echo "# FULL AUTHORS LIST:" >> $BINFILE
  cat AUTHORS | fmt -w 65 | sed s/^/"#     "/g >> $BINFILE
  echo "#" >> $BINFILE

  # Full GNU GPL
  echo "# LICENSE:" >> $BINFILE
  cat COPYING | fmt -w 65 | sed s/^/"#     "/g >> $BINFILE

  # Pointers.
  echo "#" >> $BINFILE
  echo "#  take a look at http://gna.org/projects/pdbv" >> $BINFILE
  echo "#  send comments at <pdbv-dev@gna.org>" >> $BINFILE
  echo "#  --\"read-only\" script, checkout the CVS tree or a tarball--" >> $BINFILE
  echo "#  --if you want to modify it with ease--" >> $BINFILE

  # Base configuration.
  for use in $USE_LIST; do
      use=`echo $use | sed s/__/\ /g`
      echo "use $use;" >> $BINFILE
  done
  echo "setlocale(LC_ALL, \"\");" >> $BINFILE
#  echo "bindtextdomain(\"$PREFIX/share/locale\");"  >> $BINFILE
  echo "textdomain(\"pdbv\");"  >> $BINFILE
  echo "our \$pdbv_type = \"$PDBV_TYPE\";" >> $BINFILE
  echo "our \$dpkg_dir=\"$DPKG_DIR\";" >> $BINFILE

  # Insertion of important modules.
  MODULES_ITEM_LIST="modules/initvars.pl modules/$PDBV_TYPE.pl modules/sysinfo.pl  modules/xhtml.pl core.pl"
# core.sh must be included at the end
  for item in $MODULES_ITEM_LIST; do
      cat -s $item | sed s/"print \"The purpose of this file is not being executed alone\"\;"//g | sed s/"exit\;"//g  | sed /^#.*$/d | sed /^$/d >> $BINFILE
  done
  echo "# end" >> $BINFILE
  chmod +x $BINFILE
  echo "done"

# we create the cronfile 
#
# /dev/null instead of &1
# see http://savannah.nongnu.org/bugs/?func=detailbug&bug_id=2691&group_id=2348
echo -n "creating $CRONFILE... "
echo "# May run on a daily basis (prefered for pdbv 1.x.x)." > $CRONFILE
echo "20 15 * * *  nobody  [ -x $PREFIX/bin/pdbv ] && $PREFIX/bin/pdbv --cron=daily" >> $CRONFILE
echo " " >> $CRONFILE
echo "# May run on a hourly basis (prefered for pdbv 2.x.x)." >> $CRONFILE
echo "32 * * * *   nobody  [ -x $PREFIX/bin/pdbv ] && $PREFIX/bin/pdbv --cron=hourly" >> $CRONFILE
echo " " >> $CRONFILE
echo "# Once per week, regenerate the whole output without tests." >> $CRONFILE
echo "# Can be useful in regard of pop-con results, or because" >> $CRONFILE
echo "# it happens that a package is replaced by a package of the" >> $CRONFILE
echo "# exact same version." >> $CRONFILE
echo "05 18 * * 1   nobody  [ -x $PREFIX/bin/pdbv ] && $PREFIX/bin/pdbv --cron=daily --force" >> $CRONFILE
echo "05 18 * * 1   nobody  [ -x $PREFIX/bin/pdbv ] && $PREFIX/bin/pdbv --cron=hourly --force" >> $CRONFILE
echo " " >> $CRONFILE
echo "done"


# we create the rcfile
echo -n "creating $RCFILE... "
echo "#!`which perl`" > $RCFILE
echo "###" >> $RCFILE
echo "### Sample pdbv initialization file .pdbvrc" >> $RCFILE
echo "###" >> $RCFILE
echo " " >> $RCFILE
echo "## You can use this file to change the default behaviour of pdbv or to" >> $RCFILE
echo "## avoid having to type many many command-line options." >> $RCFILE
echo "##" >> $RCFILE
echo "## pdbv initialization file can reside in /etc/pdbvrc" >> $RCFILE
echo "## (global, for all users) or \$HOME/.pdbvrc (for a single user)." >> $RCFILE
echo "##" >> $RCFILE
echo "## To use the settings in this file, you will have to uncomment them," >> $RCFILE
echo "## as well as change them, in most cases, as the values on the" >> $RCFILE
echo "## commented-out lines are the default values (e.g. \"off\")." >> $RCFILE
echo "##" >> $RCFILE
echo "## IMPORTANT SECURITY NOTE." >> $RCFILE
echo "## Note that this file should be execute-only for mosts users." >> $RCFILE
echo "## Only the user that actually configure the whole system should" >> $RCFILE
echo "## be able to edit. Pdbv get it's configuration by running this file." >> $RCFILE
echo "## While it offers the incredible advantages to avoid creating a boring" >> $RCFILE
echo "## config parser and to permit hackers to create very efficient config" >> $RCFILE
echo "## by using perl, if anybody can edit it, anybody can push pdbv to " >> $RCFILE
echo "## execute malicious code at start..."  >> $RCFILE
echo "## (But it's like a /etc/profile ... /etc is for admins)."  >> $RCFILE
echo "## (Default mode is 0711 for this file)."  >> $RCFILE
echo " " >> $RCFILE
echo "# Listing type." >> $RCFILE
echo "\$listing=\"all\";" >> $RCFILE
echo " " >> $RCFILE
echo "# Light - will generate faster a way less polished output." >> $RCFILE
echo "# (for instance, the listing will be reset to basic)" >> $RCFILE
echo "\$light=\"0\";" >> $RCFILE
echo " " >> $RCFILE
echo "# Output directory" >> $RCFILE
echo "\$output_dir=\"/var/www/pdbv\";" >> $RCFILE
echo " " >> $RCFILE
echo "# Run pdbv by cron on which basis: hourly/daily/no" >> $RCFILE
echo "# (no means no running pdbv via cron)" >> $RCFILE
echo "\$cron=\"hourly\";" >> $RCFILE
echo " " >> $RCFILE
echo "# You can modify the default.css or, better, you can create a new" >> $RCFILE
echo "# one. With this option, you can select with file will be used." >> $RCFILE
echo "\$css=\"/etc/pdbv/default.css\";" >> $RCFILE
echo " " >> $RCFILE
echo "# For a reason I ignore sometimes, cron ignore LC and LANG settings" >> $RCFILE
echo "# With this setting you can force usage of a LANG when run by cron" >> $RCFILE
echo "#\$cron_lang=\"fr_FR\";" >> $RCFILE
echo " " >> $RCFILE
echo "# Force. Do not performs tests, regenerate the whole output everytime" >> $RCFILE
echo "# Set this to 1 if you prefer keeping data always up to date, even if" >> $RCFILE
echo "# it means making pdbv slower." >> $RCFILE
echo "# The default behavior is to run in force mode only once per week." >> $RCFILE
echo "#\$force=\"0\";" >> $RCFILE
echo " " >> $RCFILE
echo "# Debug messages." >> $RCFILE
echo "#\$debug=\"0\";" >> $RCFILE
echo " " >> $RCFILE
echo "done"

# we create the accessfile
echo -n "creating $ACCESSFILE... "
echo "## Uncomment the following if you want the pdbv output to be" > $ACCESSFILE 
echo "## only accessible from the same computer (127.0.0.1) or from the" >> $ACCESSFILE
echo "## local network (assuming it's 192.168.1)." >> $ACCESSFILE
echo "## See the apache manual for more advanced configuration."  >> $ACCESSFILE
echo "##" >> $ACCESSFILE
echo "## Edit /etc/pdbv/htaccess !" >> $ACCESSFILE
echo "#Order Deny,Allow" >> $ACCESSFILE
echo "#Allow from 127.0.0.1 192.168.1" >> $ACCESSFILE
echo "#Deny from All" >> $ACCESSFILE
echo " " >> $ACCESSFILE
echo "done"

# we update makefiles
echo -n "updating $MAKEFILE_PO... "
echo "prefix=$PREFIX" > $MAKEFILE_PO
echo "INSTALLNLSDIR=\$(prefix)/share/locale" >> $MAKEFILE_PO
cat $MAKEFILE_PO.in >> $MAKEFILE_PO
echo "done"

echo -n "updating $MAKEFILE_MAN... "
echo "prefix=$PREFIX" > $MAKEFILE_MAN
echo "INSTALLMANDIR=\$(prefix)/share/man" >> $MAKEFILE_MAN
cat $MAKEFILE_MAN.in >> $MAKEFILE_MAN
echo "done"

echo -n "updating $MAKEFILE_CSS... "
echo "rcdir=$RCDIR" >> $MAKEFILE_CSS
cat $MAKEFILE_CSS.in >> $MAKEFILE_CSS
echo "done"

# now we create the general makefile
echo -n "creating $MAKEFILE... "
rm -f $MAKEFILE && touch $MAKEFILE


###################################
###################################

## set prefix
echo "prefix=$PREFIX" >> $MAKEFILE
echo "crondir=$CRONDIR" >> $MAKEFILE
echo "rcdir=$RCDIR" >> $MAKEFILE

## make all
echo "all:" >> $MAKEFILE
echo "	cd po && make" >> $MAKEFILE
echo "	@echo  " >> $MAKEFILE
echo "	@echo now, as root, type make install" >> $MAKEFILE

## make install
echo "install:" >> $MAKEFILE
echo "	install $BINFILE \$(prefix)/bin" >> $MAKEFILE
echo "	if [ ! -e \$(rcdir) ]; then mkdir \$(rcdir); fi" >> $MAKEFILE
echo "	install --mode=644 $CRONFILE \$(crondir)/`basename $CRONFILE .cron`" >> $MAKEFILE
echo "	install --mode=644 $ACCESSFILE \$(rcdir)/htaccess" >> $MAKEFILE
echo "	install --mode=711 $RCFILE \$(rcdir)/$RCFILE" >> $MAKEFILE
echo "	cd css && make install" >> $MAKEFILE
echo "	cd po && make install" >> $MAKEFILE
echo "	cd man && make install" >> $MAKEFILE
echo " " >> $MAKEFILE

##  po
echo "new-potfile:" >> $MAKEFILE
echo "	cd po && make new-pot-file-i-really-mean-it"  >> $MAKEFILE
echo "	cd debian && debconf2pot"  >> $MAKEFILE
echo " " >> $MAKEFILE
echo "update-po:" >> $MAKEFILE
echo "	cd po && make update-po"  >> $MAKEFILE
echo "	cd debian && debconf-updatepo" >> $MAKEFILE
echo " " >> $MAKEFILE


## make dist
## Must be made by dpkg-buildpackage
echo "dist:" >> $MAKEFILE
echo "	@if [ \"\$(rel)\" == \"\" ] ; then echo \"PLEASE give the release version as arg, like rel=1.0.0. Now exit with Ctrl-C\" && sleep 10m; fi" >> $MAKEFILE
echo "	cd .. && tar cfz pdbv-\$(rel).tar.gz pdbv && gpg --detach pdbv-\$(rel).tar.gz" >> $MAKEFILE
echo "	dpkg-buildpackage -rfakeroot">> $MAKEFILE
echo " " >> $MAKEFILE

echo " done"


## make clean
echo "clean:" >> $MAKEFILE
echo "	cd po && make clean" >> $MAKEFILE
echo "	rm -f $BINFILE $MAKEFILE $MAKEFILE_PO $MAKEFILE_MAN $MAKEFILE_CSS $CRONFILE $ACCESSFILE $RCFILE" >> $MAKEFILE
echo "	find . \( -name \"#*#\" -or -name \".#*\" -or -name \"*~\" -or -name \".*~\" \) -exec rm -f {} \;"  >> $MAKEFILE
echo " " >> $MAKEFILE

echo " done"



## help

echo " "
echo "now, type make"
