#!/bin/bash

###
# newpkg -- A script to speed up creating Arch Linux PKGBUILDS
# This program is a part of pkgtools

# Copyright (C) 2008-2011 Daenyth <Daenyth+Arch _AT_ gmail _DOT_ com>
#
# Pkgtools 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.
#
# Pkgtools is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
##

CONTRIB_INFO=
BASEDIR="${HOME}/builds"
CONFIG_DIR='/etc/pkgtools/'
PKGTOOLS_DIR='/usr/share/pkgtools'
PRESET_DIR="$PKGTOOLS_DIR/newpkg/presets"
VERBOSE=0

if [[ -r "$PKGTOOLS_DIR/functions" ]]; then
    . "$PKGTOOLS_DIR/functions"
else
    printf "Unable to source function file!\n" >&2
    exit 1
fi
if [[ -r "$PKGTOOLS_DIR/functions.newpkg" ]]; then
    . "$PKGTOOLS_DIR/functions.newpkg"
else
    printf "Unable to source newpkg function file!\n" >&2
    exit 1
fi

###
# Lines below this point should not need to be edited.
##
MYVERSION=5.0
PROGNAME="$(basename $0)"

PROTODIR='/usr/share/pacman'

declare -a ACTIONS
declare -a MODIFICATIONS
declare -A COPIES
PRESET=default
DOABS=0      # Copy from ABS instead of normal operation TODO
ABSROOT='/var/abs'
EDIT=0

load_config 'newpkg.conf'

# {{{ usage()
function usage {
    msg "%s version %s -- A script to speed up creating new PKGBUILDs\n" "$PROGNAME" "$MYVERSION"
    msg "Usage: %s [options] PKGNAME\n" "$PROGNAME"
    msg "Options:\n"
    msg "    -a --action        - Add a modular action to the creation.\n"
    msg "                         Call multiple times for multiple actions\n"
    msg "    -e --edit          - Call \$EDITOR on the PKGBUILD at the end\n"
    msg "    -h --help          - Print this usage\n"
    msg "    -i --install-file  - Copy the prototype .install file\n"
    msg "    --rc               - Copy the protype for an rc.d script\n"
    msg "    -p --pkgbuild      - Specify the PKGBUILD file. If not absolute, the\n"
    msg "                         filename is relative to %s.\n" "$PROGNAME"
    msg "    -P --preset        - Use the specified modular preset\n"
    msg "    --sf --sourceforge - Set url and source to common Sourceforge urls\n"
    msg "    --split            - Create a split PKGBUILD\n"
    msg "    -v --verbose       - Print more verbose messages\n"
    exit 0
}
# }}}

# make_pkgdir() {{{
make_pkgdir () {
    mkdir -p "$pkgdir" || die 1 "Unable to create directory %s" "$pkgdir"
    vmsg "Creating package dir for %s\n" "$pkgname"

    cd "$pkgdir" || die 1 "Can't cd to %s!\n" "$pkgdir"
}
# }}}

# copy_files() {{{
copy_files() {
    vmsg "Copying files to pkgdir\n"
    for file in "${!COPIES[@]}"; do
        cp "${COPIES[$file]}" "$file"
    done
}
# }}}

# modify_files() {{{
modify_files() {
    vmsg "Running modifications\n"
    for mod in "${MODIFICATIONS[@]}"; do
        [[ $(type -t $mod) == 'function' ]] || continue
        "$mod"
    done
}
# }}}


if [[ -z "$1" ]]; then
    usage
fi

while (( $# != 0 )); do
    case "$1" in
        -a|--action)
            if [[ -z "$2" ]]; then
                die 1 "%s %s option requires a parameter!\n" "$PROGNAME" "$1"
            fi
            add_action "$2"
            shift
            ;;
        -e|--edit)
            EDIT=1
            ;;
        -h|--help)
            usage
            ;;
        -i|--install-file)
            add_action install
            ;;
        --rc)
            add_action rcd
            ;;
        -p|--pkgbuild)
            if [[ -z "$2" ]]; then
                die 1 "%s %s option requires a parameter!\n" "$PROGNAME" "$1"
            fi
            setpkgbuild "$2"; shift
            ;;
        -P|--preset)
            if [[ -z "$2" ]]; then
                die 1 "%s %s option requires a parameter!\n" "$PROGNAME" "$1"
            fi
            PRESET="$2"; shift
            ;;
        --sf|--sourceforge)
            add_action sf
            ;;
        --split)
            setpkgbuild "PKGBUILD-split.proto"
            ;;
        -v|--verbose)
            VERBOSE=1
            ;;
        -*)
            die 1 "Unrecognized option %s\n" "$1"
            ;;
        *)
            pkgname=$(echo $1 | sed 's; ;;g' | sed 's;/$;;')
            ;;
    esac
    shift
done

[[ -z "$pkgname" ]] && die 1 "No package name supplied\n"
pkgdir="${BASEDIR}/${pkgname}"

if [[ -r "$PRESET_DIR/${PRESET}.preset" ]]; then
    . "$PRESET_DIR/${PRESET}.preset"
else
    die 1 "Can't read preset '%s' in '%s'\n" "$PRESET" "$PRESET_DIR"
fi

for act in "${ACTIONS[@]}"; do
    if [[ -r "$PRESET_DIR/${act}.action" ]]; then
        . "$PRESET_DIR/${act}.action"
    else
        die 1 "Can't read action '%s' in '%s'\n" "$act" "$PRESET_DIR"
    fi
done

make_pkgdir
copy_files
modify_files
(( EDIT )) && "${EDITOR:-${VISUAL:-vi}}" PKGBUILD

msg "All done! New package prepared in %s\n" "$pkgdir"
quit 0
# vim: set ts=4 sw=4 fdm=marker et:
