#! /bin/sh
# Package Create  -  @2000 by Antonio Gallo
# agx@linux.it - www.badpenguin.org - www.linux.it/~agx
# 20 May 2000, This script is used to help a mantainer to create
#              a package to be used on Bad Penguin.
# Requirements: awk, cat, du, grep
# Mon Jul 17 20:58:43 2000, can build packages without source
# Mon Aug 21 23:07:05 2000, contribution from Biagio Lucini
# Mon Sep 11 21:58:44 2000, updated to 2.0.3 format


##### =====================      tools    =========================  #####
. /usr/local/libexec/package-library
MYVERSION="2.2.0"

##### =====================   fn_package_info          =========================  #####

fn_package_info() {
  #echoc CYAN "(*) package info"

  # CATEGORY
  ask "$PKG_CATEGORIES" "enter the package CATEGORY from this list. Current: $CATEGORY"
  read XCATEGORY
  [ "$XCATEGORY" != "" ] && CATEGORY="$XCATEGORY"
  require "CATEGORY" "$CATEGORY"
  #<lucini> - check category
  CHECKVAR=0
  for CATEGORYCHECK in $PKG_CATEGORIES; do
   if [ "$CATEGORY" = "$CATEGORYCHECK" ]; then
    CHECKVAR=1
   fi
  done
  if [ $CHECKVAR -eq 0 ]; then
    echoc RED "! invalid category, giving up."
    exit 1
  fi
  #</lucini>

  # SONAME
  ask  "$SONAME"  "enter the package SONAME without any version number"
  read XSONAME
  [ "$XSONAME" != "" ] && SONAME="$XSONAME"
  require "SONAME" "$SONAME"

  # VERSION
  ask  "$VERSION"  "enter the complete package's VERSION number"
  read XVERSION
  [ "$XVERSION" != "" ] && VERSION="$XVERSION"
  require "VERSION" "$VERSION"

  if [ "$1" == "nosave" ]; then
    export CATEGORY
    export SONAME
    export VERSION
    return 0
  fi
  
  # CONFLICTS
  SUGGESTION="${SONAME}_`echo $VERSION | cut -f 1 -d'.'`"
  ask  "$CONFLICTS"  "enter the complete CONFLICTS line. suggested $SUGGESTION"
  read XCONFLICTS
  [ "$XCONFLICTS" != "" ] && CONFLICTS="$XCONFLICTS"

  # SUMMARY
  ask  "$SUMMARY"  "enter a brief summary at least 50 chars"
  read XSUMMARY
  [ "$XSUMMARY" != "" ] && SUMMARY="$XSUMMARY"

  # COPYRIGHT
  ask "$COPYRIGHT" "enter the License of this software"
  read XCOPYRIGHT
  [ "$XCOPYRIGHT" != "" ] && COPYRIGHT="$XCOPYRIGHT"
  
  # AUTHOR
  ask  "$AUTHOR"  "(optional) enter the AUTHOR of this software"
  read XAUTHOR
  [ "$XAUTHOR" != "" ] && AUTHOR="$XAUTHOR"  
  
  # HOMEPAGE
  ask "$HOMEPAGE"  "(optional) enter the HOMEPAGE of this software"
  read XHOMEPAGE
  [ "$XHOMEPAGE" != "" ] && HOMEPAGE="$XHOMEPAGE"
  
  # ARCH
  ask "$ARCH" "enter the ARCHitecture of this package, use 'i386' or 'ANY'"
  read XARCH
  [ "$XARCH" != "" ] && ARCH="$XARCH"
  require "ARCH" "$ARCH"
    
  # OPTIM
  ask  "$OPTIM"  "enter the OPTIMized arch. of this package, use 'i386' or 'ANY'"
  read XOPTIM
  [ "$XOPTIM" != "" ] && OPTIM="$XOPTIM"  
  require "OPTIM" "$OPTIM"
  
  # MANTAINER
  ask "$MANTAINER"  "(mandatory) enter the MANTAINER of this package"
  read XMANTAINER
  [ "$XMANTAINER" != "" ] && MANTAINER="$XMANTAINER"
  require "MANTAINER" "$MANTAINER"
    
  # Save changes
  fn_write_pkginfo
  
  # read DESCRIPTION
  if [ ! -f "$PKGINST/DESCRIPTION" ]; then
    > "$PKGINST/DESCRIPTION"
  fi
  if [ -z "$PKGINST/DESCRIPTION" ]; then
    ask "press ENTER to edit the file with a full description of this package"
    read x
    edit "$PKGINST/DESCRIPTION"
  else
    ask "" "type 'edit' to to edit the full description of this package"
    read x
    [ "$x" = "edit" ] && edit "$PKGINST/DESCRIPTION"
  fi
  echo "- DESCRIPTION done."

  #echoc CYAN "(14) specify dependencies"
  #fn_read_pkginfo
  # REQUEST
  #ask "$REQUEST" "complete the REQUEST line"
  #read XREQUEST
  #if [ "$XREQUEST" != "" ] ; then
  #    REQUEST="$XREQUEST"
  #    # Save changes
  #    fn_write_pkginfo
  #fi
}


##### =====================   M  A  I  N   =========================  #####

# Banner
echo "======================================================================"
echoc CYAN  "Bad Penguin - Package Create $MYVERSION"
echo "======================================================================"

# Check Parameters
#if [ "$1" = "" ]; then
#  echoc RED "package-create: need at least 1 argument !"
#  exit 1
#fi

# Define package subdirs
fn_read_settings $1
if [ "$PKGROOT" = "" -o "$1" = "" ]; then
  echoc YELLOW "- package port not found, this is a new package "
  export SONAME="$1"
  #
  fn_package_info  nosave
  PKGROOT="$PKG_PORTSDIR/$CATEGORY/${SONAME}_$VERSION"
  echo "----------------------------------------------------------------------"
  echoc PURPLE "? Would you like to create '$PKGROOT'"
  echoc PURPLE "  If yes then press ENTER."
  read x
  mkdir -p $PKGROOT
  echoc YELLOW "! Now please fill up all the packages infoes"
  NEWPACKAGE=1
fi
PKGINST="$PKGROOT/install"
PKGINFO="$PKGINST/INFO"
PKGPATCH="$PKGINST/patches"
PKGCOMPILE="$PKGINST/compile"
PKGSCRIPT="$PKGINST/scripts"

# Check Package
if [ ! -d $PKGROOT ]; then
  echoc YELLOW "! directory $PKGROOT does not exists"
  echoc PURPLE "? press ENTER to create a new package or CTRL+C to exit"
  read x
  mkdir -p $PKGROOT
fi
# Check stucture
for i in $PKGINST $PKGCOMPILE ; do
  if [ ! -d $i ]; then
    mkdir -p $i
  fi
done
if [ ! -f $PKGINFO ]; then
  >$PKGINFO
fi

# Globals
fn_read_pkginfo
fn_package_info
# if new package
if [ "$NEWPACKAGE" = "1" ]; then
  touch $PKGCOMPILE/COMPILE
  touch $PKGCOMPILE/CUSTOMIZE
  touch $PKGCOMPILE/NOREPACK
fi  
echoc GREEN "- package create ok, please read the documentation for building the package"
exit 0
