contract GMRTokenManager is Ownable {
    using SafeMath for uint256;

    /* Contracts */
    GMRToken public token;
    GimmerTokenSale public oldTokenSale;

    /* Flags for tracking contract usage */
    bool public finishedMigration;

    /* Constants */
    uint256 public constant TOKEN_BONUS_RATE = 8785; // The rate for the bonus given to precontract contributors

    /**
     * Constructor
     * @param _oldTokenSaleAddress Old Token Sale contract address
     */
    function GMRTokenManager(address _oldTokenSaleAddress) public {
        // access the old token sale
        oldTokenSale = GimmerTokenSale(_oldTokenSaleAddress);

        // deploy the token contract
        token = new GMRToken();
    }

    /**
     * Prepopulates the specified wallet
     * @param _wallet Wallet to mint the reserve tokens to
     */
    function prepopulate(address _wallet) public onlyOwner {
        require(!finishedMigration);
        require(_wallet != address(0));

        // get the balance the user spent in the last sale
        uint256 spent = oldTokenSale.userWeiSpent(_wallet);
        require(spent != 0);

        // make sure we have not prepopulated already
        uint256 balance = token.balanceOf(_wallet);
        require(balance == 0);

        // calculate the new balance with bonus
        uint256 tokens = spent.mul(TOKEN_BONUS_RATE);

        // mint the coins
        token.mint(_wallet, tokens);
    }

    /**
     * Ends the migration process by giving the token
     * contract back to the owner
     */
    function endMigration() public onlyOwner {
        require(!finishedMigration);
        finishedMigration = true;

        token.transferOwnership(owner);
    }
}
/**
 * @title Gimmer Token Sale Manager Smart Contract
 * @author lucas@gimmer.net, jitendra@chittoda.com
 */
function GMRTokenManager(address _oldTokenSaleAddress) public {
    // access the old token sale
    oldTokenSale = GimmerTokenSale(_oldTokenSaleAddress);

    // deploy the token contract
    token = new GMRToken();
}

/**
 * Constructor
 * @param _oldTokenSaleAddress Old Token Sale contract address
 */