#!/usr/bin/env python
#
# Search packages over zypper and install all uninstalled to the system.
#
# Usage
# =====
#
# # zypper se -u <package> | zypper-bin
#

import os
import re
import sys


PACKAGE_RE = re.compile(r'^  \| (\S+)', re.M)


def main():
    script_name = os.path.basename(sys.argv[0])

    # Read table data from input
    table = sys.stdin.read()

    # If input is empty - print usage message
    if not table:
        print('Usage: zypper se -u <package> | %s') % script_name
        return

    # Find all installed packages
    packages = PACKAGE_RE.findall(table)

    if not packages:
        print('No uninstalled packages found.')
        return

    # Build command and execute it
    cmd = ['zypper', 'rm']
    cmd.extend(packages)

    cmd = ' '.join(cmd)
    print(cmd)

    os.system(cmd)


if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        print('OK OK! Exiting immediatly...')
