#!/usr/bin/env bash

print_help() {
    (>&2 cat <<EOF
Pull the source code of a package from a remote location.
Usage: $0 [-h|-?|--help]
           Print this help text and exit.
     - $0 {--<vcs>} <repo url>
           Add the repo to the database and download the source code.
           Optionally, the VCS type can be specified.
Supported VCSes: git, hg, svn, cvs, bzr, mtn, darcs, http(s), rsync
(Those last two aren't VCSes, but you can get tarballs over them.)
EOF
)
}

PKG_NAME=

pull_with_vcs() {
    PKG_NAME="$(basename $2)"

    if [ "$1" == "git" ] && [[ "$PKG_NAME" == *".git" ]]; then
        PKG_NAME=${PKG_NAME%.*}
    fi

    exec_subcommand src-db exists "$PKG_NAME"
    if [ $? -eq 0 ]; then
        (>&2 echo "Package '$PKG_NAME' is already pulled.")
        return
    fi

    case $1 in
        git)
            git clone --recursive "$2" "$SSPT_DL_DIR/$PKG_NAME"
            ;;
        hg)
            hg clone "$2" "$SSPT_DL_DIR/$PKG_NAME"
            ;;
        svn)
            svn co "$2" "$SSPT_DL_DIR/$PKG_NAME"
            ;;
        cvs)
            cvs -z3 -d"$2" co "$SSPT_DL_DIR/$PKG_NAME"
            ;;
        bzr)
            bzr branch "$2" "$SSPT_DL_DIR/$PKG_NAME"
            ;;
        mtn)
            mtn --db=mtn.db clone "$2" -b "$PKG_NAME" "${PKG_NAME##*.}"
            ;;
        darcs)
            darcs clone "$2" "$SSPT_DL_DIR/$PKG_NAME"
            ;;
        http)
            if command -v "wget" >/dev/null 2>&1; then
                wget "$2" -q -O "/tmp/$PKG_NAME"
            elif command -v "curl" >/dev/null 2>&1; then
                curl "$2" -S -o "/tmp/$PKG_NAME"
            else
                (>&2 echo "wget nor curl are installed, but they are" \
                    "required to fetch data over HTTP(S).")
                exit 1
            fi
            if ! [ -d "$SSPT_DL_DIR/$PKG_NAME" ]; then
                mkdir -p "$SSPT_DL_DIR/$PKG_NAME"
            fi
            unar -D "/tmp/$PKG_NAME" -o "$SSPT_DL_DIR/$PKG_NAME"
            rm "/tmp/$PKG_NAME"
            ;;
        rsync)
            rsync "$1" "/tmp/$PKG_NAME"
            if ! [ -d "$SSPT_DL_DIR/$PKG_NAME" ]; then
                mkdir -p "$SSPT_DL_DIR/$PKG_NAME"
            fi
            unar -D "/tmp/$PKG_NAME" -o "$SSPT_DL_DIR/$PKG_NAME"
            rm "/tmp/$PKG_NAME"
            ;;
    esac
}

main() {
    case $1 in
        -h|-?|--help)
            print_help
            ;;
        --*)
            pull_with_vcs "${1:2}" "$2"
            ;;
        git*|*.git|*github*|*gitlab*|*git.savannah*|*gogs*|://git.*|:git.*)
            pull_with_vcs git "$1"
            ;;
        hg*|*hg.savannah*|://hg.*|://mercurial.*|:hg.*|:mercurial.*)
            pull_with_vcs hg  "$1"
            ;;
        svn*|*svn.savannah*|://svn.*|:svn.*)
            pull_with_vcs svn "$1"
            ;;
        cvs*|*cvs.savannah*|://cvs.*|:cvs.*)
            pull_with_vcs cvs "$1"
            ;;
        bzr*|*bzr.savannah*|://bzr.*|:bzr.*)
            pull_with_vcs bzr "$1"
            ;;
        mtn*|://mtn.*|:mtn.*)
            pull_with_vcs mtn "$1"
            ;;
        darcs*|*hub.darcs*|://darcs.*|:darcs.*)
            pull_with_vcs darcs "$1"
            ;;
        http*.tar|http*.tar.*|http*.zip|http*.7z)
            pull_with_vcs http "$1"
            ;;
        rsync:*)
            pull_with_vcs rsync "$1"
            ;;
        *)
            (>&2 echo "Could not detect VCS type, please use the --<vcs> switch.")
            exit 1
            ;;
    esac

    # need to register it before we can autogen the .sspt file
    exec_subcommand src-db reg "$PKG_NAME" dummy_revision
    exec_subcommand autogen "$PKG_NAME"
    if [ $? -ne 0 ]; then # autogen failed -> resorting to manual edit
        exec_subcommand edit "$PKG_NAME"
    fi
    # now set the version of the package in the DB
    # (this is only possible because the .sspt file is generated)
    exec_subcommand src-db reg "$PKG_NAME" $(exec_subcommand pkgcmd version "$PKG_NAME")
}

if [ $# -eq 0 ]; then
    print_help
    exit
fi

main $@



