#!/bin/zsh

# This file is maintained at http://git.mdcc.cx/ad1810-tachod

# Based upon draai/setup.

# copyright:
COPYRIGHT='Copyright (C) 2007, 2008, 2009, 2010 Joost van Baal
           Copyright (C) 2012 Joost van Baal-Ilić'

# This program is freely distributable per the following license:
LICENSE="
This file is part of ad1810-tachod.

ad1810-tachod 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 on http://www.gnu.org/licenses/gpl.html.  You should have received a
copy of the GNU General Public License along with ad1810-tachod.

There is NO warranty."

PACKAGE=ad1810-tachod
prefix=/usr/local

typeset -a docfiles dist_docfiles scriptfiles \
 dist_scriptfiles libexecfiles dist_libexecfiles \
 dist_files

docfiles=(COPYING ChangeLog NEWS README)
dist_docfiles=($docfiles TODO INSTALL)

#man1files=(man/ad1810-tachod.1 man/dr_cdrip.1 man/dr_permutate.1 man/dr_symlinks.1 \
#  man/dr_unsort.1 man/dr_watch.1)
#dist_man1files=($man1files man/include.zmm.in man/*.azm)

scriptfiles=(ad1810-tachod)
dist_scriptfiles=($scriptfiles ad1810-tachod.in)

dist_files=($dist_docfiles $dist_scriptfiles \
  VERSION setup Makefile.in configure Makefile)

# dist_subdirs=(man script zsh help/{command,option,shortoption})

prog=`basename $0`

usage()
{
cat <<EOT
Usage: $prog [--prefix=PREFIX] [--libexecdir=LIBEXECDIR] install|uninstall
Install ad1810-tachod.

(Don't run as
 $prog dist|publish|configure|changelog
unless you really know what you are doing.)
EOT
}

init()
{
    if test -f VERSION
    then
        echo reusing version $version from file VERSION
        version=$(cat VERSION)
    else
        version=$(date +%Y%m%d)
        echo creating file VERSION
        echo $version >VERSION
    fi

    distdir=$PACKAGE-$version
    tgz=$distdir.tar.gz
}

configure()
{
    for f in ad1810-tachod.in Makefile.in
    do
       sed "s/@VERSION@/$version/; s|@top_srcdir@|`pwd`|; \
         s|@prefix@|$prefix|; s|@libexecdir@|$libexecdir|" $f >${f%.in}
       echo generated ${f%.in} from $f
    done
    # rm help.txt cmds.txt
}

changelog()
{
    # we want no unprotected emailadresses in the cl
    git log --pretty --numstat --summary | sed 's/<[^>][^>]*>//g' | git2cl >ChangeLog
}

OPTS=$(getopt -o h,l,p: --long help,license,prefix:,libexecdir: -- "$@")
eval set -- "$OPTS"

while true; do
    case "$1" in
      -h|--help)
        usage
        exit 0
        ;;
      -p|--prefix)
        prefix="$2"
        shift 2
        ;;
      --libexecdir)
        libexecdir="$2"
        shift 2
        ;;
      -l|--license)
        echo $COPYRIGHT
        echo $LICENSE
        exit 0
        ;;
      --)
        shift
        break
        ;;
      *)
        usage
        exit 1
        ;;
    esac
done

shift $(($OPTIND - 1))

command=$1
shift

exec_prefix=${prefix}

bindir=${exec_prefix}/bin
mandir=${prefix}/man
man1dir=$mandir/man1

case $command in
  install)

    for d in $DESTDIR$bindir $DESTDIR$man1dir
    do
        test -d "$d" || { mkdir -p "$d" && echo created $d }
    done
    for p in $scriptfiles
    do
        if test -f $p
        then
            install -c "$p" "${DESTDIR}${bindir}/$(basename $p)" && \
              echo installed ${DESTDIR}${bindir}/$(basename $p)
        fi
    done
    for p in $man1files
    do
        if test -f $p
        then
            install -c -m 644 "$p" "${DESTDIR}${man1dir}/$(basename $p)" && \
              echo installed ${DESTDIR}${man1dir}/$(basename $p)
        fi
    done

    ;;
  uninstall)
    # FIXME
    echo not yet implemented
    ;;
  dist)

    cp /usr/share/common-licenses/GPL COPYING

    init
    echo $version >VERSION

    configure
    changelog

    if head NEWS | grep -q $version
    then
        :
    else
        echo error: NEWS does not mention release $version
        exit 1
    fi

    rm -rf $distdir
    mkdir $distdir

    for file in $dist_files
    do
        dir=$(echo "$file" | sed -e 's,/[^/]*$,,')
        cp -pR $file $distdir/$dir
    done

    tar chof - $distdir | gzip -c >$distdir.tar.gz

    ls -l $distdir.tar.gz
    ;;
  publish)
    init
    scp $tgz beskar:www/mdcc.cx/pub/ad1810-tachod/
    ;;
  configure)
    # not for users, merely convenience for developers
    init
    configure
    ;;
  changelog)
    changelog
    ;;
  *)
    usage
    exit 1
    ;;
esac