args = parser.parse_args()

if len(args.residuals) > 0 or args.help:
    io.println(io.Yellow("usage:"), args.package, "status [-h]")
    io.exitFail()

io.printAsync(io.Empty(), "checking package..", io.Progress())
if not pkg.packageIsDir(args.package):
    io.println(io.Fail(), args.package, "does not exist")
    io.exitFail()

if not pkg.packageIsGitRepository(args.package):
    io.println(io.Fail(), args.package, "is not a git repository")
    io.exitFail()

repo = pkg.getPackageRepo(args.package)

package_clear = True

io.printAsync(io.Empty(), "checking untracked files in", args.package, "\b..",
              io.Progress())
untracked_files = repo.untracked_files
if len(untracked_files) > 0:
    package_clear = False
    io.println(io.Warn(), "found", len(untracked_files), "untracked files:")
    for untracked_file in untracked_files:
        io.println(io.WarnList(), untracked_file)
    io.newLine()

io.printAsync(io.Empty(), "checking modified files in", args.package, "\b..",
              io.Progress())
for package in pkg.packages:

    package_clear = True

    io.printAsync(io.Empty(), "checking package..", io.Progress())
    if not pkg.packageIsDir(package):
        io.println(io.Fail(), package, "does not exist")
        all_clear = False
        continue

    if not pkg.packageIsGitRepository(package):
        io.println(io.Fail(), package, "is not a git repository")
        all_clear = False
        continue

    repo = pkg.getPackageRepo(package)

    io.printAsync(io.Empty(), "checking remotes..", io.Progress())
    if 'origin' not in repo.remotes:
        io.println(io.Fail(), package, "does not have origin remote")
        all_clear = False
        continue

    io.printAsync(io.Empty(), "fetching", package, "\b..", io.Progress())
    for fetch_info in repo.remotes['origin'].fetch():
        if fetch_info.flags & git.FetchInfo.NEW_HEAD != 0:
            if package_clear:
                io.println(io.Warn(), package, "pull from origin remote:")
                package_clear = False
            io.println(io.SuccessList(), "update", fetch_info.ref,
                       "to '{}'".format(fetch_info.commit.summary))