# $Header$ -*-text-*-

# Purpose: Debian information for NCO netCDF Operators project
# Notes describe procedure to manipulate Debian-specific distribution

netCDF packages:
cd ${DATA}
wget http://ftp.debian.org/debian/pool/main/n/netcdf/netcdf_3.6.1-0.1.dsc
wget http://ftp.debian.org/debian/pool/main/n/netcdf/netcdf_3.6.1.orig.tar.gz
wget http://ftp.debian.org/debian/pool/main/n/netcdf/netcdf_3.6.1-0.1.diff.gz
dpkg-source -x netcdf_3.6.1-0.1.dsc

Debian: http://packages.debian.org/unstable/math/nco.html
Gentoo: http://gentoo-stable.iq-computing.de/browse?type=version&category=app-sci&package=nco&version=2.2.0

Debian New Maintainer's Guide (Rod05) is Bible for packaging .deb's: 
/data/zender/tmp/debian.pdf
# Debian Policy Manual
file:///usr/share/doc/debian-policy/policy.html/
Questions on procedure go to Debian Mentors <debian-mentors@lists.debian.org> 

0. Install Debian packages required to build new packages
apt-get install dh-make debhelper devscripts fakeroot gnupg debian-policy developers-reference

0.5 Create key to sign packages
   gpg --gen-key

1. Create ~/nco/debian directory to hold Debian configuration files

2. Debian build procedure recommends placing entire package source in
   subdirectory of main package. 
   For starters, we wish to create .debs of tagged releases, e.g., nco-4.6.1
   First we create a clean source distribution of nco and place it in nco-4.6.1
   Until we know what is necessary, however, we just copy a snapshot
   
   2.1 Clean all build files from development directory

cd ~/nco;make distclean;cd bld;make clean;cd ~
tar cvzf ./nco/nco.tar.gz ./nco/*
cd ~/nco;tar xvzf nco.tar.gz;mv nco nco-4.6.1
/bin/rm nco.tar.gz;tar cvzf nco-4.6.1.tar.gz ./nco-4.6.1/*
cd ~/nco/nco-4.6.1
dh_make -e zender@uci.edu -f ../nco-4.6.1.tar.gz

    2.2 The preceding steps created template debian files for a .deb,
    Those files now reside in ~/nco/debian.
    They are now checked into the CVS repository of the main distribution
    Step 2 only needs to be performed once per package
    Now that the Debian template files are a part of the upstream
    directory, future work consists of building the Debian packages

3. Build new .deb package

   3.1 First, remove detritus including *.gz files in parent directory
   from previous build

   cd ~/nco;/bin/rm *.gz
   cd ~/nco/nco-4.6.1
   dpkg-buildpackage -rfakeroot > foo 2>&1
   dpkg-buildpackage -rsudo > foo 2>&1

4. Find out which packages new package needs for building
   From Rod02 p. 14

   strace -f -o /tmp/strace_log ./configure
   for x in `dpkg -S ${grep open /tmp/strace_log | perl -pe 's!.* open\(\"([^\"]*).*!$1}

5. Going backwards: How to create the Debian tarball source given
the distributed Debian files, nco_X.Y.Z-3.dsc nco_X.Y.Z-3.orig.tar.gz,
and nco_X.Y.Z-3.diff.gz  

dpkg-source -x nco_X.Y.Z-3.dsc

This dpkg-source command is functionally equivalent to applying the
Debian diff to the original source to produce Debian source:

cd /data/zender;/bin/rm -r nco-X.Y.Z.orig nco-X.Y.Z 
tar xvzf nco_X.Y.Z.orig.tar.gz # Untar original source
cp -r nco-X.Y.Z.orig nco-X.Y.Z # Create destination for patches
patch -p0 < nco_X.Y.Z-3.diff   # Patch destination with Debian diff

6. To synchronize .debs with new releases, follow this procedure:
   cd ~/nco/bld
   make tags
# Put cute version-specific string in nco_ctl.c:nco_nmn_get()
# Install correct version numbers before updating Debian
# tags-query replace 4.6.1 with X.Y.Z+1
# If tags-query replace does not work, be sure to manually change
# versions in configure.ac, debian/files, doc/ANNOUNCE, doc/debian.txt,
# doc/index.shtml, doc/nco.texi, bld/nco_dst.pl, doc/VERSION
# 20141201: Change NCO_VERSION_PATCH in src/nco.h!!!!!!!!!!!!!!!!!!!!!!
   cd ~/nco/debian;dch -b --force-distribution --distribution=unstable -v 4.6.1-1 # Update changelog (-b forces this version number)
   emacs ~/nco/bld/nco.spec # Update changelog
# For unknown reason rules file may lose its executable bit
   chmod a+x ~/nco/debian/rules
# Rebuild autotools so new version # propagates
   cd ~/nco;aclocal;autoheader;automake --foreign;autoconf
# Save all files in emacs before tagging
   ${HOME}/nco/bld/nco_dst.pl --dbg=2 --bld --cln nco-4.6.1
# Upload tarball to SF https://sourceforge.net/projects/nco/files 
   cd;scp dust.ess.uci.edu:/var/www/html/nco/src/nco-4.6.1.tar.gz .

7. Ubuntu PPA
https://help.launchpad.net/Packaging/PPA
dput NCO nco_4.6.1-2~ppa1_source.changes
sudo apt-key adv --recv-keys --keyserver keyserver.ubuntu.com

# Location of build diagnostics for mentors to help 
http://dust.ess.uci.edu/nco/src/nco_4.6.1-1.dpkg-buildpackage.txt
http://dust.ess.uci.edu/nco/src/nco_4.6.1-1.dsc
http://dust.ess.uci.edu/nco/src/nco_4.6.1-1_i386.changes
http://dust.ess.uci.edu/nco/src/nco_4.6.1-1_i386.deb
http://dust.ess.uci.edu/nco/src/nco_4.6.1.orig.tar.gz

# Becoming a Debian developer
http://www.debian.org/devel/join/newmaint
# Debian mentor FAQ
http://people.debian.org/~mpalmer/debian-mentors_FAQ.html

Debian Acronyms, Abbreviations, and Jargon:
BTS Bug Tracking System
DD  Debian Developer
DDPO Debian Developer's Packages Overview
DNMG Debian New Maintainer's Guide
FTBFS Fails to Build from Source
ITA Intent to Adopt
NMU Non-Naintainer Upload
PTS Package Tracking System
RC  Release Critical
RFP Request for Package 
RFS Request for Sponsor 
WNPP Work-Needing and Prospective Packages 
r-dep fxm
experimental fxm (alpha, newer than sid)
unstable sid (beta)
sarge Debian 3.1
etch Debian 4.0
lenny Debian 5.0
squeeze Debian 6.0

# All NCO bugs
http://bugs.debian.org/nco
# NCO orphan/ITA bug
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=273322
# netCDF orphan/ITA bug
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=321336

# Debian QA member who was de-facto NCO maintainer
Matej Vela <vela@debian.org>
# New NCO sponsor
Oleksandr (Alex) Moskalenko <malex@debian.org> 
# Former NCO mentor/sponsor
Daniel Baumann <daniel.baumann@panthera-systems.net>
Daniel Baumann <daniel@debian.org>
# Created netCDF 3.6.1 package
Warren Turkal <wt@atmos.colostate.edu>

Matej Vela <vela@debian.org>, Daniel Baumann <daniel@debian.org>, Warren Turkal <wt@atmos.colostate.edu>

# New build system #1
# This builds NCO with debian packages, and includes netCDF4, DAP, and UDUnits2 support (?)
# Non-native debian builds Rod05 p. 46
# Set path to avoid shared library conflicts between /usr and /usr/local
# export LD_LIBRARY_PATH=/usr/lib:/lib:/usr/X11R6/lib
# sudo aptitude install antlr bison flex gsl-bin libgsl0-dev libantlr-dev netcdf-bin libnetcdfc7 libnetcdf-dev texinfo libcurl4-gnutls-dev libexpat1-dev libxml2-dev udunits-bin libudunits2-0 libudunits2-dev
cd ~/nco;cvc
sudo /bin/rm -rf ${DATA}/nco-4.6.1 ${DATA}/nco_4.6.1* ${DATA}/debian # Cleanup last build. sudo necessary for removal because dpkg-buildpackage uses sudo?
# cd ${DATA};cvs -d zender@nco.cvs.sf.net:/cvsroot/nco export -kkv -r nco-4.6.1-1 -d nco-4.6.1 nco # Export based on tag
cd ${DATA};cvs -d zender@nco.cvs.sf.net:/cvsroot/nco export -kkv -D "1 second ago" -d nco-4.6.1 nco # Export most recent
tar cvzf ./nco_4.6.1.orig.tar.gz --exclude='nco-4.6.1/debian*' --exclude='.cvsignore' --exclude='ncap_lex.c' --exclude='ncap_yacc.[ch]' ./nco-4.6.1 
/bin/rm -rf ${DATA}/nco-4.6.1 # Remove cvs-exported directory
tar xvzf ./nco_4.6.1.orig.tar.gz # Untar to get directory without excluded files
mkdir -p ${DATA}/nco-4.6.1/debian/source;cd ~/nco/debian;/bin/cp changelog compat control convert copyright doc-base files info rules ${DATA}/nco-4.6.1/debian;cd ~/nco/debian/source;/bin/cp format ${DATA}/nco-4.6.1/debian/source # Replace debian directory with _CURRENT_ (main trunk) settings
#export DEB_BUILD_OPTIONS='disable-dap-netcdf disable-netcdf4 disable-udunits2'; # Disable optional packages based on available Debian support
#cd ${DATA}/nco-4.6.1;dpkg-buildpackage -rsudo -uc -us > ~/foo.nco 2>&1 # -uc -us: Do not sign changes or source files
#cd ${DATA}/nco-4.6.1;dpkg-buildpackage -rsudo -sa > ~/foo.nco 2>&1 # -sa: Include _orig.tar.gz in .changes 
cd ${DATA}/nco-4.6.1;dpkg-buildpackage -rsudo > ~/foo.nco 2>&1
sudo dpkg --remove nco
sudo dpkg --install ${DATA}/nco_4.6.1-1_*.deb
cd ~/nco/bld;MY_BIN_DIR=/usr/bin ../bm/nco_bm.pl --regress
# http://lintian.debian.org/full/zender@uci.edu.html
lintian ${DATA}/nco_4.6.1-1_*.deb
ls -l ${DATA}/nco_4.6.1*
m ~/foo.nco
# Upload Ubuntu (rather than Debian) packages to websites
scp ${DATA}/nco_4.6.1* dust.ess.uci.edu:/var/www/html/nco/src
scp ${DATA}/nco_4.6.1* zender,nco@web.sf.net:/home/project-web/nco/htdocs/src
# NB: Make sure RPMs build before uploading to debian, since changing
# Debian versions is a PITA
# NB: Only upload pbuilder Debian Sid (not personal Ubuntu) .deb builds to Debian mentors
# cd ${DATA};dupload -t mentors nco_4.6.1-1_*.changes
bsrc # Reset shell environment for regular development

# New build system #2
# Pbuilder/chroot system
# This builds .debs for submission to Debian Sid and ultimately Ubuntu
# Configuration in ~/.pbuilderrc or /etc/pbuilderrc
# https://wiki.ubuntu.com/PbuilderHowto
# sudo aptitude install debian-archive-keyring # Avoid "Release signed by unknown key" error
# sudo DIST=sid pbuilder create --debootstrapopts --keyring=/usr/share/keyrings/debian-archive-keyring.gpg
DIST=sid sudo pbuilder update # Update chroot before building package in it
# dget http://ftp.debian.org/debian/pool/main/n/nco/nco_3.9.0-1.dsc
# dget http://ftp.debian.org/debian/pool/main/n/netcdf/netcdf_3.6.1-1.dsc
# apt-get source nco # Get package source
sudo /bin/rm /var/cache/pbuilder/result/nco_4.6.1* # Cleanup prior build
# To pass DEB_BUILD_OPTIONS to pbuilder while using sudo, one must first
# modify sudoers with visudo to prevent sudo from resetting environment
#export DEB_BUILD_OPTIONS='disable-dap-netcdf disable-netcdf4 disable-udunits2'; # Disable optional packages based on available Debian support
cd ${DATA};DIST=sid sudo pbuilder build nco_4.6.1-1.dsc > ~/foo.nco.pbuilder 2>&1
cd /var/cache/pbuilder/result;debsign -k6F635D10 nco_4.6.1-1_*.changes
lintian /var/cache/pbuilder/result/nco_4.6.1-1_*.deb
sudo dpkg --remove nco
sudo dpkg --install /var/cache/pbuilder/result/nco_4.6.1-1_*.deb
cd ~/nco/bld;MY_BIN_DIR=/usr/bin ../bm/nco_bm.pl --regress
# NB: Upload pbuilder Debian Sid packages to Debian mentors, but not
# to personal or NCO websites since most people use Ubuntu not Debian
# NB: Debian versions are a PITA, ensure RPMs build before uploading to Debian
cd /var/cache/pbuilder/result;dupload -t mentors nco_4.6.1-1_*.changes

# RPM builds as root
export rpm_root='/usr/src/redhat'
# export sudo_sng='' # sudo not-necessary when builing in user directories
export sudo_sng='sudo' # sudo necessary when building in system directories
cd ~/nco;cvc;cvu
/bin/rm -rf ${DATA}/nco-4.6.1 ${DATA}/nco-4.6.1* # Cleanup last build
${sudo_sng} /bin/rm -r -f \
${rpm_root}/BUILD/nco-4.6.1 \
${rpm_root}/RPMS/i386/nco-4.6.1-?.i386.rpm \
${rpm_root}/RPMS/i386/nco-debuginfo-4.6.1-?.i386.rpm \
${rpm_root}/RPMS/i386/nco-devel-4.6.1-?.i386.rpm \
${rpm_root}/SOURCES/nco-4.6.1.tar.gz \
${rpm_root}/SPECS/nco-4.6.1.spec \
${rpm_root}/SRPMS/nco-4.6.1-?.src.rpm
cd ${DATA};cvs -d zender@nco.cvs.sf.net:/cvsroot/nco export -kkv -r nco-4.6.1-1 -d nco-4.6.1 nco # Export based on tag
${sudo_sng} ln -s ${HOME}/nco/bld/nco.spec ${rpm_root}/SPECS/nco-4.6.1.spec
tar cvzf ./nco-4.6.1.tar.gz --exclude='nco-4.6.1/debian*' --exclude='.cvsignore' --exclude='ncap_lex.c' --exclude='ncap_yacc.[ch]' ./nco-4.6.1 
${sudo_sng} /bin/cp ${DATA}/nco-4.6.1.tar.gz ${rpm_root}/SOURCES
cd ${rpm_root}/SPECS
${sudo_sng} rpmbuild -ba --sign nco-4.6.1.spec > ~/foo.nco 2>&1
scp \
${rpm_root}/RPMS/i386/nco-4.6.1-?.i386.rpm \
${rpm_root}/RPMS/i386/nco-debuginfo-4.6.1-?.i386.rpm \
${rpm_root}/RPMS/i386/nco-devel-4.6.1-?.i386.rpm \
${rpm_root}/SRPMS/nco-4.6.1-?.src.rpm \
dust.ess.uci.edu:/var/www/html/nco/src
scp \
${rpm_root}/RPMS/i386/nco-4.6.1-?.i386.rpm \
${rpm_root}/RPMS/i386/nco-debuginfo-4.6.1-?.i386.rpm \
${rpm_root}/RPMS/i386/nco-devel-4.6.1-?.i386.rpm \
${rpm_root}/SRPMS/nco-4.6.1-?.src.rpm \
zender,nco@web.sf.net:/home/project-web/nco/htdocs/src

# RPM builds as user
# http://myy.helia.fi/~karte/linux/doc/rpm-build-as-user.html
# sudo yum install netcdf netcdf-devel udunits udunits-devel gsl gsl-devel libdap libdap-devel libnc-dap libnc-dap-devel
export rpm_root="${DATA}/rpm/nco"
#cd ~/nco;cvc;cvu # This risks committing unwanted *.[ch]pp files
mkdir -p ${DATA}/rpm/nco/TMP ${DATA}/rpm/nco/BUILD
/bin/rm -rf ${DATA}/nco-4.6.1 ${DATA}/nco-4.6.1* # Cleanup last build
/bin/rm -r -f \
${rpm_root}/nco-4.6.1-?.src.rpm \
${rpm_root}/nco-4.6.1.spec \
${rpm_root}/nco-4.6.1.tar.gz \
${rpm_root}/*/nco-4.6.1-?.*.rpm \
${rpm_root}/*/nco-debuginfo-4.6.1-?.*.rpm \
${rpm_root}/*/nco-devel-4.6.1-?.*.rpm
# cd ${DATA};cvs -d zender@nco.cvs.sf.net:/cvsroot/nco export -kkv -r nco-4.6.1-1 -d nco-4.6.1 nco # Export based on tag
cd ${DATA};cvs -d zender@nco.cvs.sf.net:/cvsroot/nco export -kkv -D "1 second ago" -dnco-4.6.1 nco # Export most recent and build as 4.6.1-1
tar cvzf ./nco-4.6.1.tar.gz --exclude='nco-4.6.1/debian*' --exclude='.cvsignore' --exclude='ncap_lex.c' --exclude='ncap_yacc.[ch]' ./nco-4.6.1 
/bin/cp ${DATA}/nco-4.6.1.tar.gz ${rpm_root}
ln -s ${HOME}/nco/bld/nco.spec ${rpm_root}/nco.spec
cd ${rpm_root}
rpmbuild -ba --sign nco.spec > ~/foo.nco 2>&1
rpmlint ${rpm_root}/*/nco-4.6.1-?.*.rpm
sudo yum remove nco
sudo yum install ${rpm_root}/*/nco-4.6.1-?.*.rpm
scp \
${rpm_root}/*/nco-4.6.1-?.*.rpm \
${rpm_root}/*/nco-debuginfo-4.6.1-?.*.rpm \
${rpm_root}/*/nco-devel-4.6.1-?.*.rpm \
${rpm_root}/nco-4.6.1-?.*.src.rpm \
dust.ess.uci.edu:/var/www/html/nco/src
scp \
${rpm_root}/*/nco-4.6.1-?.*.rpm \
${rpm_root}/*/nco-debuginfo-4.6.1-?.*.rpm \
${rpm_root}/*/nco-devel-4.6.1-?.*.rpm \
${rpm_root}/nco-4.6.1-?.*.src.rpm \
zender,nco@web.sf.net:/home/project-web/nco/htdocs/src
