#!/usr/bin/python3
# Copyright (c) TurnKey GNU/Linux - http://www.turnkeylinux.org
#
# This file is part of Verseek
#
# Verseek 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 3 of the License, or (at your
# option) any later version.

from os.path import *  # 
import sys
import argparse

import verseek_lib as verseek


def fatal(s):
    print("error: " + str(s), file=sys.stderr)
    sys.exit(1)


def list_(path):
    try:
        versions = verseek.list_(path)
    except verseek.VerseekError as e:
        fatal(e)

    for version in versions:
        print(version)


def seek(path, version):
    try:
        verseek.seek(path, version)
    except verseek.VerseekError as e:
        fatal(e)


def main():
    parser = argparse.ArgumentParser(description='Seek to available versions'
                                                 ' in Debian source package')
    parser.add_argument('-l', '--list', action='store_true', default=False,
                        help='list seekable versions')
    parser.add_argument('srcpath', help='Path to source')
    parser.add_argument('version', nargs='?', default=None,
                        help='If no version specified, undo previous seek'
                             ' (restore state)')
    args = parser.parse_args()
    srcpath = args.srcpath
    if not isdir(args.srcpath):
        fatal("no such directory `{}'".format(args.srcpath))

    if args.list:
        return list_(srcpath)

    seek(args.srcpath, args.version)


if __name__ == "__main__":
    main()
