#!/usr/bin/env python3

'''
If the automatic email-based password reset is not enabled, system
administrators can still manually reset passwords with the help of
this script.
'''

from sys import stderr
from getpass import getpass
from szurubooru import db
from szurubooru.func import users as userfuncs


def main():
    username = input('Enter username or email: ')

    try:
        user = userfuncs.get_user_by_name_or_email(username)
    except userfuncs.UserNotFoundError as e:
        print(e, file=stderr)
        return

    new_password = getpass('Enter new password for \'%s\': ' % user.name)
    check_password = getpass('Re-enter password: ')

    if check_password != new_password:
        print('Passwords do not match.', file=stderr)
        return

    try:
        userfuncs.update_user_password(user, new_password)
    except userfuncs.InvalidPasswordError as e:
        print(e, file=stderr)
        return

    db.get_session().commit()
    print('Sucessfully changed password for \'%s\'' % user.name)


if __name__ == '__main__':
    main()