key=`curl 169.254.169.254/2009-04-04/meta-data/public-keys/0/openssh-key 2>/dev/null`

if [ $? -eq 0 ] ; then

 for home in `find /home/* -maxdepth 0 -type d 2>/dev/null | tr '\n' ' '`; do
     user=`echo $home | awk -F '/' '{ print $3 }'`

     if [ ! -d $home/.ssh ] ; then
         mkdir -p $home/.ssh
         chmod 700 $home/.ssh
         chown $user $home/.ssh
     fi

     actual_key=`echo $key | awk '{ print $2 }'`

     if [ -f $home/.ssh/authorized_keys ]; then
         if [ "`grep $actual_key $home/.ssh/authorized_keys | wc -l`" -gt 0 ]; then
             continue
         fi
     fi

     echo $key >> $home/.ssh/authorized_keys
     chmod 600 $home/.ssh/authorized_keys
     chown $user $home/.ssh/authorized_keys
done

fi