#!/usr/bin/python2
import getpass
import MySQLdb

def has_wrong_path(package):
  id, pkgname, urlpath = package
  basename = '%s.tar.gz' % pkgname
  expected_urlpath = '/packages/%s/%s' % (pkgname, basename)
  if urlpath and urlpath != expected_urlpath:
    return True
  return False

QUERY = 'SELECT ID, Name, URLPath from Packages'
username = raw_input('MySQL username: ')
password = getpass.getpass()
dbconnection = MySQLdb.connect(db='AUR', user=username, passwd=password)
cursor = dbconnection.cursor()
cursor.execute(QUERY)

with open('bad_pkgs.txt', 'w') as bad_pkg_list:
  bad_pkg_list.write('ID Name URLPath\n')
  for package in cursor:
    if has_wrong_path(package):
      bad_pkg_list.write('%d %s %s\n' % package)

cursor.close()
dbconnection.close()
