#!/bin/bash
###############################################################################
# Release Manager   (c) 2007-2009 by Itzchak Rehberg (devel AT izzysoft DOT de)
# -----------------------------------------------------------------------------
# A FrontEnd to (and extender of) pkgmake
###############################################################################
# $Id: relman 51 2009-02-02 10:54:51Z izzy $

#===============================================[ Setup Script environment ]===
#------------------------------------[ Read some presets from pkgmake.conf ]---
[ -f /etc/pkgmake/pkgmake.conf ] && . /etc/pkgmake/pkgmake.conf
[ -f ~/.pkgmake/pkgmake.conf ] && . ~/.pkgmake/pkgmake.conf

#---------------------------------------------------------------[ Settings ]---
pkgroot="/usr/src/debian"
pkgmake=pkgmake
pkgmakeparms="-q -q"
release=izzy1
buildarch=noarch
mktar=1
mkdeb=1
mkrpm=0
mkchangelog=0
# ftp uses the ~/.netrc file for login information:
tarftp=
tarftpdir=
tarscp=
tarrsync=
tarsftp=
tarsftpdir=
debftp=
debftpdir=
debscp=
debrsync=
debsftp=
debsftpdir=
sdebftp=
sdebftpdir=
sdebscp=
sdebrsync=
sdebsftp=
sdebsftpdir=
rpmftp=
rpmftpdir=
rpmscp=
rpmrsync=
rpmsftp=
rpmsftpdir=
srpmftpdir=
srpmftp=
srpmscp=
srpmrsync=
srpmsftp=
srpmsftpdir=
pkgbftp=
pkgbftpdir=
pkgbscp=
pkgbrsync=
pkgbsftp=
pkgbsftpdir=
prerun=
prebuild=
postbuild=
postrun=
codeget=cp
histfile=
histscp=
histrsync=
histsftp=
histsftpdir=
descaddpkginfo=0

BINDIR=${0%/*}				# where this script resides
dispdetails=1
dryrun=0
yestoall=0
RELMANV=0.0.0				# dummy - real value in /etc/relman/version

#-----------------------------------------------------------------[ Colors ]---
red='\e[0;31m'
blue='\e[0;34m'
blink='\E[5m'
NC='\e[0m'              # No Color

#-----------------------------------------------[ Show progress (or don't) ]---
function say {
  [ $SILENT -lt 3 ] && echo -e "$1"
}

#---------------------------------------------------[ Output script header ]---
function header {
  [ $SILENT -lt 3 ] && clear
  say "${blue}relman v${RELMANV}"
  say "-------------$NC"
  say
}

#------------------------------------------------------------------[ Exits ]---
# $1 : Exit code (optional)
function finito {
  [ -e "$ChangeLog" ] && rm -f $ChangeLog
  say "${blue}* Done.$NC"
  say
  exit $1
}

#----------------------------------------------[ List configured projects ]---
function list_projects {
  typeset -i idx=0
  local TMP1=/tmp/relman1_$$
  local TMP2=/tmp/relman2_$$
  say "${blue}Configured projects:"
  say "--------------------$NC"
  say
  printf "%-15s : %20s \n" "Name" "Project"
  echo "----------------+----------------------"
  cat /etc/relman/relman.conf 2>&1 | sed -n '/[^_]*_name\=/p' >$TMP1
  cat ~/.relman/relman.conf 2>&1 | sed -n '/[^_]*_name\=/p' >>$TMP1
  cat $TMP1 | sort > $TMP2
  cat $TMP2 | while read line; do
    name=`echo $line|awk -F_ '{print $1}'`
    [ "${name:0:1}" = "#" ] && continue
    desc=`echo $line|awk -F= '{print $2}'`
    [ "${desc:0:1}" = '"' ] && desc=${desc:1}
    [ "${desc:${#desc}-1:1}" = '"' ] && desc=${desc:0:${#desc}-1}
    [ "${desc:0:1}" = "'" ] && desc=${desc:1}
    [ "${desc:${#desc}-1:1}" = "'" ] && desc=${desc:0:${#desc}-1}
    printf "%-15s : %20s \n" $name $desc
  done
  rm -f $TMP1 $TMP2
  say
  finito
}

#-----------------------------------------------------------[ Display help ]---
function show_help {
  header
  say "Syntax:"
  say "  relman"
  say "  relman --help | --list"
  say "  relman <ProjectName> <Version> [Options]"
  say
  say "Options:"
  say "  Options can be used to override what you configured for the project."
  say "  Details can be found in the man page (man relman). Available options include:"
  say
  printf "%24s : %-55s\n" "-codeget" "How to retrieve the source code"
  printf "%24s : %-55s\n" "-[no]deb" "[Don't] create Debian package"
  printf "%24s : %-55s\n" "-dryrun" "Don't execute anything, just show what would be done."
  printf "%24s : %-55s\n" "-nodebdist" "Don't distribute the Debian package at all"
  printf "%24s : %-55s\n" "-nodeb[ftp|scp|sftp|rsync]" "Don't distribute the Debian package via FTP|SCP|SFTP|RSync"
  printf "%24s : %-55s\n" "-nodescdist" "Don't distribute the HTMLized DescFile at all"
  printf "%24s : %-55s\n" "-nodesc[scp|rsync]" "Don't distribute the HTMLized DescFile via SCP|RSync"
  printf "%24s : %-55s\n" "-nodist" "Don't distribute anything, just build"
  printf "%24s : %-55s\n" "-nodisthist" "Don't distribute the history file at all"
  printf "%24s : %-55s\n" "-noftp" "Don't distribute anything to the FTP targets"
  printf "%24s : %-55s\n" "-nohist[ftp|scp|sftp|rsync]" "Don't distribute the history file via FTP|SCP|SFTP|RSync"
  printf "%24s : %-55s\n" "-nopkgbdist" "Don't distribute the PKGBUILD file (separately)"
  printf "%24s : %-55s\n" "-nopkgb[ftp|scp|sftp|rsync]" "Don't distribute the PKGBUILD file (separately) via FTP|SCP|SFTP|RSync"
  printf "%24s : %-55s\n" "-[no]rpm" "[Don't] create the [S]RPM package(s)"
  printf "%24s : %-55s\n" "-norpmdist" "Don't distribute the RPM package at all"
  printf "%24s : %-55s\n" "-norpm[ftp|scp|sftp|rsync]" "Don't distribute the RPM package via FTP|SCP|SFTP|RSync"
  printf "%24s : %-55s\n" "-norsync" "Don't distribute anything via RSync"
  printf "%24s : %-55s\n" "-noscp" "Don't distribute anything via SCP"
  printf "%24s : %-55s\n" "-nosdebdist" "Don't distribute the SDEB package at all"
  printf "%24s : %-55s\n" "-nosdeb[ftp|scp|sftp|rsync]" "Don't distribute the SDEB package via FTP|SCP|SFTP|RSync"
  printf "%24s : %-55s\n" "-nosftp" "Don't distribute anything to the SFTP targets"
  printf "%24s : %-55s\n" "-notar" "Don't create the tarball (assume it already exists)"
  printf "%24s : %-55s\n" "-notardist" "Don't distribute the tarball at all"
  printf "%24s : %-55s\n" "-notar[ftp|scp|sftp|rsync]" "Don't distribute the tarball via FTP|SCP|SFTP|RSync"
  printf "%24s : %-55s\n" "-nopre[run|build]" "Don't run the prerun|prebuild script"
  printf "%24s : %-55s\n" "-nopost[run|build]" "Don't run the postrun|postbuild script"
  printf "%24s : %-55s\n" "-p <parms>" "Pass parameters to pkgmake"
  printf "%24s : %-55s\n" "-pre[run|build] <script>" "Run specified script as prerun|prebuild"
  printf "%24s : %-55s\n" "-post[run|build] <script>" "Run specified script as postrun|postbuild"
  printf "%24s : %-55s\n" "-q" "Increase silence factor"
  printf "%24s : %-55s\n" "-r <release>" "Change the release name"
  printf "%24s : %-55s\n" "-t <name>" "Specify different template name"
  printf "%24s : %-55s\n" "-y" "Assume Yes to all questions"
  say
  finito
}

#----------------------------------------------[ Read config file (if any) ]---
# Process the first two cmdline parms first so they can be used in config
[ "${1:0:1}" != "-" ] && PRG=$1
[ "${2:0:1}" != "-" ] && version=$2
[ -f /etc/relman/version ] && . /etc/relman/version
[ -f /etc/relman/relman.conf ] && . /etc/relman/relman.conf
[ -f ~/.relman/relman.conf ] && . ~/.relman/relman.conf

#-------------------------------------------[ process command line options ]---
typeset -i SILENT=0

while [ "$1" != "" ] ; do
  case "$1" in
    -codeget) shift; _codeget=$1;;
    -deb) _mkdeb=1;;
    -dryrun) dryrun=1;;
    -nodeb) _mkdeb=0; _debscp=0; _debftp=0; _debsftp=0; _debrsync=0; _sdebftp=0; _sdebsftp=0; _sdebrsync=0;;
    -nodebdist) _debftp=0; _debscp=0; _debsftp=0; _debrsync=0;;
    -nodebftp) _debftp=0;;
    -nodebscp) _debscp=0;;
    -nodebsftp) _debsftp=0;;
    -nodescdist) _descscp=0; _descrsync=0;;
    -nodescrsync) _descrsync=0;;
    -nodescscp) _descscp=0;;
    -nodebrsync) _debrsync=0;;
    -nodist) _tarscp=0; _tarftp=0; _tarsftp=0; _tarrsync=0;
             _debftp=0; _debscp=0; _debrsync=0; _debsftp=0;
             _sdebftp=0; _sdebscp=0; _sdebrsync=0; _sdebsftp=0;
             _rpmftp=0; _rpmscp=0; _rpmrsync=0; _rpmsftp=0;
             _srpmftp=0; _srpmscp=0; _srpmrsync=0; _srpmsftp=0;
             _histftp=0; _histscp=0; _histrsync=0; _histsftp=0;
             _descscp=0; _descrsync=0; _pkgbftp=0; _pkgbsftp=0;
	     _pkgbscp=0; _pkgbrsync=0;
             ;;
    -nodisthist) _histftp=0; _histscp=0; _histrsync=0; _histsftp=0;;
    -noftp) _tarftp=0; _debftp=0; _sdebftp=0; _rpmftp=0; _srpmftp=0; _histftp=0; _pkgbftp=0;;
    -nohistftp) _histftp=0;;
    -nohistscp) _histscp=0;;
    -nohistsftp) _histsftp=0;;
    -nohistrsync) _histrsync=0;;
    -nopkgbftp) _pkgbftp=0;;
    -nopkgbrsync) _pkgbrsync=0;;
    -nopkgbscp) _pkgbscp=0;;
    -nopkgbsftp) _pkgbsftp=0;;
    -norpm) _mkrpm=0; _rpmftp=0; _rpmscp=0; _rpmrsync=0; _srpmftp=0; _srpmscp=0; _srpmrsync=0;;
    -norpmdist) _rpmftp=0; _rpmscp=0; _rpmrsync=0; _rpmsftp=0;;
    -norpmftp) _rpmftp=0;;
    -norpmscp) _rpmscp=0;;
    -norpmsftp) _rpmsftp=0;;
    -norpmrsync) _rpmrsync=0;;
    -norsync) _tarrsync=0; _debrsync=0; _sdebrsync=0; _rpmrsync=0; _srpmrsync=0; _descrsync=0; _pkgbrsync=0;;
    -noscp) _tarscp=0; _debscp=0; _sdebscp=0; _rpmscp=0; _srpmscp=0; _descscp=0; _histscp=0; _pkgbscp=0;;
    -nosdebdist) _sdebftp=0; _sdebscp=0; _sdebrsync=0; _sdebsftp=0;;
    -nosdebftp) _sdebftp=0;;
    -nosdebscp) _sdebscp=0;;
    -nosdebsftp) _sdebsftp=0;;
    -nosdebrsync) _sdebrsync=0;;
    -nosftp) _tarsftp=0; _debsftp=0; _sdebsftp=0; _rpmsftp=0; _srpmsftp=0; _histsftp=0; _pkgbsftp=0;;
    -nosrpmdist) _srpmftp=0; _srpmscp=0; _srpmrsync=0; _srpmsftp=0;;
    -notar) _mktar=0;;
    -notardist) _tarftp=0; _tarscp=0; _tarsftp=0; _tarrsync=0;;
    -notarftp) _tarftp=0;;
    -notarscp) _tarscp=0;;
    -notarsftp) _tarsftp=0;;
    -notarrsync) _tarrsync=0;;
    -noprerun) _prerun=0;;
    -noprebuild) _prebuild=0;;
    -nopostbuild) _postbuild=0;;
    -nopostrun) _postrun=0;;
    -p) shift; _pkgmakeparms="${_pkgmakeparms} $1";;
    -prerun) shift; _prerun=$1;;
    -prebuild) shift; _prebuild=$1;;
    -postbuild) shift; _postbuild=$1;;
    -postrun) shift; _postrun=$1;;
    -q) let SILENT=$SILENT+1;;
    -r) shift; release=$1;;
    -rpm) _mkrpm=1;;
    -t) shift; _spectpl="${BUILDDEB}/SPECS/$1";;
    -y) yestoall=1;;
    --help) show_help;;
    --list) list_projects;;
  esac
  shift
done

#-----------------------------------------------[ Process cmd line options ]---
function getopts {
  [ -n "${_mkdeb}" ] && mkdeb=${_mkdeb}
  [ -n "${_mkrpm}" ] && mkrpm=${_mkrpm}
  [ -n "${_mktar}" ] && mktar=${_mktar}
  for varname in tarftp tarscp tarsftp tarrsync debftp debsftp debscp debrsync pkgbftp pkgbrsync pkgbscp pkgbsftp sdebftp sdebsftp sdebscp sdebrsync rpmftp rpmsftp rpmscp rpmrsync srpmftp srpmsftp srpmscp srpmrsync prerun prebuild postbuild postrun codeget histscp histftp histrsync histfile histdate; do
    eval ba=\$"_${varname}"
    [ -n "$ba" ] && eval ${varname}=$ba
    [ "${!varname}" = "0" ] && eval ${varname}=""
  done
  [ -n "${_pkgmakeparms}" ] && pkgmakeparms=${_pkgmakeparms}
  [ -n "${_spectpl}" ] && spectpl=${_spectpl}
}

#------------------[ Read one char user input and convert it to lower case ]---
function yesno {
  read -n 1 -p "" ready
  echo
  res=`echo $ready|tr [:upper:] [:lower:]`
}

function stayorgo {
  [ "$res" != "y" ] && {
    echo "* Process canceled."
    exit 0
  }
  echo
}

#---------------------------------------[ Ask Parm vals ($1/$2 not passed) ]---
function getname {
  echo -en "${blue}Enter package name:$NC "
  read PRG
  [ ${PRG:0:1} = "-" ] && {
    echo -e "${red}Package name may not start with a \"-\" sign. Try again.$NC"
    getname
  }
}

function getversion {
  echo -en "${blue}Enter version:$NC "
  read version
  [ ${PRG:0:1} = "-" ] && {
    echo -e "${red}Version number may not start with a \"-\" sign. Try again.$NC"
    getversion
  }
}

function getparam {
  echo -en "${blue}$1:$NC "
  read param
}

function yesno12 {
  echo -en "${blue}$1 (y/n)?$NC "
  read -n 1 ready
  [ -z "$ready" ] && ready=n
  echo
  res=`echo $ready|tr [:upper:] [:lower:]`
  if [ $res = "y" ]; then param=1
  else param=0; fi
}

function createyesno {
  [ $2 -eq 1 ] && echo " $1: Yes" || echo " $1: No"
}

#------------------------------------------------------[ Evaluate Settings ]--
function getprogcfg {
  eval prog=\$"${PRG}_name"
  [ -z "$prog" ] && prog=$PRG
  eval srcdir=\$"${PRG}_srcdir"
  [ -z "$srcdir" ] && srcdir="${pkgroot}/BUILD/${prog}-${version}"
  eval spectpl=\$"${PRG}_spectpl"
  eval descfile=\$"${PRG}_descfile"
  [ -n "$descfile" -a ! -e "$descfile" ] && {
    echo -e "${red}Specified DescFile ($descfile) does not exist and thus will be skipped.$NC\n"
    descfile=
  }
  for varname in buildarch release pkgmakeparms pkgbftpdir pkgbsftpdir tarftpdir tarsftpdir debftpdir debsftpdir sdebftpdir sdebsftpdir rpmftpdir rpmsftpdir srpmftpdir srpmsftpdir mktar mkdeb mkrpm histscp histrsync histftp histftpdir histfile histdate descaddpkginfo; do
    eval ba=\$"${PRG}_${varname}"
    [ -n "$ba" ] && eval "${varname}=\"$ba\""
  done
  eval group=\$"${PRG}_group"
  eval section=\$"${PRG}_section"
  for varname in descscp descrsync pkgbftp pkgbrsync pkgbscp pkgbsftp tarftp tarsftp tarscp tarrsync debftp debsftp debscp debrsync sdebftp sdebsftp sdebscp sdebrsync rpmftp rpmsftp rpmscp rpmrsync srpmftp srpmsftp srpmscp srpmrsync codeget postbuild postrun prebuild prerun; do
    eval ba=\$"${PRG}_${varname}"
    [ -n "$ba" ] && eval ${varname}=\"$ba\"
    [ "${!varname}" = "0" ] && eval "${varname}=\"\""
  done
}

#----------------------------------------------------------[ Run a command ]---
function runcmd {
  if [ $dryrun -eq 0 ]; then
    eval $1
  else
    echo -e "${blue}- Exec:$NC $1"
  fi
}

#---------------------------------------------------------------[ FTP XFer ]---
# $1 : Type (for display, e.g. "Tar" or "RPM")
# $2 : Server (e.g. ftp.sourceforge.net)
# $3 : Server Directory (e.g. incoming)
# $4 : Local Directory
# $5 : File name
# $6 : Protocol (ftp|sftp, ftp if not specified)
function ftpxfer {
  if [ "$6" = "sftp" ]; then
    local proto="SFTP"
    local cmd="sftp"
  else
    local proto="FTP"
    local cmd="ftp"
  fi
  say "${blue}* Distributing $1 via ${proto}$NC"
  if [ -e ${4}/${5} ]; then
    ftptmp=/tmp/ftp.$$
    touch $ftptmp
    [ "$cmd" != "sftp" ] && {
      echo "open $2">$ftptmp
      echo "hash" >>$ftptmp
      echo "bin" >>$ftptmp
    }
    echo "cd $3" >>$ftptmp
    echo "lcd $4" >>$ftptmp
    echo "put $5" >>$ftptmp
    echo "bye" >>$ftptmp
    if [ $dryrun -eq 0 ]; then
      if [ "$cmd" = "sftp" ]; then
        echo "sftp -b $ftptmp $2"
        sftp -b $ftptmp $2
      else
        ftp <$ftptmp
      fi
    else
      runcmd "$cmd <$ftptmp"
    fi
    rm $ftptmp
  else
    echo -e "${red}- $1 file $5 not found, cannot distribute it.$NC"
  fi
}

#---------------------------------------------------------------[ SCP XFer ]---
# $1 : Type (for display, e.g. "Tar" or "RPM")
# $2 : Source
# $3 : Target
function scpxfer {
  say "${blue}* Distributing $1 via SCP$NC"
  if [ -f $2 ]; then
    runcmd "scp $2 $3"
  else
    echo -e "${red}- $1 file $2 not found, cannot distribute it.$NC"
  fi
}

#-------------------------------------------------------------[ RSync XFer ]---
# $1 : Type (for display, e.g. "Tar" or "RPM")
# $2 : Source
# $3 : Target
function rsyncxfer {
  say "${blue}* Distributing $1 via RSync$NC"
  if [ -f $2 ]; then
    runcmd "rsync -avP -e ssh $2 $3"
  else
    echo -e "${red}- $1 file $2 not found, cannot distribute it.$NC"
  fi
}

#------------------------------------------------------------------[ Exits ]---
# $1 : Exit code (optional)
function finito {
  [ -e "$ChangeLog" ] && rm -f $ChangeLog
  say "${blue}* Done.$NC"
  say
  exit $1
}

#------------------------------------------------------[ Check for pkgmake ]---
if [ -f /etc/pkgmake/version ]; then
  . /etc/pkgmake/version
else
  header
  say "${red}Could not find the pkgmake config in /etc/pkgmake/$NC"
  say
  say "${blue}Looks like pkgmake is not installed, or a version prior to 0.1.4 is"
  say "used. Please install (or update to) the latest version of pkgmake; without"
  say "that there's nothing I can do."
  say "More information can be obtained at http://www.izzysoft.de/?topic=software$NC"
  say
  finito 2
fi

#======================================================[ Interactive Start ]===
header

[ -z "$PRG" ] && getname

getprogcfg
getopts $args
builddir="${pkgroot}/BUILD/${prog}-${version}"

[ -z "$version" ] && {
  getversion
  # If the entered package is not configured, $srcdir must be reconfigured:
    typeset -i chars=${#srcdir}
    let chars=$chars-1
    [ "${srcdir:$chars}" = "-" ] && srcdir="${pkgroot}/BUILD/${prog}-${version}"
  getparam "Source Directory ($srcdir)"
  [ -n "$param" ] && srcdir=$param
  yesno12 "Create Tar from Source Directory"
  mktar=$param
  getparam "Release ($release)"
  [ -n "$param" ] && release=$param
  getparam "Build Architecture ($buildarch)"
  [ -n "$param" ] && buildarch=$param
  yesno12 "Make Debian package"
  mkdeb=$param
  yesno12 "Make RPM package"
  mkrpm=$param
  [ $mkrpm -o $mkdeb ] && {
    getparam "Spec Template to use (${prog}.tpl)"
    [ -n "$param" ] && spectpl="$param"
  }
  yesno12 "Distribute *.tar via FTP"
  [ $param -eq 0 ] && tarftp=""
  yesno12 "Distribute *.tar via SSH"
  [ $param -eq 0 ] && tarscp=""
  yesno12 "Distribute *.deb via FTP"
  [ $param -eq 0 ] && debftp=""
  yesno12 "Distribute *.deb via SSH"
  [ $param -eq 0 ] && debscp=""
  getparam "Extra parameters to pkgmake"
  pkgmakeparms=$param
  dispdetails=1
}

tb=`echo "$builddir"|sed 's/\//\\\\\//g'` # escape slashes from builddir
for varname in prerun prebuild postbuild postrun; do
  [ -n "${!varname}" ] &&  {
    var=`echo "${!varname}"|sed "s/%p/$prog/"|sed "s/%v/$version/"|sed "s/%b/$tb/"`
    eval "${varname}=\"$var\""
  }
done

tarfile=${pkgroot}/SOURCES/${prog}-${version}.tar.gz

#=================================================[ Cleanup "Dependencies" ]===
[ $mktar -eq 0 ] && {
  tarftp=
  tarsftp=
  tarscp=
  tarrsync=
}
[ $mkdeb -eq 0 ] && {
  debftp=
  debsftp=
  debscp=
  debrsync=
  sdebftp=
  sdebscp=
  sdebsftp=
  sdebrsync=
}
[ $mkrpm -eq 0 ] && {
  rpmftp=
  rpmsftp=
  rpmscp=
  rpmrsync=
  srpmftp=
  srpmsftp=
  srpmscp=
  srpmrsync=
}

#=====================================================[ Processing Package ]===
# Printout configuration details
# $1: setting
# $2: display name
function dispdetail() {
  [ -n "$1" ] && {
    printf " %-25s: %-15s\n" "$2" "$1"
  }
}

#-----------------------[ Input verification (only when called w/o params) ]---
[ $dispdetails -eq 1 ] && {
  echo -e "${blue}Processing the following details:$NC"
  dispdetail "$prog" "Program"
  dispdetail "$version" "Version"
  dispdetail "$release" "Release"
  dispdetail "$srcdir" "Source Dir"
  dispdetail "$descfile" "DescFile"
  [ -n "$descfile" ] && createyesno "Add PkgInfo to DescFile  " $descaddpkginfo
  createyesno "Create Tar               " $mktar
  dispdetail "$buildarch" "BuildArch"
  createyesno "Debian Package           " $mkdeb
  createyesno "RPM                      " $mkrpm
  echo -n " Spec Template            : "
  if [ -z "$spectpl" ]; then echo "${BUILDDEB}/SPECS/${prog}.tpl"
  else echo $spectpl; fi
  dispdetail "$group" "RPM Group"
  dispdetail "$section" "DEB section"
  dispdetail "$tarftp" "TAR Distribution (FTP)"
  dispdetail "$tarsftp" "TAR Distribution (sFTP)"
  dispdetail "$tarscp" "TAR Distribution (SCP)"
  dispdetail "$tarrsync" "TAR Distribution (RSync)"
  dispdetail "$debftp" "DEB Distribution (FTP)"
  dispdetail "$debsftp" "DEB Distribution (sFTP)"
  dispdetail "$debscp" "DEB Distribution (SCP)"
  dispdetail "$debrsync" "DEB Distribution (RSync)"
  dispdetail "$sdebftp" "SDEB Distribution (FTP)"
  dispdetail "$sdebsftp" "SDEB Distribution (sFTP)"
  dispdetail "$sdebscp" "SDEB Distribution (SCP)"
  dispdetail "$sdebrsync" "SDEB Distribution (RSync)"
  dispdetail "$rpmftp" "RPM Distribution (FTP)"
  dispdetail "$rpmsftp" "RPM Distribution (sFTP)"
  dispdetail "$rpmscp" "RPM Distribution (SCP)"
  dispdetail "$rpmrsync" "RPM Distribution (RSync)"
  dispdetail "$srpmftp" "SRPM Distribution (FTP)"
  dispdetail "$srpmsftp" "SRPM Distribution (sFTP)"
  dispdetail "$srpmscp" "SRPM Distribution (SCP)"
  dispdetail "$srpmrsync" "SRPM Distribution (RSync)"
  dispdetail "$pkgbftp" "PKGBUILD Distribution (FTP)"
  dispdetail "$pkgbrsync" "PKGBUILD Distribution (RSync)"
  dispdetail "$pkgbscp" "PKGBUILD Distribution (SCP)"
  dispdetail "$pkgbsftp" "PKGBUILD Distribution (sFTP)"
  dispdetail "$histfile" "HistFile"
  dispdetail "$histftp" "HistFile Distri. (FTP)"
  dispdetail "$histsftp" "HistFile Distri. (sFTP)"
  dispdetail "$histscp" "HistFile Distri. (SCP)"
  dispdetail "$histrsync" "HistFile Distri. (RSync)"
  dispdetail "$descscp" "DescFile Distri. (SCP)"
  dispdetail "$descrsync" "DescFile Distri (RSync)"
  dispdetail "$prerun" "PreRun script"
  dispdetail "$prebuild" "PreBuild script"
  dispdetail "$postbuild" "PostBuild script"
  dispdetail "$postrun" "PostRun script"
  dispdetail "$pkgmakeparms" "Extra parms to pkgmake"
  createyesno "dryrun                   " $dryrun
  [ $yestoall -eq 0 ] && {
    echo -en "${blue}Continue (y/n)? $NC"
    yesno
    stayorgo
  }
  say
}

say "${blue}Preparing ${prog} v${version} for release:${NC}"
[ "$builddir" = "/" ] && {
  say "${red}Yikes! Your builddir is set to '/' - you do not really mean this?!?$NC"
  finito 7
}
[ "${builddir:0:7}" = "/BUILD/" ] && {
  say "${red}Yikes! Your builddir is set to '/BUILD/*'??? Sure you put your build"
  say "environment directly into the root? Looks more like your setting for"
  say "pkgroot is messed up. You need to fix this first.$NC"
  finito 7
}

#-----------------------------------------[ Calling PreRun script (if any) ]---
[ -n "$prerun" ] && {
  say "${blue}* Running pre-processing script: $prerun"
  runcmd "$prerun"
}

#----------------------------------------[ XFer sources to build directory ]---
if [ "$srcdir" != "$builddir" ]; then
  say "${blue}* Obtaining the code$NC"
  here=`pwd`
  [ -e $builddir ] && rm -rf $builddir
  cd $srcdir
  case $codeget in
    "cp") runcmd "mkdir -p $builddir"; runcmd "cp -pr * $builddir/";;
    "cvs") module=`cat $srcdir/CVS/Repository`
           cvsroot=`cat $srcdir/CVS/Root`
           [ -z "$module" ] && {
	     say "${red}  Could not find out the CVS module name. Perhaps the sourcedir is no CVS workdir?$NC"
	     finito 3
	   }
           runcmd "mkdir -p $builddir";
           cd ${pkgroot}/BUILD
	   cmd="cvs -d $cvsroot export -D now -d ${prog}-${version} $module"
	   case $SILENT in
	     1) cmd="$cmd >/dev/null";;
	     2) cmd="$cmd &>/dev/null";;
	   esac
           runcmd "$cmd"
	   [ $? -gt 0 ] && {
	     say "${red}  Looks like CVS aborted unnormally. So we do the same.$NC"
	     finito 20
	   }
	   ;;
    "svn") cmd="svn export -q $srcdir $builddir"
	   case $SILENT in
	     1) cmd="$cmd >/dev/null";;
	     2) cmd="$cmd &>/dev/null";;
	   esac
           runcmd "$cmd"
	   [ $? -gt 0 ] && {
	     say "${red}  Looks like SVN aborted unnormally. So we do the same.$NC"
	     finito 20
	   }
	   ;;
    "")    ;;
    *) say "${red}  The value specified for codeget ($codeget) is invalid!$NC"
       finito 4
       ;;
  esac
  rc=`cd $here`
fi

#---------------------------------------[ Calling PreBuild script (if any) ]---
[ -n "$prebuild" ] && {
  say "${blue}* Running pre-build script: $prebuild"
  runcmd "$prebuild"
}

#---------------------------------------[ Create a ChangeLog (if possible) ]---
if [ $mkchangelog -gt 0 ]; then
  if [ -n "$histfile" ]; then
    say "${blue}* Creating ChangeLog from HistFile$NC"
    ChangeLog=/tmp/ChangeLog.$$
    hstart=0
    IFS_ORI=$IFS
    IFS="§"
    set -f   # switch off file name expansion - changelog uses "*" at line start!
    while read line; do
      if [ $hstart -eq 0 ]; then
        text=`echo $line | awk '{print $1}'`      # version
	[ "$text" != "v$version" ] && continue
	text=`echo $line | awk '{print $2}'`      # releasedate
        if [ -n "$text" ]; then
          [ "${text:0:1}" = "(" ] && text=${text:1} # strip off brackets
          len=${#text}-1
          [ "${text:$len}" = ")" ] && text=${text:0:$len}
          case $histdate in
            "d.m.Y") text=`echo $text|awk -F. '{print $3"-"$2"-"$1}'`;;
            "Y-m-d") text=`echo $text|awk -F- '{print $1"-"$2"-"$3}'`;;
            "d/m/Y") text=`echo $text|awk -F. '{print $3"-"$2"-"$1}'`;;
            "m/d/Y") text=`echo $text|awk -F. '{print $3"-"$1"-"$2}'`;;
            *) text=`LANG=C date +"%Y-m-d"`;;
          esac
	  text=`LANG=C date +"%a %b %d %Y" -d $text`
	  echo "* $text $VENDOR">$ChangeLog
	else
          echo "* `LANG=C date +"%a %b %d %Y"` $VENDOR">$ChangeLog
	fi
	hstart=1
      else
        [ "${line:0:1}" = "v" ] && break;
        case ${line:0:2} in
	  "--") continue;;
	  '+ '|'! '|'* ') echo "-${line:1}">>$ChangeLog;;
	  *) echo $line >>$ChangeLog;;
	esac
      fi
    done<$histfile
    set +f
    IFS=$IFS_ORI
  fi
fi

#--------------------------------------------------------[ Calling pkgmake ]---
export PKGMAKE_USED="relman v${RELMANV}"
[ "${mkdeb}${mkrpm}${mktar}" != "000" ] && {
  say "${blue}* Calling pkgmake$NC"
  [ -n "${pkgbftp}${pkgbrsync}${pkgbscp}${pkgbsftp}" ] && {
    PKGBUILDFILE=/tmp/PKGBUILD_${prog}.$$
    pkgmakeparms="$pkgmakeparms -pkgbuildfile $PKGBUILDFILE"
  }
  cmd="$pkgmake $prog $version -r $release -b $buildarch $pkgmakeparms"
  [ -n "$spectpl" ] && cmd="$cmd -t $spectpl"
  if [ -n "$codeget" ]; then
    [ $mktar -lt 1 ] && cmd="$cmd -notar"
  else
    if [ $mktar -gt 0 ]; then cmd="$cmd -d $srcdir"; else cmd="$cmd -notar"; fi
  fi
  if [ $mkdeb -gt 0 ]; then cmd="$cmd -deb"; else cmd="$cmd -nodeb"; fi
  if [ $mkrpm -gt 0 ]; then cmd="$cmd -rpm"; else cmd="$cmd -norpm"; fi
  [ -n "$group" ] && cmd="$cmd -g \"$group\""
  [ -n "$section" ] && cmd="$cmd -section \"$section\""
  [ -e "$ChangeLog" ] && cmd="$cmd -c $ChangeLog"
  [ -e "$descfile" ] && cmd="$cmd -descfile $descfile"
  runcmd "$cmd"
}

#--------------------------------------[ Calling PostBuild script (if any) ]---
[ -n "$postbuild" ] && {
  say "${blue}* Running post-build script: $postbuild"
  runcmd "$postbuild"
}

#------------------------------------------------------------[ Continue??? ]---
[ -z "${tarscp}${tarftp}${tarsftp}${tarrsync}${pkgbftp}${pkgbrsync}${pkgbscp}${pkgbsftp}${debftp}${debsftp}${debscp}${debrsync}${sdebftp}${sdebsftp}${sdebscp}${sdebrsync}${rpmftp}${rpmsftp}${rpmscp}${rpmrsync}${srpmftp}${srpmsftp}${srpmscp}${srpmrsync}${histscp}{$histrsync}${histsftp}${descscp}${descrsync}" ] && finito

[ $yestoall -eq 0 ] && {
  echo -en "${blue}* Going to distribute. Continue (y/n)?$NC "
  yesno
  stayorgo
}

#---------------------------------------------------------------[ Tar Dist ]---
[ -n "$tarftp" ] && ftpxfer "Tar" $tarftp $tarftpdir "${pkgroot}/SOURCES" "${prog}-${version}.tar.gz"
[ -n "$tarsftp" ] && ftpxfer "Tar" "$tarsftp" "$tarsftpdir" "${pkgroot}/SOURCES" "${prog}-${version}.tar.gz" "sftp"
[ -n "$tarscp" ] && scpxfer "Tar" $tarfile $tarscp
[ -n "$tarrsync" ] && rsyncxfer "Tar" $tarfile $tarrsync

#---------------------------------------------------------------[ DEB Dist ]---
[ -n "${debscp}${debftp}${debsftp}${debrsync}" ] && {
  debdir="${pkgroot}/DEBS";
  debname="${prog}_${version}-${release}"
  case $buildarch in
    "noarch") debdir="${debdir}/all"; debfile="${debname}_all.deb"; debtarget="${debscp}/binary-all/";;
    *) debdir="${debdir}/${buildarch}"; debfile="${debname}_${buildarch}.deb"; debtarget="${debscp}/binary-${buildarch}/";;
  esac
  [ -n "$debftp" ] && ftpxfer "Deb" $debftp $debftpdir $debdir $debfile
  [ -n "$debsftp" ] && ftpxfer "Deb" $debsftp $debsftpdir $debdir $debfile "sftp"
  [ -n "$debscp" ] && scpxfer "Deb" ${debdir}/${debfile} $debtarget
  [ -n "$debrsync" ] && rsyncxfer "Deb" ${debdir}/${debfile} $debrsync
}

[ -n "${sdebscp}${sdebftp}${sdebsftp}${sdebrsync}" ] && {
  debdir="${pkgroot}/SDEBS"; debfile="${prog}-${version}-${release}.sdeb";
  [ -n "$sdebftp" ] && ftpxfer "SDeb" $sdebftp $sdebftpdir $debdir $debfile
  [ -n "$sdebsftp" ] && ftpxfer "SDeb" $sdebsftp $sdebsftpdir $debdir $debfile "sftp"
  [ -n "$sdebscp" ] && scpxfer "SDeb" ${debdir}/${debfile} $sdebscp
  [ -n "$sdebrsync" ] && rsyncxfer "SDeb" ${debdir}/${debfile} $sdebrsync
}

#---------------------------------------------------------------[ RPM Dist ]---
[ -n "${rpmscp}${rpmftp}${rpmsftp}${rpmrsync}" ] && {
  rpmdir="${pkgroot}/RPMS/${buildarch}"; rpmfile="${prog}-${version}-${release}.${buildarch}.rpm"
  [ -n "$rpmftp" ] && ftpxfer "RPM" $rpmftp $rpmftpdir $rpmdir $rpmfile
  [ -n "$rpmsftp" ] && ftpxfer "RPM" $rpmsftp $rpmsftpdir $rpmdir $rpmfile "sftp"
  [ -n "$rpmscp" ] && scpxfer "RPM" ${rpmdir}/${rpmfile} $rpmscp
  [ -n "$rpmrsync" ] && rsyncxfer "RPM" ${rpmdir}/${rpmfile} $rpmrsync
}
  
[ -n "${srpmscp}${srpmftp}${srpmsftp}${srpmrsync}" ] && {
  rpmdir="${pkgroot}/SRPMS"; rpmfile="${prog}-${version}-${release}.src.rpm";
  [ -n "$srpmftp" ] && ftpxfer "SRPM" $srpmftp $srpmftpdir $rpmdir $rpmfile
  [ -n "$srpmsftp" ] && ftpxfer "SRPM" $srpmsftp $srpmsftpdir $rpmdir $rpmfile "sftp"
  [ -n "$srpmscp" ] && scpxfer "SRPM" ${rpmdir}/${rpmfile} $srpmscp
  [ -n "$srpmrsync" ] && rsyncxfer "SRPM" ${rpmdir}/${rpmfile} $srpmrsync
}

#----------------------------------------------------------[ HistFile Dist ]---
[ -n "$histfile" ] && {
  [ -n "$histftp" ] && {
    lfile="/tmp/${prog}.hist"
    cp $histfile $lfile
    ftpxfer "HistFile" $histftp $histftpdir /tmp "${prog}.hist"
    rm -f $lfile
  }
  [ -n "$histscp" ] && scpxfer "HistFile" $histfile ${histscp}/${prog}.hist
  [ -n "$histrsync" ] && rsyncxfer "HistFile" $histfile $histrsync
}

#----------------------------------------------------------[ DescFile Dist ]---
[ -n "$descfile" -a -n "${descscp}${descrsync}" ] && {
  TMPFILE=/tmp/${prog}.desc
  sed 's/\&/\&amp\;/g' $descfile | sed 's/</\&lt\;/g' | sed -n 's/>/\&gt\;/g;p' |\
  sed 's/"/\&quot\;/g' | sed -n '1h;2,$H;${g;s/\n\n/<br>\n/g;p}' >$TMPFILE
  [ -e "/etc/relman/${prog}.dr" ] && drepfile="/etc/relman/${prog}.dr"
  [ -e "$HOME/.relman/${prog}.dr" ] && drepfile="$HOME/.relman/${prog}.dr"
  [ -n "$drepfile" ] && {
    desc=`cat "$TMPFILE"`
    while read dline; do
      desc="${desc//${dline%%;*}/${dline#*;}}"
    done<$drepfile
    echo -e "$desc">$TMPFILE
  }
  [ $descaddpkginfo -ne 0 ] && {
    [ -z "$spectpl" ] && spectpl="${BUILDDEB}/SPECS/${prog}.tpl"
    pkgarch="`sed -n '/^BuildArch:/p' $spectpl`"
    [ -n "$pkgarch" ] && {
      pkgarch="${pkgarch/BuildArch/Architecture}"
      echo -e "<br>\n${pkgarch/noarch/ALL}">>$TMPFILE
    }
    pkgarch="`sed -n '/^Requires:/p' $spectpl`"
    [ -n "$pkgarch" ] && echo -e "<br>\n$pkgarch">>$TMPFILE
  }
  [ -n "$descscp" ] && scpxfer "DescFile" $TMPFILE $descscp
  [ -n "$descrsync" ] && rsyncxfer "DescFile" $TMPFILE $descrsync
  rm -f $TMPFILE
}

#----------------------------------------------------------[ PKGBUILD Dist ]---
[ -n "${pkgbftp}${pkgbrsync}${pkgbscp}${pkgbsftp}" ] && {
  if [ -f "$PKGBUILDFILE" ]; then
    md5=$(md5sum -b ${pkgroot}/SOURCES/${prog}-${version}.tar.gz|awk '{print $1}')
    runcmd "sed -i \"s/md5sums=()/md5sums('$md5')/\" $PKGBUILDFILE"
    runcmd "sed -i \"/('')/d\"  $PKGBUILDFILE"
    [ -n "$pkgbftp" ] && ftpxfer "PKGBUILD" $pkgbftp $pkgbftpdir "${PKGBUILDFILE%/*}" "${PKGBUILDFILE##*/}"
    [ -n "$pkgbrsync" ] && rsyncxfer "PKGBUILD" "$PKGBUILDFILE" $pkgbrsync
    [ -n "$pkgbscp" ] && scpxfer "PKGBUILD" "$PKGBUILDFILE" $pkgbscp
    [ -n "$pkgbftp" ] && ftpxfer "PKGBUILD" $pkgbftp $pkgbftpdir "${PKGBUILDFILE%/*}" "${PKGBUILDFILE##*/}" "sftp"
  else
    say "${red}! PKGBUILD file not found, cannot distribute it.$NC"
  fi
  runcmd "rm -f $PKGBUILDFILE"
}

#----------------------------------------[ Calling PostRun script (if any) ]---
[ -n "$postrun" ] && {
  say "${blue}* Running post-processing script: ${postrun}$NC"
  runcmd "$postrun"
}

#----------------------------------------------------------[ Finished work ]---
finito
