<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>hackengineer</title>
	<atom:link href="http://www.hackengineer.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.hackengineer.com</link>
	<description>Getting things to do what you want</description>
	<lastBuildDate>Mon, 28 May 2012 19:58:04 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Download, setup and automate folder sync&#8217;s with a cronjob</title>
		<link>http://www.hackengineer.com/download-setup-and-automate-folder-syncs-with-a-cronjob/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=download-setup-and-automate-folder-syncs-with-a-cronjob</link>
		<comments>http://www.hackengineer.com/download-setup-and-automate-folder-syncs-with-a-cronjob/#comments</comments>
		<pubDate>Mon, 28 May 2012 18:45:23 +0000</pubDate>
		<dc:creator>hackengineer</dc:creator>
				<category><![CDATA[networking]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[cronjob]]></category>
		<category><![CDATA[rsync]]></category>

		<guid isPermaLink="false">http://www.hackengineer.com/?p=449</guid>
		<description><![CDATA[To get two synced is pretty easy once you have the networking figured out. You might want to put the scripts in your /Users/local/bin/ directory but you can put them where ever you like. Add the path to the synced directories in BOTH scripts. Now you are ready to sync. To test run the script [...]]]></description>
				<content:encoded><![CDATA[<a class="downloadlink" href="http://www.hackengineer.com/wp-content/plugins/download-monitor/download.php?id=1" title="VersionBeta 0.1 downloaded 293 times" >Download syncMe Beta 0.1 Here (293)</a>
<p>To get two synced is pretty easy once you have the <a title="DDNS, Port Forwarding and SSH Auto Authentication with RSA keys" href="http://www.hackengineer.com/ddns-port-forwarding-and-ssh-auto-authentication-with-rsa-keys/">networking figured out</a>. You might want to put the scripts in your /Users/local/bin/ directory but you can put them where ever you like. Add the path to the synced directories in BOTH scripts. Now you are ready to sync. To test run the script in the terminal:</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ syncMe.sh<br />
Initializing local sync folder<br />
Initializing remote sync folder none-&amp;gt;1.0<br />
Setting Remote Delete Lock<br />
Checking Remote Sync Lock<br />
Checking for remote Deletions<br />
Deleted files in Sync<br />
Clearing Remote Delete Lock<br />
Receiving<br />
receiving file list ... done<br />
EngineeringProjects/mySync/</div></td></tr></tbody></table></div>
<p>sent 38 bytes received 398811 bytes 159539.60 bytes/sec<br />
total size is 55629350557 speedup is 139474.71<br />
building file list &#8230; done<br />
EngineeringProjects/mySync/DDNS_PortForwarding.png<br />
EngineeringProjects/mySync/DDNS_PortForwarding.vsd</p>
<p>sent 516249 bytes received 2302 bytes 345700.67 bytes/sec<br />
total size is 55629350567 speedup is 107278.46</p>
<p>Thats it! Do the same steps on the remote side for a full bi-directional sync. If you want to delete files</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ syncDel.sh deleteme<br />
Removing deleteme</div></td></tr></tbody></table></div>
<p>When the remote initializes a sync the file deletme will be deleted. Last thing is to automate the sync with a cronjob. On the terminal type contab -e and add</p>
<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br /></div></td><td><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">RUNNINGFROMCRON=1<br />
*/15 * * * * /usr/local/bin/syncMe.sh</div></td></tr></tbody></table></div>
<p>This set a variable so the script knows its being run from a cronjob and will not ask for user input and will sync the folder every 15 minutes.  And there you have it, virtually unlimited sync space for free!</p>
<p><a title="Projects" href="http://www.hackengineer.com/homepage/">Projects Home</a></p>
<p><script type="text/javascript">// < ![CDATA[
google_ad_client = "ca-pub-7101321993029582";
/* mySync */
google_ad_slot = "7593802595";
google_ad_width = 468;
google_ad_height = 60;
// ]]&gt;</script><br />
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">// < ![CDATA[</p>
<p>// ]]&gt;</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackengineer.com/download-setup-and-automate-folder-syncs-with-a-cronjob/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Coding a Simple Dropbox with Bash</title>
		<link>http://www.hackengineer.com/coding-a-simple-dropbox-with-bash/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=coding-a-simple-dropbox-with-bash</link>
		<comments>http://www.hackengineer.com/coding-a-simple-dropbox-with-bash/#comments</comments>
		<pubDate>Mon, 28 May 2012 18:44:46 +0000</pubDate>
		<dc:creator>hackengineer</dc:creator>
				<category><![CDATA[networking]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[awk]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[rsync]]></category>

		<guid isPermaLink="false">http://www.hackengineer.com/?p=430</guid>
		<description><![CDATA[I wrote this bash script very recently and I would say its still in &#8220;beta&#8221;. USE AT YOUR OWN RISK!! It has been working great but I recommend backing up your synced data just in case. With that said lets get into the code! I have two bash scripts, one to sync the data and [...]]]></description>
				<content:encoded><![CDATA[<p>I wrote this bash script very recently and I would say its still in &#8220;beta&#8221;. USE AT YOUR OWN RISK!! It has been working great but I recommend backing up your synced data just in case.</p>
<p>With that said lets get into the code! I have two bash scripts, one to sync the data and one to properly delete files. In order to get two folders synced for the first time is enter the path to the local sync folder, the path to the remote sync folder, and the port. Remember that by default rsync does not have access to the remotes root folder only the user home directory that you log in as. The remote sync directory is relative to this directory.</p>
<div class="codecolorer-container bash geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br />125<br />126<br />127<br />128<br />129<br />130<br />131<br />132<br />133<br />134<br />135<br />136<br />137<br />138<br />139<br />140<br />141<br />142<br />143<br />144<br />145<br />146<br />147<br />148<br />149<br />150<br />151<br />152<br />153<br />154<br />155<br />156<br />157<br />158<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#!/bin/sh</span><br />
<br />
<span style="color: #666666; font-style: italic;">#Copyright (2012 hackengineer.com)</span><br />
<span style="color: #666666; font-style: italic;">#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the &quot;Software&quot;), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:</span><br />
<span style="color: #666666; font-style: italic;">#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.</span><br />
<span style="color: #666666; font-style: italic;">#THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</span><br />
<br />
<span style="color: #007800;">VAR_LOCAL_SYNC_DIR</span>=<span style="color: #ff0000;">&quot;/Users/myUserName/mySyncDir&quot;</span> <span style="color: #666666; font-style: italic;">#ADD root sync directory here without trailing /</span><br />
<span style="color: #007800;">VAR_REMOTE_SYNC_DIR</span>=<span style="color: #ff0000;">&quot;www.remoteHost.com:remoteSyncDir&quot;</span> <span style="color: #666666; font-style: italic;">#ADD remote sync directory relative to ssh directory here without trailing /</span><br />
<span style="color: #666666; font-style: italic;">#VAR_REMOTE_SYNC_DIR=&quot;mac.local:remoteSyncDir&quot; #local network example</span><br />
<span style="color: #007800;">VAR_PORT</span>=<span style="color: #000000;">22</span><br />
<span style="color: #007800;">VAR_RSYNC</span>=<span style="color: #ff0000;">&quot;/usr/bin/rsync&quot;</span> <span style="color: #666666; font-style: italic;">#specify where rsync lives if needed</span><br />
<span style="color: #666666; font-style: italic;">#--------------------------------------------------</span><br />
<br />
<span style="color: #007800;">VAR_VERSION</span>=<span style="color: #000000;">0.1</span><br />
<br />
<br />
<span style="color: #000000; font-weight: bold;">function</span> deleteFile <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;removing <span style="color: #007800;">$VAR_FULL_FILE</span>&quot;</span><br />
&nbsp; &nbsp; <span style="color: #007800;">VAR_DATE</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">date</span><span style="color: #000000; font-weight: bold;">`</span><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$VAR_FULL_FILE</span>,<span style="color: #007800;">$VAR_TIMESTAMP</span>,<span style="color: #007800;">$VAR_SIZE</span>,<span style="color: #007800;">$VAR_DATE</span>&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncLocalDeleteLog<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-r</span> <span style="color: #007800;">$VAR_FULL_FILE</span><span style="color: #000000; font-weight: bold;">`</span><br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">#Will not delete from a cron</span><br />
<span style="color: #000000; font-weight: bold;">function</span> proptDelete <span style="color: #7a0874; font-weight: bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$RUNNINGFROMCRON</span>&quot;</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #000000;">1</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$VAR_FULL_FILE</span> changed, Rem-&gt;Loc Size:<span style="color: #007800;">$VAR_SIZE</span>-&gt;<span style="color: #007800;">$VAR_MY_SIZE</span> ModTime:<span style="color: #007800;">$VAR_TIMESTAMP</span>-&gt;<span style="color: #007800;">$VAR_MY_TIMESTAMP</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Delete?(y/n) &quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">read</span> word1<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$word1</span> = <span style="color: #ff0000;">&quot;y&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; deleteFile<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">fi</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$VAR_FULL_FILE</span> changed!! Size:<span style="color: #007800;">$VAR_SIZE</span>-&gt;<span style="color: #007800;">$VAR_MY_SIZE</span> ModTime:<span style="color: #007800;">$VAR_TIMESTAMP</span>-&gt;<span style="color: #007800;">$VAR_MY_TIMESTAMP</span>&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncErrorLog<br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">fi</span><br />
<span style="color: #7a0874; font-weight: bold;">&#125;</span><br />
<br />
<br />
<span style="color: #666666; font-style: italic;">#Verify local sync dir exists</span><br />
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-e</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;&lt;initializing local sync folder&gt;&quot;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">#make sure root dir exists</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-e</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">fi</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">#create .rsync dir</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">mkdir</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">`</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">#add neccisary files.</span><br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">touch</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDelete<br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">touch</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDeleteTemp<br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">touch</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncExcludeList<br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'clear'</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDeleteLock<br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">touch</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDeleteLockTemp<br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'clear'</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncSyncLock<br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">touch</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncSyncLockTemp<br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">touch</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncVersion<br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">touch</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncVersionTemp<br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">touch</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncErrorLog<br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'Filename,ModifiedTime,Size,DeleteTime'</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncLocalDeleteLog<br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$VAR_VERSION</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncVersion<br />
<span style="color: #000000; font-weight: bold;">fi</span><br />
<br />
<span style="color: #666666; font-style: italic;">#Verify remote sync dir exists suppress error if rsync fails</span><br />
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;none&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncVersionTemp<br />
<span style="color: #007800;">$VAR_RSYNC</span> <span style="color: #660033;">-q</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;ssh -p <span style="color: #007800;">$VAR_PORT</span>&quot;</span> <span style="color: #007800;">$VAR_REMOTE_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncVersion <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncVersionTemp <span style="color: #000000;">2</span><span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">/</span>dev<span style="color: #000000; font-weight: bold;">/</span>null<br />
<span style="color: #666666; font-style: italic;">#echo &quot;1&quot; &gt;&gt;$VAR_LOCAL_SYNC_DIR/.rsync/rsyncVersionTemp </span><br />
<span style="color: #007800;">VAR_REMOTE_VERSION</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncVersionTemp<span style="color: #000000; font-weight: bold;">`</span><br />
<br />
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$VAR_REMOTE_VERSION</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #007800;">$VAR_VERSION</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span> <span style="color: #666666; font-style: italic;"># if versions DONT match</span><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;&lt;/initializing&gt;&lt;initializing remote sync folder <span style="color: #007800;">$VAR_REMOTE_VERSION</span>-&gt;<span style="color: #007800;">$VAR_VERSION</span>&gt;&quot;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># copy all files in .rsync</span><br />
&nbsp; &nbsp; <span style="color: #007800;">$VAR_RSYNC</span> <span style="color: #660033;">-r</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;ssh -p <span style="color: #007800;">$VAR_PORT</span>&quot;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync <span style="color: #007800;">$VAR_REMOTE_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># init delete/exclude list</span><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDeleteLockTemp<br />
&nbsp; &nbsp; <span style="color: #007800;">$VAR_RSYNC</span> <span style="color: #660033;">-r</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;ssh -p <span style="color: #007800;">$VAR_PORT</span>&quot;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDeleteLockTemp <span style="color: #007800;">$VAR_REMOTE_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDelete<br />
&nbsp; &nbsp; <span style="color: #007800;">$VAR_RSYNC</span> <span style="color: #660033;">-r</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;ssh -p <span style="color: #007800;">$VAR_PORT</span>&quot;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDeleteLockTemp <span style="color: #007800;">$VAR_REMOTE_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncExcludeList <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;"># init locks to clear</span><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;clear&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDeleteLockTemp<br />
&nbsp; &nbsp; <span style="color: #007800;">$VAR_RSYNC</span> <span style="color: #660033;">-r</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;ssh -p <span style="color: #007800;">$VAR_PORT</span>&quot;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDeleteLockTemp <span style="color: #007800;">$VAR_REMOTE_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDeleteLock &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #007800;">$VAR_RSYNC</span> <span style="color: #660033;">-r</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;ssh -p <span style="color: #007800;">$VAR_PORT</span>&quot;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDeleteLockTemp <span style="color: #007800;">$VAR_REMOTE_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncSyncLock<br />
<span style="color: #000000; font-weight: bold;">fi</span><br />
<br />
<span style="color: #666666; font-style: italic;">#Send Lock</span><br />
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;&lt;setting Remote Delete Lock&gt;<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span><br />
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'lock'</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDeleteLockTemp<br />
<span style="color: #007800;">$VAR_RSYNC</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;ssh -p <span style="color: #007800;">$VAR_PORT</span>&quot;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDeleteLockTemp <span style="color: #007800;">$VAR_REMOTE_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDeleteLock<br />
<br />
<span style="color: #666666; font-style: italic;">#Check Remote SyncLock Before Sync</span><br />
<span style="color: #7a0874; font-weight: bold;">echo</span> &nbsp;<span style="color: #ff0000;">&quot;&lt;checking Remote Sync Lock&gt;<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span><br />
<span style="color: #007800;">$VAR_RSYNC</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;ssh -p <span style="color: #007800;">$VAR_PORT</span>&quot;</span> <span style="color: #007800;">$VAR_REMOTE_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncSyncLock <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncSyncLockTemp <br />
<span style="color: #007800;">VAR_LOCK</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncSyncLockTemp<span style="color: #000000; font-weight: bold;">`</span><br />
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$VAR_LOCK</span> = <span style="color: #ff0000;">'lock'</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <br />
<span style="color: #000000; font-weight: bold;">do</span> <br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;remote Delete in progress. &nbsp;please wait...&quot;</span><br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">2</span>;<br />
&nbsp; &nbsp; <span style="color: #007800;">$VAR_RSYNC</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;ssh -p <span style="color: #007800;">$VAR_PORT</span>&quot;</span> <span style="color: #007800;">$VAR_REMOTE_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncSyncLock <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncSyncLockTemp <br />
&nbsp; &nbsp; <span style="color: #007800;">VAR_LOCK</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncSyncLockTemp<span style="color: #000000; font-weight: bold;">`</span><br />
<span style="color: #000000; font-weight: bold;">done</span><br />
<br />
<br />
<span style="color: #666666; font-style: italic;">#Get Delete List</span><br />
<span style="color: #7a0874; font-weight: bold;">echo</span> &nbsp;<span style="color: #ff0000;">&quot;&lt;/checking&gt;&lt;checking for remote Deletions&gt;<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span><br />
<span style="color: #007800;">$VAR_RSYNC</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;ssh -p <span style="color: #007800;">$VAR_PORT</span>&quot;</span> <span style="color: #007800;">$VAR_REMOTE_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDelete <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDeleteTemp <br />
<br />
<br />
<span style="color: #666666; font-style: italic;">#Delete Files that match name/size/mod date</span><br />
<span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDeleteTemp<span style="color: #000000; font-weight: bold;">`</span><br />
<span style="color: #000000; font-weight: bold;">do</span><br />
&nbsp; &nbsp; <span style="color: #007800;">VAR_FILE</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$i</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> -F, <span style="color: #ff0000;">'{print $1}'</span><span style="color: #000000; font-weight: bold;">`</span><br />
&nbsp; &nbsp; <span style="color: #007800;">VAR_TIMESTAMP</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$i</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> -F, <span style="color: #ff0000;">'{print $2}'</span><span style="color: #000000; font-weight: bold;">`</span><br />
&nbsp; &nbsp; <span style="color: #007800;">VAR_SIZE</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$i</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> -F, <span style="color: #ff0000;">'{print $3}'</span><span style="color: #000000; font-weight: bold;">`</span><br />
<br />
&nbsp; &nbsp; <span style="color: #007800;">VAR_FULL_FILE</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span>/<span style="color: #007800;">$VAR_FILE</span>&quot;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-e</span> <span style="color: #007800;">$VAR_FULL_FILE</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span> &nbsp;<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">#Get size for directory or file</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-d</span> <span style="color: #007800;">$VAR_FULL_FILE</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007800;">VAR_MY_SIZE</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">du</span> <span style="color: #660033;">-s</span> <span style="color: #007800;">$VAR_FULL_FILE</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">'print $1'</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #000000; font-weight: bold;">`</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007800;">VAR_MY_SIZE</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-laT</span> <span style="color: #007800;">$VAR_FULL_FILE</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">'print $5'</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #000000; font-weight: bold;">`</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">fi</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007800;">VAR_MY_TIMESTAMP</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">stat</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;%m&quot;</span> <span style="color: #660033;">-t</span> <span style="color: #ff0000;">&quot;%s&quot;</span> &nbsp;<span style="color: #007800;">$VAR_FULL_FILE</span><span style="color: #000000; font-weight: bold;">`</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">#Verify Local and romote files are identical.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$VAR_TIMESTAMP</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #007800;">$VAR_MY_TIMESTAMP</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; proptDelete<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">elif</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$VAR_SIZE</span> <span style="color: #000000; font-weight: bold;">!</span>= <span style="color: #007800;">$VAR_MY_SIZE</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; proptDelete<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; deleteFile<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">fi</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$VAR_FULL_FILE</span> &lt; --File does not exist localy&quot;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">fi</span> <br />
<span style="color: #000000; font-weight: bold;">done</span><br />
<br />
<span style="color: #666666; font-style: italic;">#Send Clear Delete List</span><br />
<span style="color: #7a0874; font-weight: bold;">echo</span> &nbsp;<span style="color: #ff0000;">&quot;&lt;Deleted files in Sync&gt;<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span><br />
<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDeleteTemp<span style="color: #000000; font-weight: bold;">`</span><br />
<span style="color: #007800;">$VAR_RSYNC</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;ssh -p <span style="color: #007800;">$VAR_PORT</span>&quot;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDeleteTemp <span style="color: #007800;">$VAR_REMOTE_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDelete <br />
<span style="color: #007800;">$VAR_RSYNC</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;ssh -p <span style="color: #007800;">$VAR_PORT</span>&quot;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDeleteTemp <span style="color: #007800;">$VAR_REMOTE_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncExcludeList <br />
<br />
<span style="color: #666666; font-style: italic;">#Send Lock release</span><br />
<span style="color: #7a0874; font-weight: bold;">echo</span> &nbsp;<span style="color: #ff0000;">&quot;&lt;clearing Remote Delete Lock&gt;<span style="color: #000099; font-weight: bold;">\r</span>&quot;</span><br />
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'clear'</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDeleteLockTemp<br />
<span style="color: #007800;">$VAR_RSYNC</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;ssh -p <span style="color: #007800;">$VAR_PORT</span>&quot;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDeleteLockTemp <span style="color: #007800;">$VAR_REMOTE_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDeleteLock<br />
<br />
<span style="color: #666666; font-style: italic;">#Bi Directional Sync</span><br />
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;&lt;receiving&gt;&quot;</span><br />
<span style="color: #007800;">$VAR_RSYNC</span> <span style="color: #660033;">-auv</span> <span style="color: #660033;">--copy-links</span> <span style="color: #660033;">--keep-dirlinks</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;ssh -p <span style="color: #007800;">$VAR_PORT</span>&quot;</span> <span style="color: #660033;">--exclude</span> <span style="color: #ff0000;">'.rsync'</span> <span style="color: #660033;">--exclude-from</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncExcludeList <span style="color: #007800;">$VAR_REMOTE_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span> <br />
<br />
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\n</span>&lt;sending&gt;&quot;</span><br />
<span style="color: #007800;">$VAR_RSYNC</span> <span style="color: #660033;">-auv</span> <span style="color: #660033;">--copy-links</span> <span style="color: #660033;">--keep-dirlinks</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;ssh -p <span style="color: #007800;">$VAR_PORT</span>&quot;</span> <span style="color: #660033;">--exclude</span> <span style="color: #ff0000;">'.rsync'</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span> <span style="color: #007800;">$VAR_REMOTE_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span><br />
<span style="color: #000000; font-weight: bold;">&lt;/</span>sending<span style="color: #000000; font-weight: bold;">&gt;&lt;/</span>receiving<span style="color: #000000; font-weight: bold;">&gt;&lt;/</span>clearing<span style="color: #000000; font-weight: bold;">&gt;&lt;/</span>checking<span style="color: #000000; font-weight: bold;">&gt;&lt;/</span>setting<span style="color: #000000; font-weight: bold;">&gt;&lt;/</span>initializing<span style="color: #000000; font-weight: bold;">&gt;</span></div></td></tr></tbody></table></div>
<p>The code is well commented but here is a quick rundown. When the script is ran for the first time it will create the specified directorys if they dont exist and initializes a .rsync folder which holds the system files.</p>
<p>Next it sets the remote &#8220;deleteLock file&#8221; to prevent any remote files from being deleted while a sync is taking place. In case files were being deleted before the lock was set, a synclock file is checked prior to checking out the deleted file list.</p>
<p>Once cleared the delete file list is downloaded. The files are checked to make sure the modification date and size are identical before deleting the files. This helps ensure only identical files are deleted in case of the match had been modified after a long period without syncing. After the deleted files have been removed locally, the remote list is cleared.</p>
<p>Finally the remote modified files are downloaded using rsync. The &#8211;copy-links and &#8211;keep-dirlinks options have been enabled as I like to selectively chose which folders to sync while maintaining the original directory structure. My sync folder typically contains soft links (ln -s ) aka shortcuts to folders I would like to have synced. Also the -a options ensures that the permissions, modified date, for example are preserved. The -u option only transfers files that are newer.</p>
<p>Rsync has many options which can be customized to your situation. Be sure to be careful as it can delete files on both sides.</p>
<p>For the delete script all you need to do is again add the local sync directory. It take the files you would like to delete as command line arguments and DOES NOT PROMPT for each deletion, so be sure you have the right files.</p>
<div class="codecolorer-container bash geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#!/bin/sh</span><br />
<br />
<span style="color: #666666; font-style: italic;">#Copyright (2012 hackengineer.com)</span><br />
<span style="color: #666666; font-style: italic;">#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the &quot;Software&quot;), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:</span><br />
<span style="color: #666666; font-style: italic;">#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.</span><br />
<span style="color: #666666; font-style: italic;">#THE SOFTWARE IS PROVIDED &quot;AS IS&quot;, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.</span><br />
<br />
<span style="color: #666666; font-style: italic;">#ADD root sync directory Here</span><br />
<span style="color: #007800;">VAR_LOCAL_SYNC_DIR</span>=<span style="color: #ff0000;">&quot;/Users/userName/sync&quot;</span><br />
<span style="color: #666666; font-style: italic;">#--------------------------------------------------</span><br />
<br />
<br />
<span style="color: #666666; font-style: italic;">#make sure sync directory has been inititalized</span><br />
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">!</span> <span style="color: #660033;">-e</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span> <br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Please run 2 way sync to initialize folder prior to deleting files&quot;</span><br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">exit</span><br />
<span style="color: #000000; font-weight: bold;">fi</span><br />
<br />
<span style="color: #666666; font-style: italic;">#Set Sync Lock</span><br />
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'lock'</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncSyncLock<br />
<br />
<span style="color: #666666; font-style: italic;">#Verify no remote sync is in progress</span><br />
<span style="color: #007800;">VAR_LOCK</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDeleteLock<span style="color: #000000; font-weight: bold;">`</span><br />
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #007800;">$VAR_LOCK</span> = <span style="color: #ff0000;">&quot;lock&quot;</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <br />
<span style="color: #000000; font-weight: bold;">do</span> <br />
&nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;remote sync in progress. &nbsp;please wait...&quot;</span><br />
&nbsp; &nbsp; <span style="color: #c20cb9; font-weight: bold;">sleep</span> <span style="color: #000000;">2</span>;<br />
&nbsp; &nbsp; <span style="color: #007800;">VAR_LOCK</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">cat</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDeleteLock<span style="color: #000000; font-weight: bold;">`</span><br />
<span style="color: #000000; font-weight: bold;">done</span><br />
<br />
<span style="color: #666666; font-style: italic;">#Log relative file path, size and mod date prior to deletion</span><br />
<span style="color: #007800;">VAR_LOCAL_SYNC_DIR_E</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-pe</span> <span style="color: #ff0000;">'s|\/|\\\/|g'</span><span style="color: #000000; font-weight: bold;">`</span><br />
<span style="color: #000000; font-weight: bold;">for</span> i <span style="color: #000000; font-weight: bold;">in</span> $<span style="color: #000000; font-weight: bold;">@</span><br />
<span style="color: #000000; font-weight: bold;">do</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">#Verify file exists</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-e</span> <span style="color: #007800;">$i</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">#Check for directory</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-d</span> <span style="color: #007800;">$i</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007800;">VAR_SIZE</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">du</span> <span style="color: #660033;">-s</span> <span style="color: #007800;">$i</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">'print $1'</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #000000; font-weight: bold;">`</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007800;">VAR_SIZE</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">ls</span> <span style="color: #660033;">-laT</span> <span style="color: #007800;">$i</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">awk</span> <span style="color: #7a0874; font-weight: bold;">&#123;</span><span style="color: #ff0000;">'print $5'</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #000000; font-weight: bold;">`</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">fi</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">#if in root dir</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">pwd</span><span style="color: #000000; font-weight: bold;">`</span> = <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span> <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007800;">VAR_RELATIVE_DIR</span>=<span style="color: #ff0000;">&quot;&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007800;">VAR_RELATIVE_DIR</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #7a0874; font-weight: bold;">pwd</span> <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-pe</span> <span style="color: #ff0000;">&quot;s/.*(<span style="color: #007800;">$VAR_LOCAL_SYNC_DIR_E</span>\/?)(.*)/&quot;</span><span style="color: #ff0000;">'\$2'</span><span style="color: #ff0000;">&quot;/&quot;</span><span style="color: #000000; font-weight: bold;">`</span><span style="color: #ff0000;">&quot;/&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">fi</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #007800;">VAR_TIMESTAMP</span>=<span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">stat</span> <span style="color: #660033;">-f</span> <span style="color: #ff0000;">&quot;%m&quot;</span> <span style="color: #660033;">-t</span> <span style="color: #ff0000;">&quot;%s&quot;</span> &nbsp;<span style="color: #007800;">$i</span><span style="color: #000000; font-weight: bold;">`</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">#Log files for remote deletion</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$VAR_RELATIVE_DIR</span><span style="color: #007800;">$i</span>,<span style="color: #007800;">$VAR_TIMESTAMP</span>,<span style="color: #007800;">$VAR_SIZE</span>&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncDelete<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">#Log blocked sync list until remote has deleted local copies</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$VAR_RELATIVE_DIR</span><span style="color: #007800;">$i</span>&quot;</span> <span style="color: #000000; font-weight: bold;">&gt;&gt;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncExcludeList<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">#Remove file</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;Removing <span style="color: #007800;">$VAR_RELATIVE_DIR</span><span style="color: #007800;">$i</span>&quot;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">`</span><span style="color: #c20cb9; font-weight: bold;">rm</span> <span style="color: #660033;">-r</span> <span style="color: #007800;">$i</span><span style="color: #000000; font-weight: bold;">`</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$i</span> does not exist&quot;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">fi</span><br />
<span style="color: #000000; font-weight: bold;">done</span><br />
<br />
<span style="color: #666666; font-style: italic;">#Clear Sync Lock</span><br />
<span style="color: #7a0874; font-weight: bold;">echo</span> <span style="color: #ff0000;">'clear'</span> <span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #007800;">$VAR_LOCAL_SYNC_DIR</span><span style="color: #000000; font-weight: bold;">/</span>.rsync<span style="color: #000000; font-weight: bold;">/</span>rsyncSyncLock</div></td></tr></tbody></table></div>
<p>It is worth noting that this script does use &#8220;rm -r&#8221;! Be sure of what your deleting when using this script</p>
<p><a href="http://www.hackengineer.com/download-setup-and-automate-folder-syncs-with-a-cronjob/" title="Download, setup and automate folder sync’s with a cronjob">next page &#8212;></a></p>
<p><script type="text/javascript"><!--
google_ad_client = "ca-pub-7101321993029582";
/* mySync */
google_ad_slot = "7593802595";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackengineer.com/coding-a-simple-dropbox-with-bash/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>DDNS, Port Forwarding and SSH Auto Authentication with RSA keys</title>
		<link>http://www.hackengineer.com/ddns-port-forwarding-and-ssh-auto-authentication-with-rsa-keys/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=ddns-port-forwarding-and-ssh-auto-authentication-with-rsa-keys</link>
		<comments>http://www.hackengineer.com/ddns-port-forwarding-and-ssh-auto-authentication-with-rsa-keys/#comments</comments>
		<pubDate>Mon, 28 May 2012 18:43:18 +0000</pubDate>
		<dc:creator>hackengineer</dc:creator>
				<category><![CDATA[networking]]></category>
		<category><![CDATA[ddns]]></category>
		<category><![CDATA[encryption]]></category>
		<category><![CDATA[port forwarding]]></category>
		<category><![CDATA[rsa]]></category>
		<category><![CDATA[rsync]]></category>
		<category><![CDATA[ssh]]></category>

		<guid isPermaLink="false">http://www.hackengineer.com/?p=409</guid>
		<description><![CDATA[Alright, if you want to sync two computers you will need to enable sftp.  On a Mac this is easy.  Open system preferences, sharing and enable Remote Login.  This enables ssh on your mac on port 22.  If you have two machines using the same router each machine will need to have a different port. [...]]]></description>
				<content:encoded><![CDATA[<p>Alright, if you want to sync two computers you will need to enable sftp.  On a Mac this is easy.  Open system preferences, sharing and enable Remote Login.  This enables ssh on your mac on port 22.  If you have two machines using the same router each machine will need to have a different port. On mac you need to open /System/Library/LaunchDaemons/ssh.plist and change serviceName and bounjour string from ssh to the desired port. I use 22 on my desktop and 26 on my laptop. Here is a list of <a href="http://en.wikipedia.org/wiki/List_of_TCP_and_UDP_port_numbers">standard ports</a>.</p>
<p>Now that ssh is enabled on both computers you need to know how to find it on the internet.  The process isnt hard and can be done for free.  The process will start with the host domain name in a nice human readable format (www.remoteHost.com), but this is no good for a computer, it likes numbers.  So using DDNS it can translate the human readable domain name to the actual ip address.  I use <a href="http://www.no-ip.com/">no-ip&#8217;s free service</a> and run its dynamic updater on each of my machines.  This will get us to the remote router, but now the router needs to know what computer to send the request to as there usually are many devices connected to any given router.  Using port forwarding the router can be configured to forward any given port to a given computer.  We will be using sftp (ssh) which is by default on port 22 but this can can be specified.  Therefore the router will need to forward any port 22 requests to the &#8220;remote&#8221; computer illustrated below.  <a href="http://portforward.com/">This is done differently depending on your router.</a></p>
<p><a href="http://www.hackengineer.com/wp-content/uploads/2012/05/DDNS_PortForwarding1.png"><img class="alignnone  wp-image-462" title="DDNS_PortForwarding" src="http://www.hackengineer.com/wp-content/uploads/2012/05/DDNS_PortForwarding1.png" alt="" width="614" height="186" /></a><br />
Now that we know how to find the remote destination we need to do this securely.  By using rsync with the -e option we can specify the use of a secure shell (SSH).  This is great but we want to do it without having to enter the password every time we want to transfer data.  To get around this we will first have to generate a set of authentication keys using ssh-keygen.  Open a terminal and enter the following:</p>
<div class="codecolorer-container vhdl geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br /></div></td><td><div class="vhdl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ cd ~/.ssh/<br />
$ ssh-keygen<br />
Generating public/private rsa key pair.<br />
Enter <span style="color: #000080; font-weight: bold;">file</span> <span style="color: #000080; font-weight: bold;">in</span> which <span style="color: #000080; font-weight: bold;">to</span> save the key <span style="color: #000066;">&#40;</span>/Users/*****/.ssh/id_rsa<span style="color: #000066;">&#41;</span><span style="color: #000066;">:</span><br />
Enter passphrase <span style="color: #000066;">&#40;</span>empty <span style="color: #000080; font-weight: bold;">for</span> no passphrase<span style="color: #000066;">&#41;</span><span style="color: #000066;">:</span><br />
Enter same passphrase again<span style="color: #000066;">:</span><br />
Your identification has been saved <span style="color: #000080; font-weight: bold;">in</span> /Users/*****/.ssh/.<br />
Your public key has been saved <span style="color: #000080; font-weight: bold;">in</span> is_rsa.pub.<br />
The key fingerprint <span style="color: #000080; font-weight: bold;">is</span><span style="color: #000066;">:</span><br />
sd<span style="color: #000066;">:</span><span style="color: #ff0000;">34</span><span style="color: #000066;">:</span><span style="color: #ff0000;">56</span><span style="color: #000066;">:</span>4j<span style="color: #000066;">:</span><span style="color: #ff0000;">56</span><span style="color: #000066;">:</span>hg<span style="color: #000066;">:</span><span style="color: #ff0000;">45</span><span style="color: #000066;">:</span>fg<span style="color: #000066;">:</span><span style="color: #ff0000;">45</span><span style="color: #000066;">:</span>4g<span style="color: #000066;">:</span>5h<span style="color: #000066;">:</span><span style="color: #ff0000;">42</span><span style="color: #000066;">:</span>df<span style="color: #000066;">:</span>9c<span style="color: #000066;">:</span>f3<span style="color: #000066;">:</span>dc<br />
The key<span style="color: #ff0000;">'s</span> randomart image <span style="color: #000080; font-weight: bold;">is</span><span style="color: #000066;">:</span><br />
+<span style="color: #008000; font-style: italic;">--[ RSA 2048]----+</span><br />
|        . o + ..o|<br />
|     . o o . o o.|<br />
|       o + o . o |<br />
|       . o . o   |<br />
|      . . S . .  |<br />
|       o . +.    |<br />
|       <span style="color: #ff0000;">0</span> . o+    |<br />
|       . .<span style="color: #000066;">=</span>      |<br />
|        E        |<br />
+<span style="color: #008000; font-style: italic;">-----------------+</span></div></td></tr></tbody></table></div>
<p>Leave the passphrase blank. This will generate a keypair (private and public). Leave the private key in the .ssh folder and copy the public key contents into the authorized_users file in the remote .ssh folder. For example enter the following in the terminal:</p>
<div class="codecolorer-container vhdl geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="vhdl codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">$ rsync ~/.ssh/id_rsa.pub user@www.remoteHost.com<span style="color: #000066;">:</span>.ssh/id_rsa.pub<br />
$ ssh user@www.remoteHost.com<br />
password<span style="color: #000066;">:</span>********<br />
$ cat ~/.ssh/id_rsa.pub &amp;gt<span style="color: #000066;">;</span>&amp;gt<span style="color: #000066;">;</span> ~/.ssh/authorized_keys<br />
$ <span style="color: #000080; font-weight: bold;">exit</span></div></td></tr></tbody></table></div>
<p>You also may need to adjust the permissions to 600 if you have problems. Now you should be able to ssh into the remote computer without entering a password. Authentication is done by matching the private key with the public key automatically. Do not share the private key!</p>
<p><a href="http://www.hackengineer.com/coding-a-simple-dropbox-with-bash/" title="Coding a Simple Dropbox with Bash">next page&#8212;></a></p>
<p><script type="text/javascript"><!--
google_ad_client = "ca-pub-7101321993029582";
/* mySync */
google_ad_slot = "7593802595";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackengineer.com/ddns-port-forwarding-and-ssh-auto-authentication-with-rsa-keys/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Roll Your own 2TB Dropbox on mac/unix</title>
		<link>http://www.hackengineer.com/roll-your-own-2tb-dropbox/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=roll-your-own-2tb-dropbox</link>
		<comments>http://www.hackengineer.com/roll-your-own-2tb-dropbox/#comments</comments>
		<pubDate>Mon, 28 May 2012 02:18:31 +0000</pubDate>
		<dc:creator>hackengineer</dc:creator>
				<category><![CDATA[networking]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[cronjob]]></category>
		<category><![CDATA[dropbox]]></category>
		<category><![CDATA[rsync]]></category>

		<guid isPermaLink="false">http://www.hackengineer.com/?p=360</guid>
		<description><![CDATA[I have a lot of friends who are against having all of there data off in some cloud with who knows snooping  around.  For me this isnt an issue as I dont usually put sensitive material online and if I do its encrypted with disk utility.  It wasn&#8217;t until I had to get a laptop to be able to have all my [...]]]></description>
				<content:encoded><![CDATA[<p>I have a lot of friends who are against having all of there data off in some cloud with who knows snooping  around.  For me this isnt an issue as I dont usually put sensitive material online and if I do its encrypted with disk utility.  It wasn&#8217;t until I had to get a laptop to be able to have all my work with me while going back to school full time that I realized I needed much more synced space than the 7 free GB dropbox provided (2GB default + accrued referral bonus).  To upgrade to a 100GB dropbox would cost $20 a month, no thanks.  Since I pretty much always leave my desktop as a SVN server, I decided it was going to become my personal 2TB dropbox.</p>
<p>There are a bunch of programs out there that have plenty of bells and whistles to help with this.  All I needed was something to sync specified folders from my desktop to laptop. It had to be a bi-directional sync meaning if I added or removed a file on one side the same would happen on the other.  I didn&#8217;t need backups (that was time-machine is for) or versioning (all code is kept in svn repos).  Goodsync seemed to be good but I didnt feel like paying $30 + $10 per license.  I saw freeware with poor reviews or not really bi-directional (no remote deletion).  I had been looking for a good excuse to work on my bash scripting abilities and decided this would make for a good project.</p>
<p>All macs come with a handy program called <a href="http://ss64.com/bash/rsync.html">rsync</a>.  It is really easy to use and solved half of the problem up front, one way sync.  If I only needed to mirror a folder I would have been done, but that is not very dropbox like.  I want to add and delete to both folders at any given time.  Rsync&#8217;s conundrum is that it cannot tell if a local missing file is because it was deleted locally or added remotely.  Linux pinguin gives a good <a href="http://www.linuxpinguin.de/2010/10/bidirectional-rsync-flaws/">run though</a> of this issue.</p>
<div class="codecolorer-container bash geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">#local-&gt;remote</span><br />
rsync <span style="color: #660033;">-auv</span> <span style="color: #660033;">-e</span> <span style="color: #c20cb9; font-weight: bold;">ssh</span> mySyncFolder<span style="color: #000000; font-weight: bold;">/</span> remoteHost.com:<span style="color: #000000; font-weight: bold;">/</span>remoteSyncFolder<br />
<br />
<span style="color: #666666; font-style: italic;">#remote-&gt;local</span><br />
rsync <span style="color: #660033;">-auv</span> <span style="color: #660033;">-e</span> <span style="color: #c20cb9; font-weight: bold;">ssh</span> remoteHost.com:<span style="color: #000000; font-weight: bold;">/</span>remoteSyncFolder mySyncFolder<span style="color: #000000; font-weight: bold;">/</span></div></td></tr></tbody></table></div>
<p>My solution uses rsync to power the transfer and adds logic to keep track of deleted files.  A list of all local deleted files is maintained on both sides (as of now no GUI exists so files must be deleted via terminal).  When a sync occurs the remote list of deleted files is pulled and all local copies are locally deleted if they have the identical size/mod time.  Now all of the files that have been deleted on the remote side are in sync locally but the locally deleted files still exist on the remote side.  These files will not be deleted until the remote computer initializes the sync and does the same process.  To avoid downloading files that have been deleted locally and not remotely, the local list of deleted files is used to tell rysnc not to blacklist these files. The same script is ran on the remote to sync its local files.</p>
<p><a href="http://www.hackengineer.com/wp-content/uploads/2012/05/Screen-Shot-2012-05-27-at-8.30.35-PM.png"><img class="alignnone  wp-image-384" title="mySyncFlow" src="http://www.hackengineer.com/wp-content/uploads/2012/05/Screen-Shot-2012-05-27-at-8.30.35-PM.png" alt="mySync flow diagram" width="765" height="298" /></a><br />
All this is seamlessly automated by adding the script to a cronjob set for every 15 minutes ( or any time interval of you choice) and a very basic dropbox has been implemented!  Now I can transition from my laptop to desktop and back with out having to worry about manually tranfering large amounts of data.  Best of all its free with practically unlimited space!</p>
<div class="codecolorer-container bash geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br /></div></td><td><div class="bash codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">*/</span><span style="color: #000000;">15</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">*</span> <span style="color: #000000; font-weight: bold;">/</span>usr<span style="color: #000000; font-weight: bold;">/</span>loca<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>syncMe.sh</div></td></tr></tbody></table></div>
<p>If your interested in doing this yourself I will be posting part II with a walk through of the code and how to set up DDNS/port forwarding/sftp/authentication on the mac.  All you need to sync data securely from anywhere!</p>
<p><a href="http://www.hackengineer.com/ddns-port-forwarding-and-ssh-auto-authentication-with-rsa-keys/" title="DDNS, Port Forwarding and SSH Auto Authentication with RSA keys">next page &#8212;></a></p>
<p><script type="text/javascript">// < ![CDATA[
// < ![CDATA[
google_ad_client = "ca-pub-7101321993029582";
/* mySync */
google_ad_slot = "7593802595";
google_ad_width = 468;
google_ad_height = 60;
// ]]&gt;</script><br />
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">// < ![CDATA[
// < ![CDATA[</p>
<p>// ]]&gt;</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackengineer.com/roll-your-own-2tb-dropbox/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Prototyping Structured Light Algorithm in Matlab</title>
		<link>http://www.hackengineer.com/prototyping-structured-light-algorithm-in-matlab/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=prototyping-structured-light-algorithm-in-matlab</link>
		<comments>http://www.hackengineer.com/prototyping-structured-light-algorithm-in-matlab/#comments</comments>
		<pubDate>Wed, 07 Mar 2012 05:51:12 +0000</pubDate>
		<dc:creator>hackengineer</dc:creator>
				<category><![CDATA[Computer Vision]]></category>
		<category><![CDATA[gaussian]]></category>
		<category><![CDATA[Matlab]]></category>
		<category><![CDATA[Structured Light]]></category>

		<guid isPermaLink="false">http://www.hackengineer.com/?p=261</guid>
		<description><![CDATA[Matlab provides a great environment to work with images and therefore a great place to develop the Structured Light Algorithm.  The beagle board was used to project the structured light images and capture the resulting scene.  These images were then loaded into matlab providing data needed to develop the algorithm.  Working with Hana Wang and [...]]]></description>
				<content:encoded><![CDATA[<p>Matlab provides a great environment to work with images and therefore a great place to develop the Structured Light Algorithm.  The beagle board was used to project the structured light images and capture the resulting scene.  These images were then loaded into matlab providing data needed to develop the algorithm.  Working with Hana Wang and Rajoshi Biswas, the majority of the algorithm was developed as follows:</p>
<p>At this point we were able to create decent point clouds with matlab.  The resulting pointclouds can be shown in matlab using the &#8216;mesh&#8217; function.  To add color us the &#8216;caxis&#8217; function.</p>
<div class="codecolorer-container matlab geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:600px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br />47<br />48<br />49<br />50<br />51<br />52<br />53<br />54<br />55<br />56<br />57<br />58<br />59<br />60<br />61<br />62<br />63<br />64<br />65<br />66<br />67<br />68<br />69<br />70<br />71<br />72<br />73<br />74<br />75<br />76<br />77<br />78<br />79<br />80<br />81<br />82<br />83<br />84<br />85<br />86<br />87<br />88<br />89<br />90<br />91<br />92<br />93<br />94<br />95<br />96<br />97<br />98<br />99<br />100<br />101<br />102<br />103<br />104<br />105<br />106<br />107<br />108<br />109<br />110<br />111<br />112<br />113<br />114<br />115<br />116<br />117<br />118<br />119<br />120<br />121<br />122<br />123<br />124<br /></div></td><td><div class="matlab codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #228B22;">%% Total number of Images n = 20 %%</span><br />
n = <span style="color: #33f;">18</span>;<br />
height = <span style="color: #33f;">480</span>;<br />
width = <span style="color: #33f;">640</span>;<br />
<span style="color: #228B22;">%scan = zeros(height, width, 3, n);</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/figure.html"><span style="color: #0000FF;">figure</span></a>;<br />
<br />
l=<span style="color: #080;">&#91;</span><span style="color: #33f;">1</span>,<span style="color: #33f;">3</span>,<span style="color: #33f;">5</span>,<span style="color: #33f;">7</span>,<span style="color: #33f;">9</span>,<span style="color: #33f;">11</span>,<span style="color: #33f;">13</span>,<span style="color: #33f;">15</span>,<span style="color: #33f;">17</span>,<span style="color: #33f;">19</span><span style="color: #080;">&#93;</span>; &nbsp; &nbsp; &nbsp; <span style="color: #228B22;">%% Two sequences, l and r</span><br />
r=<span style="color: #080;">&#91;</span><span style="color: #33f;">2</span>,<span style="color: #33f;">4</span>,<span style="color: #33f;">6</span>,<span style="color: #33f;">8</span>,<span style="color: #33f;">10</span>,<span style="color: #33f;">12</span>,<span style="color: #33f;">14</span>,<span style="color: #33f;">16</span>,<span style="color: #33f;">18</span>,<span style="color: #33f;">20</span><span style="color: #080;">&#93;</span>;<br />
binProjCol = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/zeros.html"><span style="color: #0000FF;">zeros</span></a><span style="color: #080;">&#40;</span>height,width,<span style="color: #33f;">9</span><span style="color: #080;">&#41;</span>;<br />
bright = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/zeros.html"><span style="color: #0000FF;">zeros</span></a><span style="color: #080;">&#40;</span>height,width,<span style="color: #33f;">9</span><span style="color: #080;">&#41;</span>;<br />
projCol = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/zeros.html"><span style="color: #0000FF;">zeros</span></a><span style="color: #080;">&#40;</span>height,width<span style="color: #080;">&#41;</span>;<br />
projColNormalized = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/zeros.html"><span style="color: #0000FF;">zeros</span></a><span style="color: #080;">&#40;</span>height,width<span style="color: #080;">&#41;</span>;<br />
dright = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/zeros.html"><span style="color: #0000FF;">zeros</span></a><span style="color: #080;">&#40;</span>height,width<span style="color: #080;">&#41;</span>;<br />
camCol = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/zeros.html"><span style="color: #0000FF;">zeros</span></a> <span style="color: #080;">&#40;</span>height, width<span style="color: #080;">&#41;</span>;<br />
camColNormalized = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/zeros.html"><span style="color: #0000FF;">zeros</span></a> <span style="color: #080;">&#40;</span>height, width<span style="color: #080;">&#41;</span>;<br />
<span style="color: #228B22;">%y = zeros (height, width);</span><br />
depth = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/zeros.html"><span style="color: #0000FF;">zeros</span></a> <span style="color: #080;">&#40;</span>height, width<span style="color: #080;">&#41;</span>;<br />
<span style="color: #228B22;">%% Read the images obtained by scanning %%</span><br />
<span style="color: #0000FF;">for</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/i.html"><span style="color: #0000FF;"><span style="color: #33f;">i</span></span></a> = <span style="color: #33f;">1</span> : n<br />
&nbsp; &nbsp; scan<span style="color: #080;">&#40;</span>:,:,:,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/i.html"><span style="color: #0000FF;"><span style="color: #33f;">i</span></span></a><span style="color: #080;">&#41;</span> = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/imread.html"><span style="color: #0000FF;">imread</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/strcat.html"><span style="color: #0000FF;">strcat</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/num2str.html"><span style="color: #0000FF;">num2str</span></a><span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/i.html"><span style="color: #0000FF;"><span style="color: #33f;">i</span></span></a><span style="color: #080;">&#41;</span> <span style="color:#A020F0;">'.png'</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; imshow<span style="color: #080;">&#40;</span>scan<span style="color: #080;">&#40;</span>:,:,:,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/i.html"><span style="color: #0000FF;"><span style="color: #33f;">i</span></span></a><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #228B22;">%pause;</span><br />
<span style="color: #0000FF;">end</span><br />
<br />
display<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Images read'</span><span style="color: #080;">&#41;</span><br />
<br />
<span style="color: #228B22;">%% To decode bit sequence %%</span><br />
<span style="color: #228B22;">%grayscan = zeros(height,width,n);</span><br />
grayProjCol = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/zeros.html"><span style="color: #0000FF;">zeros</span></a><span style="color: #080;">&#40;</span>height,width,n<span style="color: #080;">&#41;</span>;<br />
binary = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/zeros.html"><span style="color: #0000FF;">zeros</span></a><span style="color: #080;">&#40;</span>height,width<span style="color: #080;">&#41;</span>;<br />
<br />
fid = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fopen.html"><span style="color: #0000FF;">fopen</span></a><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'pointcloud_Steven.xyz'</span>,<span style="color:#A020F0;">'w'</span><span style="color: #080;">&#41;</span>;<br />
<span style="color: #228B22;">%% Convert from rgb2gray</span><br />
<span style="color: #0000FF;">for</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/i.html"><span style="color: #0000FF;"><span style="color: #33f;">i</span></span></a>=<span style="color: #33f;">1</span>:n<br />
&nbsp; &nbsp; <span style="color: #228B22;">%grayscan(:,:,i)= mat2gray(scan(:,:,:,i)); % used to verify sl patterns</span><br />
&nbsp; &nbsp; grayscan<span style="color: #080;">&#40;</span>:,:,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/i.html"><span style="color: #0000FF;"><span style="color: #33f;">i</span></span></a><span style="color: #080;">&#41;</span>= rgb2gray<span style="color: #080;">&#40;</span>scan<span style="color: #080;">&#40;</span>:,:,:,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/i.html"><span style="color: #0000FF;"><span style="color: #33f;">i</span></span></a><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%grayscan is a double</span><br />
&nbsp; &nbsp; imshow<span style="color: #080;">&#40;</span>grayscan<span style="color: #080;">&#40;</span>:,:,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/i.html"><span style="color: #0000FF;"><span style="color: #33f;">i</span></span></a><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #228B22;">% &nbsp; &nbsp;pause;</span><br />
<span style="color: #0000FF;">end</span><br />
<br />
<span style="color: #228B22;">%% Extract bitsequence, find column number and the final coordinates</span><br />
<span style="color: #0000FF;">for</span> k = <span style="color: #33f;">1</span> : height<br />
&nbsp; &nbsp; <span style="color: #0000FF;">for</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a> = <span style="color: #33f;">1</span>: width<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #228B22;">%% To extract bitsequence from the images</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">for</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/i.html"><span style="color: #0000FF;"><span style="color: #33f;">i</span></span></a>=<span style="color: #33f;">1</span>:<span style="color: #080;">&#40;</span>n/<span style="color: #33f;">2</span><span style="color: #080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">if</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/abs.html"><span style="color: #0000FF;">abs</span></a><span style="color: #080;">&#40;</span>grayscan<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a>,l<span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/i.html"><span style="color: #0000FF;"><span style="color: #33f;">i</span></span></a><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>-grayscan<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a>,r<span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/i.html"><span style="color: #0000FF;"><span style="color: #33f;">i</span></span></a><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>&gt;<span style="color: #33f;">50</span><span style="color: #080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">if</span> grayscan<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a>,l<span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/i.html"><span style="color: #0000FF;"><span style="color: #33f;">i</span></span></a><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span> &gt; grayscan<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a>,r<span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/i.html"><span style="color: #0000FF;"><span style="color: #33f;">i</span></span></a><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grayProjCol<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a>,l<span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/i.html"><span style="color: #0000FF;"><span style="color: #33f;">i</span></span></a><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>=<span style="color: #33f;">1</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #228B22;">%grayProjCol(k,j,r(i))=0;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; grayProjCol<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a>,l<span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/i.html"><span style="color: #0000FF;"><span style="color: #33f;">i</span></span></a><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>=<span style="color: #33f;">0</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #228B22;">%grayProjCol(k,j,r(i))=1;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">end</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">end</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">end</span><br />
&nbsp; &nbsp; <span style="color: #0000FF;">end</span><br />
<span style="color: #0000FF;">end</span><br />
<span style="color: #0000FF;">for</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/i.html"><span style="color: #0000FF;"><span style="color: #33f;">i</span></span></a>=<span style="color: #33f;">1</span>:n<br />
&nbsp; &nbsp; imshow<span style="color: #080;">&#40;</span>grayProjCol<span style="color: #080;">&#40;</span>:,:,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/i.html"><span style="color: #0000FF;"><span style="color: #33f;">i</span></span></a><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #228B22;">%pause;</span><br />
<span style="color: #0000FF;">end</span><br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/disp.html"><span style="color: #0000FF;">disp</span></a><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'bitsequence extracted'</span><span style="color: #080;">&#41;</span>;<br />
<span style="color: #228B22;">%% Convert the gray code sequence to decimal number = column number</span><br />
<br />
<span style="color: #0000FF;">for</span> k = <span style="color: #33f;">1</span> : height<br />
&nbsp; &nbsp; <span style="color: #0000FF;">for</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a> = <span style="color: #33f;">1</span>: width<br />
&nbsp; &nbsp; &nbsp; &nbsp; binProjCol<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a>,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> = grayProjCol<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a>,l<span style="color: #080;">&#40;</span><span style="color: #33f;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; projCol<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span> = binProjCol<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a>,<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>*<span style="color: #33f;">2</span>^<span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span>n/<span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>-<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">for</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/i.html"><span style="color: #0000FF;"><span style="color: #33f;">i</span></span></a> = <span style="color: #33f;">2</span> :<span style="color: #080;">&#40;</span>n/<span style="color: #33f;">2</span><span style="color: #080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; binProjCol<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a>,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/i.html"><span style="color: #0000FF;"><span style="color: #33f;">i</span></span></a><span style="color: #080;">&#41;</span> = xor<span style="color: #080;">&#40;</span>grayProjCol<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a>,l<span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/i.html"><span style="color: #0000FF;"><span style="color: #33f;">i</span></span></a><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>, binProjCol<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a>,<span style="color: #33f;">i</span>-<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span> <span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; projCol<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span> = projCol<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span> + <span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><span style="color: #33f;">2</span>^<span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span>n/<span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>-<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/i.html"><span style="color: #0000FF;"><span style="color: #33f;">i</span></span></a><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>*<span style="color: #080;">&#40;</span>binProjCol<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a>,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/i.html"><span style="color: #0000FF;"><span style="color: #33f;">i</span></span></a><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">end</span><br />
&nbsp; &nbsp; <span style="color: #0000FF;">end</span><br />
<span style="color: #0000FF;">end</span><br />
<br />
tempMax = <span style="color: #33f;">0</span>;<br />
tempMin = <span style="color: #33f;">2</span>^<span style="color: #080;">&#40;</span>n/<span style="color: #33f;">2</span><span style="color: #080;">&#41;</span>;<br />
projColMean = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/mean.html"><span style="color: #0000FF;">mean</span></a><span style="color: #080;">&#40;</span>projCol<span style="color: #080;">&#41;</span>;<br />
<span style="color: #0000FF;">for</span> k = <span style="color: #33f;">1</span> : height<br />
&nbsp; &nbsp; <span style="color: #0000FF;">for</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a> = <span style="color: #33f;">1</span>: width<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">if</span><span style="color: #080;">&#40;</span>projCol<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span>&gt;projColMean<span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span>+<span style="color: #33f;">150</span><span style="color: #080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; projCol<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span> = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/nan.html"><span style="color: #0000FF;">NaN</span></a>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">elseif</span> <span style="color: #080;">&#40;</span>projCol<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span>&gt; tempMax<span style="color: #080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempMax = projCol<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">end</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">if</span><span style="color: #080;">&#40;</span>projCol<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span>&lt;projcolmean <span style="color: #080;">&#40;</span><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span>-<span style="color: #33f;">20</span><span style="color: #080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; projCol<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span> = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/nan.html"><span style="color: #0000FF;">NaN</span></a>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">elseif</span> <span style="color: #080;">&#40;</span>projCol<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span>&lt; tempMin<span style="color: #080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; tempMin = projCol<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">end</span><br />
&nbsp; &nbsp; <span style="color: #0000FF;">end</span><br />
<span style="color: #0000FF;">end</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #080;">&#91;</span>r,firstCamCol,z<span style="color: #080;">&#93;</span> = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/find.html"><span style="color: #0000FF;">find</span></a><span style="color: #080;">&#40;</span>projCol,<span style="color: #33f;">1</span>,<span style="color:#A020F0;">'first'</span><span style="color: #080;">&#41;</span>;<br />
<span style="color: #080;">&#91;</span>r,lastCamCol,z<span style="color: #080;">&#93;</span> = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/find.html"><span style="color: #0000FF;">find</span></a><span style="color: #080;">&#40;</span>projCol,<span style="color: #33f;">1</span>,<span style="color:#A020F0;">'last'</span><span style="color: #080;">&#41;</span>;<br />
<span style="color: #080;">&#91;</span>r,c,projColList<span style="color: #080;">&#93;</span> = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/find.html"><span style="color: #0000FF;">find</span></a><span style="color: #080;">&#40;</span>projCol<span style="color: #080;">&#41;</span>;<br />
numOfCamCol = lastCamCol-firstCamCol;<br />
firstProjCol = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/min.html"><span style="color: #0000FF;">min</span></a><span style="color: #080;">&#40;</span>projColList<span style="color: #080;">&#41;</span>;<br />
lastProjCol = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/max.html"><span style="color: #0000FF;">max</span></a><span style="color: #080;">&#40;</span>projColList<span style="color: #080;">&#41;</span>;<br />
numOfProjCol = lastProjCol - firstProjCol;<br />
<br />
<br />
<span style="color: #0000FF;">for</span> k = <span style="color: #33f;">1</span> : height<br />
&nbsp; &nbsp; <span style="color: #0000FF;">for</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a> = <span style="color: #33f;">1</span>: width<br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #228B22;">%y(k,j) = k;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">if</span> <span style="color: #080;">&#40;</span>projCol<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span> ~= <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/nan.html"><span style="color: #0000FF;">NaN</span></a><span style="color: #080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #228B22;">%% Calculate the coordinates of the point</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; projColNormalized<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span> = <span style="color: #080;">&#40;</span>projCol<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span>-<span style="color: #080;">&#40;</span>firstProjCol-<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>/<span style="color: #080;">&#40;</span>numOfProjCol<span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%480</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; camColNormalized<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span> = <span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><span style="color: #33f;">j</span>-<span style="color: #080;">&#40;</span>firstCamCol-<span style="color: #33f;">1</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>/numOfCamCol<span style="color: #080;">&#41;</span>; <span style="color: #228B22;">%width</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">if</span> <span style="color: #080;">&#40;</span> <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/abs.html"><span style="color: #0000FF;">abs</span></a><span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span><span style="color: #080;">&#40;</span>camColNormalized<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span>- projColNormalized<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>&amp;lt;<span style="color: #33f;">0.10</span><span style="color: #080;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; depth<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span> = <span style="color: #080;">&#40;</span>camColNormalized<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span>-projColNormalized<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #228B22;">%depth(k,j) = ((projColNormalized(k,j)-(camColNormalized(k,j))));</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">end</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">else</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; depth<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span> = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/nan.html"><span style="color: #0000FF;">NaN</span></a>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0000FF;">end</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/fprintf.html"><span style="color: #0000FF;">fprintf</span></a><span style="color: #080;">&#40;</span>fid,<span style="color:#A020F0;">'%4.1f %4.1f %4.9f %d \r\n'</span>,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a>,k,depth<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span>,projCol<span style="color: #080;">&#40;</span>k,<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/j.html"><span style="color: #0000FF;"><span style="color: #33f;">j</span></span></a><span style="color: #080;">&#41;</span><span style="color: #080;">&#41;</span>;<br />
&nbsp; &nbsp; <span style="color: #0000FF;">end</span><br />
<span style="color: #0000FF;">end</span></div></td></tr></tbody></table></div>

<a href='http://www.hackengineer.com/prototyping-structured-light-algorithm-in-matlab/donkey3d_3/' title='donkey3D_matlab'><img width="150" height="150" src="http://www.hackengineer.com/wp-content/uploads/2012/03/donkey3D_3-150x150.png" class="attachment-thumbnail" alt="donkey3D_matlab" /></a>
<a href='http://www.hackengineer.com/prototyping-structured-light-algorithm-in-matlab/donkey3d_4/' title='donkey3D_Matlab_color'><img width="150" height="150" src="http://www.hackengineer.com/wp-content/uploads/2012/03/donkey3D_4-150x150.png" class="attachment-thumbnail" alt="donkey3D_Matlab_color" /></a>
<a href='http://www.hackengineer.com/prototyping-structured-light-algorithm-in-matlab/donkey3d_5/' title='donkey3D_Matlab_color2'><img width="150" height="150" src="http://www.hackengineer.com/wp-content/uploads/2012/03/donkey3D_5-150x150.png" class="attachment-thumbnail" alt="donkey3D_Matlab_color2" /></a>

<p>The code above works but notice it does not handle cleaning up any erroneous points where no Structured Light is found (fuzzy black and white areas).   This was implemented after the code was ported to C.  Since the noise has a gaussian distribution it can be filtered out using a gaussian filter.</p>
<div class="codecolorer-container matlab geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /></div></td><td><div class="matlab codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/disp.html"><span style="color: #0000FF;">disp</span></a><span style="color: #080;">&#40;</span><span style="color:#A020F0;">'Calculated depth'</span><span style="color: #080;">&#41;</span>;<br />
zInv = <a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/flipud.html"><span style="color: #0000FF;">flipud</span></a><span style="color: #080;">&#40;</span>depth<span style="color: #080;">&#41;</span>;<br />
H2 = fspecial<span style="color: #080;">&#40;</span><span style="color:#A020F0;">'gaussian'</span>,<span style="color: #33f;">5</span>,.6<span style="color: #080;">&#41;</span>;<br />
zInvFilt = imfilter<span style="color: #080;">&#40;</span>zInv,H2<span style="color: #080;">&#41;</span>;<br />
<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/figure.html"><span style="color: #0000FF;">figure</span></a>;<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/mesh.html"><span style="color: #0000FF;">mesh</span></a><span style="color: #080;">&#40;</span>zInvFilt<span style="color: #080;">&#41;</span>;<br />
<a href="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/caxis.html"><span style="color: #0000FF;">caxis</span></a> <span style="color: #080;">&#40;</span><span style="color: #080;">&#91;</span><span style="color: #33f;">0.001</span> <span style="color: #33f;">0.013</span><span style="color: #080;">&#93;</span><span style="color: #080;">&#41;</span>;<br />
<span style="color: #228B22;">%caxis ([-0.007 0.0125])</span></div></td></tr></tbody></table></div>
<p>This cleans up the noise and provides good results.  The video below shows a better representation of the 3D model.  The noise has been greatly reduced in the blue area where no SL patterns were projected. There are a few areas that were not the best surfaces to reflect the projected light such as the glossy surface that is close to being parallel with the camera.<iframe src="http://www.youtube.com/embed/KxFlG4Vcurs" frameborder="0" width="640" height="360"></iframe></p>
<p style="text-align: center;"><a title="Installing a pushbutton and using BeagleBoard GPIO interrupts" href="http://www.hackengineer.com/installing-a-pushbutton-and-using-beagleboard-gpio-interrupts/">&lt;&#8211;Previous </a>    <a title="Kinect-Like 3D camera" href="http://www.hackengineer.com/3dcam">Project Home</a>     <a title="Downloading the SL source code and compiling natively on Beagle board" href="http://www.hackengineer.com/downloading-the-sl-source-code-and-compiling-natively-on-beagle-board/">Next &#8211;&gt;</a></p>
<p><script type="text/javascript">// < ![CDATA[
// < ![CDATA[
// < ![CDATA[
// < ![CDATA[
// < ![CDATA[
// < ![CDATA[  google_ad_client = "ca-pub-7101321993029582"; /* banner3DCam */ google_ad_slot = "3562568900"; google_ad_width = 468; google_ad_height = 60;
// ]]&gt;</script><br />
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">// < ![CDATA[
// < ![CDATA[
// < ![CDATA[
// < ![CDATA[
// < ![CDATA[
// < ![CDATA[
// ]]&gt;</script></projcolmean></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackengineer.com/prototyping-structured-light-algorithm-in-matlab/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Rendering results with Meshlab</title>
		<link>http://www.hackengineer.com/rendering-results-with-meshlab/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rendering-results-with-meshlab</link>
		<comments>http://www.hackengineer.com/rendering-results-with-meshlab/#comments</comments>
		<pubDate>Sun, 26 Feb 2012 08:54:28 +0000</pubDate>
		<dc:creator>hackengineer</dc:creator>
				<category><![CDATA[Computer Vision]]></category>
		<category><![CDATA[3D]]></category>
		<category><![CDATA[meshlab]]></category>
		<category><![CDATA[point cloud]]></category>

		<guid isPermaLink="false">http://hackengineer.wordpress.com/?p=122</guid>
		<description><![CDATA[Meshlab is pretty great for 3D point clouds and its free!  Here are a few steps that help really make the point clouds look good. Open Meshlab and open the .xyz file from the 3D camera Delete any points that look like they dont belong ( if you only see a small group of points [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://meshlab.sourceforge.net/">Meshlab</a> is pretty great for 3D point clouds and its free!  Here are a few steps that help really make the point clouds look good.</p>
<ul>
<li>Open Meshlab and open the .xyz file from the 3D camera</li>
<li>Delete any points that look like they dont belong ( if you only see a small group of points you are probably zoomed out really far dude to a rogue point; delete it and zoom in)</li>
</ul>
<p style="text-align: center;"><a href="http://hackengineer.files.wordpress.com/2012/02/screen-shot-2012-02-28-at-10-17-40-pm.png"><img class="aligncenter size-medium wp-image-158" title="xyzZoomedOut" src="http://hackengineer.files.wordpress.com/2012/02/screen-shot-2012-02-28-at-10-15-45-pm.png?w=300" alt="" width="300" height="261" /><img class="aligncenter size-medium wp-image-159" title="xyz" src="http://hackengineer.files.wordpress.com/2012/02/screen-shot-2012-02-28-at-10-17-40-pm.png?w=300" alt="" width="300" height="285" /></a></p>
<ul>
<li>Orient the pointcloud so that it represents the original scene (looking straight at it).  We will now compute the normals for each point.
<ul>
<li>Filters-&gt;point Set-&gt;Compute Normals For Point Sets
<ul>
<li># of neighbors = 100</li>
<li>Check flip normals w.r.t viewpoint</li>
</ul>
</li>
<li>Render-&gt;Lighting-&gt;Light On</li>
<li>The points should now have a shading effect depending on there normal.  To verify render-&gt;Show Vertex Normals</li>
</ul>
</li>
</ul>
<p><a href="http://hackengineer.files.wordpress.com/2012/02/screen-shot-2012-02-28-at-10-18-50-pm.png"><img class="aligncenter size-medium wp-image-160" title="xyzNormalized" src="http://hackengineer.files.wordpress.com/2012/02/screen-shot-2012-02-28-at-10-18-50-pm.png?w=300" alt="" width="300" height="293" /></a><img class="aligncenter size-medium wp-image-163" title="xzyVertexNorms" src="http://hackengineer.files.wordpress.com/2012/02/screen-shot-2012-02-28-at-10-19-38-pm.png?w=300" alt="" width="300" height="272" /></p>
<ul>
<li>Lets add some color to the depth to make it stand out (darker as depth increases).  Meshlab has a tool to set color per vertex.  Use the z coordinate as a variable as shown below.
<ul>
<li>Filters-&gt;Color Creation and Processing-&gt;Per Vertex Color Function</li>
<li>For each of the colors adjust this formula for good results; z*0.78+125</li>
</ul>
</li>
</ul>
<p><a href="http://hackengineer.files.wordpress.com/2012/02/screen-shot-2012-02-28-at-10-27-11-pm.png"><img class="aligncenter size-medium wp-image-162" title="xyzWithColor" src="http://hackengineer.files.wordpress.com/2012/02/screen-shot-2012-02-28-at-10-27-11-pm.png?w=300" alt="" width="300" height="288" /></a></p>
<ul>
<li>We should have a good looking pointcloud at this point.  We can also generate a surface mesh with our point cloud.
<ul>
<li>Filters-&gt;Point Set-&gt;Surface Reconstruction:Poisson
<ul>
<li>Set Octree Depth to 10 or so</li>
</ul>
</li>
<li>The toolbar has a list of different view options.  Click the ones with cylinders on them to view in surface mode.</li>
</ul>
</li>
</ul>
<p><a href="http://hackengineer.files.wordpress.com/2012/02/screen-shot-2012-02-28-at-10-57-33-pm.png"><img class="aligncenter size-medium wp-image-164" title="xyzMeshFrame" src="http://hackengineer.files.wordpress.com/2012/02/screen-shot-2012-02-28-at-10-57-33-pm.png?w=300" alt="" width="300" height="288" /></a><a href="http://hackengineer.files.wordpress.com/2012/02/screen-shot-2012-02-28-at-10-58-06-pm.png"><img class="aligncenter size-medium wp-image-165" title="xyzSurface" src="http://hackengineer.files.wordpress.com/2012/02/screen-shot-2012-02-28-at-10-58-06-pm.png?w=300" alt="" width="300" height="292" /></a></p>
<p>And there you have it!  3D picture taken with the beagleboard.  The results look pretty good for a completely portable setup and HVGA projector.  I look forward to seeing what others can do with high resolution hardware!  Thanks for reading!</p>
<p style="text-align: center;"><a title="Downloading the SL source code and compiling natively on Beagle board" href="http://www.hackengineer.com/downloading-the-sl-source-code-and-compiling-natively-on-beagle-board/">&lt;&#8211;Previous</a>     <a title="Kinect-Like 3D camera" href="http://www.hackengineer.com/3dcam">Project Home</a></p>
<p><script type="text/javascript"><!--
google_ad_client = "ca-pub-7101321993029582";
/* banner3DCam */
google_ad_slot = "3562568900";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackengineer.com/rendering-results-with-meshlab/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Downloading the SL source code and compiling natively on Beagle board</title>
		<link>http://www.hackengineer.com/downloading-the-sl-source-code-and-compiling-natively-on-beagle-board/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=downloading-the-sl-source-code-and-compiling-natively-on-beagle-board</link>
		<comments>http://www.hackengineer.com/downloading-the-sl-source-code-and-compiling-natively-on-beagle-board/#comments</comments>
		<pubDate>Sun, 26 Feb 2012 08:52:34 +0000</pubDate>
		<dc:creator>hackengineer</dc:creator>
				<category><![CDATA[Computer Vision]]></category>
		<category><![CDATA[compile]]></category>
		<category><![CDATA[Makefile]]></category>
		<category><![CDATA[Structured Light]]></category>
		<category><![CDATA[toolchain]]></category>

		<guid isPermaLink="false">http://hackengineer.wordpress.com/?p=118</guid>
		<description><![CDATA[The source code is posted on google code.  You can download it straight from the interwebs with beagleboard.  Download the folder, run the Makefile, run ./slGPIO.cpp, and push the button to start scanning!  Once completed if the program finds a usbdrive it will save the point cloud there.  If not it will be saved in [...]]]></description>
				<content:encoded><![CDATA[<p>The source code is posted on <a href="http://code.google.com/p/kinect-like-3d-camera/">google code</a>.  You can download it straight from the interwebs with beagleboard.  Download the folder, run the Makefile, run ./slGPIO.cpp, and push the button to start scanning!  Once completed if the program finds a usbdrive it will save the point cloud there.  If not it will be saved in the results folder.</p>
<p>Contents</p>
<ul>
<li>Graycoded Structured Light images [20]</li>
<li>Makefile  // includes links to openCV cflags and libs.  Use this to natively compile SL code.</li>
<li>sl.cpp  //sl program</li>
<li>slGPIO.cpp  // starting point.  Initializes GPIO interrupt and calls sl.cpp</li>
<li>results folder //result go here if no SDcard is found</li>
</ul>
<div><strong><span style="text-decoration: underline;">Makefile</span></strong></div>
<div></div>
<div>Makefiles are a timesaver, so Ill post the entire contents here for copy/paste purposes (myself included).  Notice the shell call to pkg-config.  This is a really handy way to link to the location of the openCV cflags and libs</div>
<div>
<pre class="brush: shell; gutter: true"># &#039;make&#039;        build executable file &#039;mycc&#039;
# &#039;make clean&#039;  removes all .o and executable files

.PHONY: depend clean

SCAN = slGPIO
DISPLAY = display

CC = gcc
CFLAGS := -g -Wall -DLINUX $(shell pkg-config opencv --cflags)
LIBs := $(shell pkg-config opencv --libs)
LFLAGS =

# Source files
SRCs := \
	slGPIO.cpp
	#sl.cpp \

# List of object files to compile
OBJs = $(SRCs:.cpp=.o)

all: $(SCAN)

$(DISPLAY): display.o
	gcc -g -Wall 

$(SCAN): $(OBJs)
	$(CC) $(CFLAGS) $(INCLUDES) -o $(SCAN) $(OBJs) $(LFLAGS) $(LIBs)

# 	cp $(SCAN) Release/

%.o: %.cpp %.h
	$(CC) $(CFLAGS) $(INCLUDES) -c $&lt;  -o $@

clean:
	$(RM) *.o *~ $(SCAN)</pre>
</div>
<div><strong style="text-decoration: underline;">sl.cpp</strong></div>
<p>The sl code was written by myself and Peter Hokanson.  Its not to long and is broken up into 3 sections.  The source code will likely be easier to read with proper highlighting.  OpenCV was only used to manipulate the camera and projector.  Part 1 below projects and captures the images:</p>
<pre class="brush: cpp; gutter: true">#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;math.h&gt;
#include &lt;assert.h&gt; // For my own paranoia&#039;s sake
#include &lt;opencv/cv.h&gt;
#include &lt;opencv/highgui.h&gt;
#define WHITE_THRESHOLD 5
#define BLACK_THRESHOLD -5
#define NUM_IMAGES 18
#define CAM_WIDTH 640
#define PROJ_WIDTH 640
#define Z_SCALE 500   //250 works

//#define TEST_RECOGNIZER

int gray2bin(int gray);
bool file_exists(const char * filename);
int slScanner(void);

int slScanner(void)
{
	//*********PART1 - Project SL and Cap Imgs************
	//Initialize
	IplImage* slPat[NUM_IMAGES] = {0};
	IplImage* capImg[NUM_IMAGES + 1] = {0};
// 	int height,width,step,channels;
	uchar *data;
	char s[128];
	int i,outFileNum=0;

	//Usage
	//if(argc&gt;1){
	//	printf(&quot;Usage: sl images are in same folder named grayIMGx.png and range from 0-19\n&quot;);
	//	exit(0);
	//}

	//Load sl images
#ifndef TEST_RECOGNIZER
	for(int i=0;i&lt;NUM_IMAGES;i++){
		sprintf(s,&quot;%d.png&quot;,i+1);
		slPat[i]=cvLoadImage(s);
		if(!slPat[i]){
			printf(&quot;Could not load image file: %s\n&quot;,s);
			exit(0);
		}
	}

	// create a window
	cvStartWindowThread();
	cvNamedWindow(&quot;mainWin&quot;, CV_WINDOW_NORMAL);
	cvSetWindowProperty(&quot;mainWin&quot;, CV_WND_PROP_FULLSCREEN, CV_WINDOW_FULLSCREEN);

	// pause
	cvWaitKey(500);

	// set up camera
	CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
	if (!capture){
		fprintf( stderr, &quot;Camera not found \n&quot;);
		getchar();
		exit(0);
	}
	cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT, 480);
	cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, 640);
	//cvSetCaptureProperty(capture, CV_CAP_PROP_FPS, 5);
	//cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_COUNT, 1);
	//Maybe this will fix the frame delay?

	//Display images and capture
	for(int i=0;i&lt;NUM_IMAGES;i++){

		// show the image
		cvShowImage(&quot;mainWin&quot;, slPat[i] );

		// pause
		cvWaitKey(5);

		// capture the image
		cvGrabFrame(capture);
		cvGrabFrame(capture);
		cvGrabFrame(capture);
		cvGrabFrame(capture);
		cvGrabFrame(capture); //Get to last frame in buffer
		capImg[NUM_IMAGES + 1] = cvRetrieveFrame(capture);   

		// write the image
		//cvWriteFrame(writer, capImg[i]);
		capImg[i] = cvCloneImage(capImg[NUM_IMAGES + 1]);
		sprintf(s,&quot;results/%d.png&quot;,i+1);
		cvSaveImage(s,capImg[i]);
	}

	cvReleaseCapture(&amp;capture);

	//Display captured images for debug
	for(int i=0;i&lt;NUM_IMAGES;i++){

		// show the image (for debug)
		cvShowImage(&quot;mainWin&quot;, capImg[i] );

		// pause
		cvWaitKey(100);
	}

#else // TEST_RECOGNIZER
	for(int i=0;i&lt;NUM_IMAGES;i++) {
		sprintf(s,&quot;setupp/%d.png&quot;,i+1);
		capImg[i]=cvLoadImage(s);
		if(!capImg[i]){
			printf(&quot;Could not load image file: %s\n&quot;,s);
			exit(0);
		}
	}
#endif</pre>
<p>Part 2 contains the SL decoding and depth redering algorithm:</p>
<pre class="brush: cpp; gutter: true">	//*********PART2 - Decode captured images************

	// First initialize a matrix for showing the decoded values, needs 10 bits,
	// so could move to short* if memory becomes an issue.
	int *columnmap = (int*)calloc(capImg[0]-&gt;imageSize, sizeof(int));
	if(columnmap == NULL) {
		printf(&quot;Allocation for column map failed! Aborting\n&quot;);
		return 1;
	}

	// We&#039;re going to step over matching inverse pairs of projected patterns, and
	// perform a threshold operation to determine whether a given pixel is white
	// or not.
	for(int i=0;i&lt;NUM_IMAGES;i+=2){
		printf(&quot;Processing scan patterns %d, %d\n&quot;,i,i+1);
		uchar* data1 = (uchar*)capImg[i]-&gt;imageData;
		uchar* data0 = (uchar*)capImg[i+1]-&gt;imageData;

		for(int y=0;y&lt;capImg[i]-&gt;height;y++){
			for(int x=0;x&lt;capImg[i]-&gt;width;x++) {
				int u = y*capImg[i]-&gt;widthStep+x*capImg[i]-&gt;nChannels;
				if(columnmap[u] == -1){
					// We have discarded this pixel
					continue;
				}
				int gray1 = data1[u] + data1[u+1] + data1[u+2];
				int gray0 = data0[u] + data0[u+1] + data0[u+2]; 

				// Shift our Gray code bit vector
				columnmap[u] = columnmap[u] &lt;&lt; 1;
				if(gray1 - gray0 &gt; WHITE_THRESHOLD){
					// We have a white pixel!
					assert((columnmap[u] &amp; 1) == 0);
					columnmap[u] |= 1;
				} else if(gray1 - gray0 &lt; BLACK_THRESHOLD){
					// A black pixel
					assert((columnmap[u] &amp; 1) == 0);
					columnmap[u] |= 0;
				} else {
					// Not quite sure about this pixel: Punt!
					columnmap[u] = -1;
				}
			}
		}

		// Avoid memory leaks!
// 		printf(&quot;Releasing images %d, %d\n&quot;, i,i+1);
// 		cvReleaseImage(&amp;capImg[i]);
// 		cvReleaseImage(&amp;capImg[i+1]);
	}

	// Convert Gray code to decimal
	for(i = 0; i &lt; capImg[0]-&gt;imageSize; i++){
// 		int tmp = columnmap[i];
		columnmap[i] = gray2bin(columnmap[i]);
// 		printf(&quot;Gray: %4x -&gt; Binary %4x\n&quot;,tmp,columnmap[i]);
	}

	// TODO: Test against projector patterns -&gt; 0 disparity
	// TODO: Test with SL images

    //Find available filename on jumpdrive
    sprintf(s,&quot;/media/sda1/result%d.xyz&quot;,outFileNum);
    while (file_exists(s)) {
        outFileNum++;
        sprintf(s,&quot;/media/sda1/result%d.xyz&quot;,outFileNum);
    }

	// Write the pointcloud
	FILE *f = fopen(s,&quot;w&quot;);
	if(f == NULL) {
	  printf(&quot;Error opening %s:  Trying local directory...\n&quot;,s);
	  FILE *f = fopen(&quot;result.xyz&quot;,&quot;w&quot;);
	  if(f == NULL) {
            printf(&quot;Error opening result.xyz:  Aborting!\n&quot;);
            return 1;
	  }
	}

	printf(&quot;Writing pointcloud...\n&quot;);
	for(int y = 0;y &lt; capImg[0]-&gt;height; y++){
		for(int x = 0;x &lt; capImg[0]-&gt;width; x++) {
			int u = y*capImg[0]-&gt;widthStep+x*capImg[0]-&gt;nChannels;

			if(columnmap[u] == -1) continue; // Discarded!

			float disp = (float)columnmap[u]/(PROJ_WIDTH) - (float)x/CAM_WIDTH;
// 			printf(&quot;%f\n&quot;,disp);
			if(disp == 0.0) continue;
			float z = (float)Z_SCALE/(disp);

			fprintf(f,&quot;%d %d %f\n&quot;,x,y,z);
		}
	}

	fclose(f);</pre>
<p>Part 3 is a work in progress.  All thats contained here is some cleanup.</p>
<pre class="brush: cpp; gutter: true">	//*******Part3 - Project 3D object with openGL*********

	for(int i = 0; i &lt; NUM_IMAGES; i++){
		cvReleaseImage(&amp;capImg[i]);
		//cvReleaseImage(&amp;slPat[i]);
	}
	//Exit
	cvDestroyWindow(&quot;mainWin&quot;);
	free(columnmap);
	return 0;
}</pre>
<p style="text-align: center;"><a title="Prototyping Structured Light Algorithm in Matlab" href="http://www.hackengineer.com/prototyping-structured-light-algorithm-in-matlab/">&lt;&#8211;Previous</a>     <a title="Kinect-Like 3D camera" href="http://www.hackengineer.com/3dcam">Project Home</a>     <a title="Rendering results with Meshlab" href="http://www.hackengineer.com/rendering-results-with-meshlab/">Next &#8211;&gt;</a></p>
<p><script type="text/javascript">// < ![CDATA[
// < ![CDATA[
// < ![CDATA[
// < ![CDATA[ google_ad_client = "ca-pub-7101321993029582"; /* banner3DCam */ google_ad_slot = "3562568900"; google_ad_width = 468; google_ad_height = 60;
// ]]&gt;</script><br />
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">// < ![CDATA[
// < ![CDATA[
// < ![CDATA[
// < ![CDATA[
// ]]&gt;</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackengineer.com/downloading-the-sl-source-code-and-compiling-natively-on-beagle-board/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Installing a pushbutton and using BeagleBoard GPIO interrupts</title>
		<link>http://www.hackengineer.com/installing-a-pushbutton-and-using-beagleboard-gpio-interrupts/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=installing-a-pushbutton-and-using-beagleboard-gpio-interrupts</link>
		<comments>http://www.hackengineer.com/installing-a-pushbutton-and-using-beagleboard-gpio-interrupts/#comments</comments>
		<pubDate>Sun, 26 Feb 2012 08:51:08 +0000</pubDate>
		<dc:creator>hackengineer</dc:creator>
				<category><![CDATA[Computer Vision]]></category>
		<category><![CDATA[BeagleBoard]]></category>
		<category><![CDATA[GPIO]]></category>
		<category><![CDATA[interrupt]]></category>

		<guid isPermaLink="false">http://hackengineer.wordpress.com/?p=115</guid>
		<description><![CDATA[The pushbutton allows the user to start a 3D scan by the push of a button, similar to how a real portable camera works.  I wasnt able to access the beagleboards user button with software, so I used a simple SPST switch and wired one side to GND and the other to GPIO 139.  I [...]]]></description>
				<content:encoded><![CDATA[<p>The pushbutton allows the user to start a 3D scan by the push of a button, similar to how a real portable camera works.  I wasnt able to access the beagleboards user button with software, so I used a simple SPST switch and wired one side to GND and the other to GPIO 139.  I mounted the button on the topside of the polycarb housing.  With project deadlines luming, I didn&#8217;t add any hardware de-bouncing, but this can be done later in software.</p>

<a href='http://www.hackengineer.com/installing-a-pushbutton-and-using-beagleboard-gpio-interrupts/img_0678/' title='gpioWiring'><img width="150" height="150" src="http://www.hackengineer.com/wp-content/uploads/2012/02/img_0678-e1330482345938-150x150.jpg" class="attachment-thumbnail" alt="gpioWiring" /></a>
<a href='http://www.hackengineer.com/installing-a-pushbutton-and-using-beagleboard-gpio-interrupts/img_0679/' title='GPIO'><img width="150" height="150" src="http://www.hackengineer.com/wp-content/uploads/2012/02/img_0679-e1330482418647-150x150.jpg" class="attachment-thumbnail" alt="GPIO" /></a>

<p>The guys over at ridgerun have a <a href="https://www.ridgerun.com/developer/wiki/index.php/How_to_use_GPIO_signals">great example</a> on GPIO and interrupts, which I have modified to suit my purposes.  I have posted modifications below:</p>
<p><span style="text-decoration: underline;"><strong>GPIO Setup:</strong></span></p>
<pre class="brush: cpp; gutter: true">gpio = 139; // SPST connected to GPIO 139
gpio_export(gpio); //export to user space
gpio_set_dir(gpio, 0); //set direction to input
gpio_set_edge(gpio, “falling”);  // Set interrupt edge, can be rising, falling or both.
gpio_fd = gpio_fd_open(gpio);</pre>
<p><span style="text-decoration: underline;"><strong>Upon interrupt:</strong></span></p>
<pre class="brush: cpp; gutter: true">printf(&quot;Starting Scann&quot;);
slScanner(); // run the sl code
printf(&quot;Scan Completen&quot;);
printf(&quot;Press button to scan again or Ctrl C to quit&quot;);</pre>
<p style="text-align: center;"><a title="Installing your camera module and ENV.txt" href="http://www.hackengineer.com/installing-your-camera-module-and-env-txt/">&lt;&#8211;Previous</a>     <a title="Kinect-Like 3D camera" href="http://www.hackengineer.com/3dcam">Project Home</a>     <a title="Prototyping Structured Light Algorithm in Matlab" href="http://www.hackengineer.com/prototyping-structured-light-algorithm-in-matlab/">Next &#8211;&gt;</a></p>
<p><script type="text/javascript">// < ![CDATA[
// < ![CDATA[
// < ![CDATA[
// < ![CDATA[
// < ![CDATA[
google_ad_client = "ca-pub-7101321993029582";
/* banner3DCam */
google_ad_slot = "3562568900";
google_ad_width = 468;
google_ad_height = 60;
// ]]&gt;</script><br />
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">// < ![CDATA[
// < ![CDATA[
// < ![CDATA[
// < ![CDATA[
// < ![CDATA[</p>
<p>// ]]&gt;</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackengineer.com/installing-a-pushbutton-and-using-beagleboard-gpio-interrupts/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Installing your camera module and ENV.txt</title>
		<link>http://www.hackengineer.com/installing-your-camera-module-and-env-txt/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=installing-your-camera-module-and-env-txt</link>
		<comments>http://www.hackengineer.com/installing-your-camera-module-and-env-txt/#comments</comments>
		<pubDate>Sun, 26 Feb 2012 08:49:37 +0000</pubDate>
		<dc:creator>hackengineer</dc:creator>
				<category><![CDATA[Computer Vision]]></category>
		<category><![CDATA[BeagleBoard]]></category>
		<category><![CDATA[camera module]]></category>
		<category><![CDATA[lens]]></category>

		<guid isPermaLink="false">http://hackengineer.wordpress.com/?p=112</guid>
		<description><![CDATA[Setting up the Leopardboard 365 VGA Camera board is pretty straight forward.  Connect it to the beagleboards camera header.  Edit the UENV.TXT file with the following line.  If it does not exist create it in /media/mmcblk0p1/UENV.TXT camera=lbcmvga We also need to build a jig to mount the lens to the camera.  Since the lens uses a magnetism to adhere a metal washer will [...]]]></description>
				<content:encoded><![CDATA[<p>Setting up the Leopardboard 365 VGA Camera board is pretty straight forward.  Connect it to the beagleboards camera header.  Edit the UENV.TXT file with the following line.  If it does not exist create it in /media/mmcblk0p1/UENV.TXT</p>
<ul>
<li>camera=lbcmvga</li>
</ul>
<p>We also need to build a jig to mount the lens to the camera.  Since the lens uses a magnetism to adhere a metal washer will do the trick.  A hot-glue gun works great for attaching the washer to the camera board.</p>

<a href='http://www.hackengineer.com/installing-your-camera-module-and-env-txt/img_0674-2/' title='cameraJig'><img width="150" height="150" src="http://www.hackengineer.com/wp-content/uploads/2012/02/img_06741-e1330480981996-150x150.jpg" class="attachment-thumbnail" alt="cameraJig" /></a>
<a href='http://www.hackengineer.com/installing-your-camera-module-and-env-txt/img_0675/' title='cameraJigWithLens'><img width="150" height="150" src="http://www.hackengineer.com/wp-content/uploads/2012/02/img_0675-e1330481146769-150x150.jpg" class="attachment-thumbnail" alt="cameraJigWithLens" /></a>

<p style="text-align: center;"><a title="Setting up your BeagleBoard with Angstrom, toolchain, and openCV" href="http://www.hackengineer.com/setting-up-your-beagleboard-with-angstrom-toolchain-and-opencv/">&lt;&#8211;Previous</a>     <a title="Kinect-Like 3D camera" href="http://www.hackengineer.com/3dcam">Project Home</a>     <a title="Installing a pushbutton and using BeagleBoard GPIO interrupts" href="http://www.hackengineer.com/installing-a-pushbutton-and-using-beagleboard-gpio-interrupts/">Next &#8211;&gt;</a></p>
<p><script type="text/javascript">// < ![CDATA[
// < ![CDATA[
google_ad_client = "ca-pub-7101321993029582";
/* banner3DCam */
google_ad_slot = "3562568900";
google_ad_width = 468;
google_ad_height = 60;
// ]]&gt;</script><br />
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">// < ![CDATA[
// < ![CDATA[</p>
<p>// ]]&gt;</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackengineer.com/installing-your-camera-module-and-env-txt/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting up your BeagleBoard with Angstrom, toolchain, and openCV</title>
		<link>http://www.hackengineer.com/setting-up-your-beagleboard-with-angstrom-toolchain-and-opencv/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=setting-up-your-beagleboard-with-angstrom-toolchain-and-opencv</link>
		<comments>http://www.hackengineer.com/setting-up-your-beagleboard-with-angstrom-toolchain-and-opencv/#comments</comments>
		<pubDate>Sun, 26 Feb 2012 08:23:46 +0000</pubDate>
		<dc:creator>hackengineer</dc:creator>
				<category><![CDATA[Computer Vision]]></category>
		<category><![CDATA[Angstrom]]></category>
		<category><![CDATA[BeagleBoard]]></category>
		<category><![CDATA[Narcissus]]></category>
		<category><![CDATA[OpenCV]]></category>
		<category><![CDATA[toolchain]]></category>

		<guid isPermaLink="false">http://hackengineer.wordpress.com/?p=102</guid>
		<description><![CDATA[The easiest way I found to get openCV up and running on the beagleboard was to build an image from scatch using the narcissus tool.  It works and it works really well in just a few steps. Select Beagleboard Select the Packages you want OpenCV &#8211; Libraries will be readily available to include in your [...]]]></description>
				<content:encoded><![CDATA[<p>The easiest way I found to get openCV up and running on the beagleboard was to build an image from scatch using the <a href="http://narcissus.angstrom-distribution.org/">narcissus tool</a>.  It works and it works really well in just a few steps.</p>
<ol>
<li>Select Beagleboard</li>
<li>Select the Packages you want
<ul>
<li>OpenCV &#8211; Libraries will be readily available to include in your code</li>
<li>Toolchain &#8211; Ability to compile code natively on beagleboard</li>
</ul>
</li>
<li>Build ( this step takes a while so be patient)</li>
</ol>
<p>I find it useful to select the build type as an SD image under the advanced option.  Once downloaded you can download the image onto the sd card easily with <a href="https://launchpad.net/win32-image-writer">win32DiskImager</a>.  After the image has been loaded, plug the SD card into the beagle board and boot it up.  This step also takes a long while to install but once it boots your all set!<br />
<a href="http://hackengineer.files.wordpress.com/2012/02/screenshot-3.png"><img class="size-full wp-image-103 aligncenter" title="NarcissusTool" src="http://hackengineer.files.wordpress.com/2012/02/screenshot-3.png" alt="" width="614" height="552" /></a></p>
<p style="text-align: center;"><a title="Structured Light vs Microsoft Kinect" href="http://www.hackengineer.com/structured-light-vs-microsoft-kinect/">&lt;&#8211;Previous</a>     <a title="Kinect-Like 3D camera" href="http://www.hackengineer.com/3dcam">Project Home</a>     <a title="Installing your camera module and ENV.txt" href="http://www.hackengineer.com/installing-your-camera-module-and-env-txt/">Next &#8211;&gt;</a></p>
<p><script type="text/javascript"><!--
google_ad_client = "ca-pub-7101321993029582";
/* banner3DCam */
google_ad_slot = "3562568900";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script><br />
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.hackengineer.com/setting-up-your-beagleboard-with-angstrom-toolchain-and-opencv/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
