Bluetooth keyboard does not connect automatically to laptop with Mac OS Sierra

Not sure if it started after upgrade to the latest version of MacOS Sierra 10.12, but I feel like so.
My laptop loses connection to wireless keyboard very often and does not reconnect automatically. Turning on and off does not help.
I tried to find an explanation and possible solution, and the only hint I found was to re-connect the keyboard.
This is how you do it:

  1. Call Keyboard preferences window by typing in [CMD] +[Space] -> keyboard

  2. Click the [Setup Bluetooth Keyboard] in the bottom corner of the Dialog

  3. Observe your joined but disconnected keyboard in the list

  4. Press the (x) on the right side of the list item with you keyboard name and remove it from the list
  5. Turn Off your bluetooth keyboard by long holding the power button
  6. Turn On the keyboard and keep holding the power button – it should stop started blinking 2 short blinks – pause – 2 short blinks – pause …
  7. Observe the keyboard appeared in the list and there is a [Pair] button on the right

  8. Press the [Pair] button while still holding the power button and wait until the keyboard gets connected – you will see a Dialog with a secret code, exactly as you see when connection a new bluetooth device.

  9. At the moment, your bluetooth keyboard is visible and you must type in the secret code using this remote keyboard!

That it it, you keyboard is not connected and visible again.

Alas… from time to time, when the laptop is disconnected for quite long time, for example, after the weekend, I still have this issue that keyboard is lost/disconnected, so I have to repeat aforementioned steps again and again. Up to now, I still don’t understand what is the issue and how to solve it permanently.

Functional approach in PHP

The code snippet below will only work in PHP-5.3+!.
Isn’t it the famous functional programming with functions as first class citizens, closures and other functional bla-bla?

function one($a = 0) {
    return function(callable $b) use ($a) {
        return call_user_func($b, $a);
print_r(call_user_func(one(1), function ($x) { return $x + 4; } ));

This was created just for fun. As you may see, in place of callable you can put any valid PHP callable, a form ['Class', 'method']

Use your imagination for other real things, but also try to stay not cursed later by those who will inherit your untraversable codebase :LOL:

[SOLVED] Upgrading from PHP 5.5 to PHP 5.6 on Ubuntu 12.04 LTS

Recently I had to upgrade PHP version on the build server based on Ubuntu 12.04 LTS to PHP 5.6 At that moment there was PHP 5.5 installed on the server. Everything should have been as easy and promising as described, for example, in this post. Yes, it was going well to the moment

sudo apt-get dist-upgrade

Then dpkg claimed unmet dependencies… You know, as usual. I decided – f…k it, I will remove existing php and everything related. Simple removal with

sudo apt-get remove php*

did not work fine and stuck on removing phpmyadmin.

That guysexited with an error code 10 being reported from its post-remove script. After pretty short googling, I found the solution which worked for me.

The similar shit happened with php5-imagick package, so the solution I used was similar to phpmyadmin removal. Afterwards, I checked for presence anything starting with or inlcuding php:

sudo dpkg -l | grep php

Everything was clean. OK, here we go…

sudo apt-get clean && sudo apt-get autoclean
sudo apt-get udpate && sudo apt-get upgrade && sudo apt-get dist-upgrade

…and still the same error in result:

Reading package lists… Done Building dependency tree Reading state information… Done Some packages could not be installed. This may mean that you have requested an impossible situation or if you are using the unstable distribution that some required packages have not yet been created or been moved out of Incoming. The following information may help to resolve the situation: The following packages have unmet dependencies: php5 : Depends: libapache2-mod-php5 (>= but it is not going to be installed or libapache2-mod-php5filter (>= but it is not going to be installed or php5-cgi (>= but it is not going to be installed or php5-fpm (>= but it is not going to be installed E: Unable to correct problems, you have held broken packages.

I tried install with -f flag, package-by-package. Nothing worked. The final unmet dependency was:

sudo apt-get -f install php5-json
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
 php5-json : Depends: phpapi-20121212
E: Unable to correct problems, you have held broken packages.

And here I stuck, as googling at this point didn’t help me at all… I visited the home of php5.6 PPA hoping to find something there. And a brilliant idea came to my mind – I should check APT’s sources list! I saw this files inside /etc/apt/sources.list.d:

/etc/apt/sources.list.d$ ll
total 32
drwxr-xr-x 2 root root 4096 Jan  5 11:12 ./
drwxr-xr-x 6 root root 4096 Jan  5 11:11 ../
-rw-r--r-- 1 root root  138 Jan  5 10:32 chris-lea-node_js-precise.list
-rw-r--r-- 1 root root  138 Jan  5 10:32
-rw-r--r-- 1 root root   60 Jan  5 10:32 nginx-stable-lucid.list
-rw-r--r-- 1 root root   60 Jan  5 10:32
-rw-r--r-- 1 root root  134 Jan  5 10:32 ondrej-php5-5_6-precise.list
-rw-r--r-- 1 root root  134 Jan  5 10:32
-rw-r--r-- 1 root root  126 Jan  5 10:32 ondrej-php5-precise.list
-rw-r--r-- 1 root root  126 Jan  5 10:32

I realised, the problem could be that packages were overwritten from older php-5.5, which apparently was installed manually to the server some time earlier. Once I removed these two files:


and updated apt one more time, I was able to install PHP 5.6 as usual:

sudo apt-get install php5
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
  shtool libc-client2007e node-node-uuid libicu52 libicu48 libv8- libmcrypt4 libt1-5 mlock libssl-dev libvpx1 libssl-doc zlib1g-dev libgd3 libev4 libv8-dev libc-ares2 libev-dev
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
  libapache2-mod-php5 php5-cli php5-json php5-readline
Suggested packages:
The following NEW packages will be installed:
  libapache2-mod-php5 php5 php5-cli php5-json php5-readline
0 upgraded, 5 newly installed, 0 to remove and 0 not upgraded.
Need to get 5,575 kB of archives.
After this operation, 18.9 MB of additional disk space will be used.
Do you want to continue [Y/n]? Y

The main idea of this story, before adding new custom repository for PHP, make sure you removed any other old ones! Good luck and happy coding!

Firefox Java Plugin – Debian Wheezy

By default Firefox has no Java plugin because of security issues.
One can install plugin by following next steps:
0. Exit Firefox browser if it is running
1. Make directory if it does not exist -> /usr/lib/mozilla/plugins
2. Make a symbolic link for file which resides in JRE directory, e.g.:

sudo ln -s /usr/lib/jvm/jdk1.8.0_20/jre/lib/amd64/ /usr/lib/mozilla/plugins/

Please note, that amd64 is an architecture of the OS you have installed, possibly it could be i386 in your case.
3. Start Firefox and type about:plugins in address box to check if browser able to see Java plugin.

Install proprietary Oracle JDK in Debian 7.0 Wheezy

This is a YAP (yet another post) about how one can manually install Oracle’s proprietary JDK/JRE version (in Debian 7.0 Wheezy as an example).

First of all, download fresh version of JDK/JRE from the Oracle website.
Copy archive to desired location, in the following example we will use


as an installation source directory.
Unpack the archive and run next commands:

sudo update-alternatives --install /usr/bin/java java /usr/lib/jvm/jdk/bin/java 1071
sudo update-alternatives --install /usr/bin/javac javac /media/mydisk/jdk/javac 1071
sudo update-alternatives --install /usr/bin/jcontrol jcontrol /media/mydisk/jdk/bin/jcontrol 1071

You may need read man pages for update-alternatives to check out the parameters and what they mean.

Now, if you want freshly installed version of java/javac to be default in your system, run next commands:

sudo update-alternatives --config java
sudo update-alternatives --config javac
sudo update-alternatives --config jcontrol

Follow the instructions issued by update-alternatives to select default version among the list of available installations.
After that, check if everything worked fine by issuing commands

java -version
javac -version

You should see 1.8.0 for both.

Having error while trying use jqFileUpload with RequireJS?

Once upon a time, I needed to use jqFileUPload plugin (actually modified version which provides Angular’s directive around native jqFileUpload plugin).
In the same time, my Angular-base project was configured to be used together with RequireJS.
I spend few ours trying to figure out what I was doing wrong – why I was getting errors in my browser’s console.
Those errors were about something weird… Something in `load-image-exif` and `load-image` files.

My require.js main config file looked like this:

  paths: {
    'semantic': '../../bower_components/semantic-ui/build/packaged/javascript/semantic.min',
    'jquery': '../../bower_components/jquery/dist/jquery.min',
    'domReady': '../../bower_components/requirejs-domready/domReady',
    'angular': '../../bower_components/angular/angular.min',
    'angular-route': '../../bower_components/angular-route/angular-route.min',
    'angular-resource': '../../bower_components/angular-resource/angular-resource.min',
    'jquery.fileupload': '../../bower_components/angular-jqfile-upload/js/jquery.fileupload',
    'jquery.fileupload-angular': '../../bower_components/angular-jqfile-upload/js/jquery.fileupload-angular',
    'jquery.ui.widget': '../../bower_components/angular-jqfile-upload/js/vendor/jquery.ui.widget',
    'jquery.fileupload-image': '../../bower_components/angular-jqfile-upload/js/jquery.fileupload-image',
    'jquery.fileupload-process': '../../bower_components/angular-jqfile-upload/js/jquery.fileupload-process',
    'jquery.fileupload-audio': '../../bower_components/angular-jqfile-upload/js/jquery.fileupload-audio',
    'jquery.fileupload-video': '../../bower_components/angular-jqfile-upload/js/jquery.fileupload-video',
    'jquery.fileupload-validate': '../../bower_components/angular-jqfile-upload/js/jquery.fileupload-validate',
    'jquery.fileupload-ui': '../../bower_components/angular-jqfile-upload/js/jquery.fileupload-ui',
    'jquery.iframe-transport': '../../bower_components/angular-jqfile-upload/js/jquery.iframe-transport',
    'canvas-to-blob': '../../bower_components/blueimp-canvas-to-blob/js/canvas-to-blob.min',
    'load-image': '../../bower_components/blueimp-load-image/js/load-image.min',
    'load-image-exif': '../../bower_components/blueimp-load-image/js/load-image-exif',
    'load-image-ios': '../../bower_components/blueimp-load-image/js/load-image-ios',
    'load-image-meta': '../../bower_components/blueimp-load-image/js/load-image-meta',
    'load-image-orientation': '../../bower_components/blueimp-load-image/js/load-image-orientation'
  // angular does not support AMD out of the box, put it in shim
  shim: {
	  'semantic': {
		  exports: 'semantic',
		  deps: ['jquery']
    'angular': {
      exports: 'angular'
  // kick start application
  deps: ['./bootstrap']

Those errors made me crazy so I even decided to get rid of using RequireJS with Angular app at all!
Next day, I decided Google for a bit and I was extremly lucky to get to this answer –
No votes for it, but that is correct answer and helps resolve the issue.

To recall here, the reason is – I used minified version of `load-image` script, which includes all dependencies in it…
So, all I had to do is replace this line with a path to not minified version:

    'load-image': '../../bower_components/blueimp-load-image/js/load-image<span style="text-decoration: line-through;">.min</span>',

After had that done everything started up.

How to add role to the PostgreSQL

If you need have superuser access to PostgreSQL via command line psql utility, create needed username first:

sudo -u postgres createuser NEEDED_USERNAME

You will be asked whether role should be a superuser:

Shall the new role be a superuser? (y/n)

answer Y (yes) if so and hit Enter.
Now you have NEEDED_USERNAME allowed to access databases as a superuser via psql.

Postgres and earthdistance extension

First of all, let me describe how one can install extensions for your Postgresql.
It depends whether you have version 8.4+ or 9.1+ installed.

In following sections I will describe procedure related to Debian based OSes.

First of all, you have to install contrib packages for corresponding version:

root@debian:/home/web# apt-cache search postgres | grep contrib
postgresql-contrib - additional facilities for PostgreSQL (supported version)
postgresql-contrib-8.4 - additional facilities for PostgreSQL
postgresql-contrib-9.1 - additional facilities for PostgreSQL

with command:

apt-get install postgresql-contrib postgresql-contrib-x.y

After that, restart postgresql daemon:

/etc/init.d/postgresql restart

Change to the database owner account, e.g.:

su postgres

Change to the contrib modules’ directory:

cd /usr/share/postgresql/8.4/contrib/

Now, there are 2 different way how you could install extensions for your database.

1. Postgres-8.4.x
Load the SQL files for needed extension using command:

psql -U user_name -d database_name -f module_name.sql

for example,

psql -U postgres -d your_database_name -f cube.sql
psql -U postgres -d your_database_name -f earthdistance.sql

2. Postgres-9.1.x
Extensions are stored under /usr/share/postgresql/9.1/extension directory,
so you need to cd to it:

cd /usr/share/postgresql/9.1/extension

and then launch psql.
In opened database console run this query:

CREATE EXTENSION "cube"; -- you will not be able install "earthdistance" w/o "cube" extension
CREATE EXTENSION "earthdistance"; --or any other extension you need

That is all – you can use installed extension.

Okay, now lets proceed to the main topic of the post.
What is earthdistance?
It is a simplified solution to make calculations over spatial coordinates (latitude, longitude), calculate distance between two points in the Earth.
In case if you do not want install and learn PostGIS extension for the Postgres DB, although that extension provides much more functionality, you can go with earthdistance.
In earthdistance the Earth is treated as an ideal sphere and if it works for you – you don’t have too precise calculations -, then you can earthdistance.
Otherwise you definitely should take a look at PostGIS.

So, here is the main point – what the SQL should look like when you want to search location of some subject based on its coordinates?
Just use such expression in WHERE part of you SQL (for example):

... WHERE earth_box(ll_to_earth({longitude_float_value}, {latitude_float_value}), {radius_in_meters}) @> ll_to_earth(some_table.latitude, some_table.longitude)

of course values in curly braces should be just numbers, no need to put { and } outside.

Install PHP 5.4 from DotDeb

In case if you want have latest php-5.4 both in CLI and with Apache-2.x it is better to install it through apt-get
You need to add these source to sources.list file:

deb squeeze all
deb-src squeeze all
deb squeeze-php54 all
deb-src squeeze-php54 all

If you are getting this error

W: GPG error: stable Release: The following signatures couldn’t be verified because the public key is not available: NO_PUBKEY E9C74FEEA2098A6E
W: GPG error: squeeze Release: The following signatures couldn’t be verified because the public key is not available: NO_PUBKEY ABF5BD827BD9BF62

you need follow such steps:

gpg --keyserver --recv-key 89DF5277
gpg -a --export 89DF5277 | sudo apt-key add -
gpg --keyserver --recv-key ABF5BD827BD9BF62
gpg -a --export ABF5BD827BD9BF62 | sudo apt-key add -
sudo apt-get update