I began to use org-mode for planning out my tasks in GTD-style system. Putting every org files in a directory of a Dropbox folder, I run emacs to edit / manage these files from three different local machines: Cygwin, Mac OS X, and Debian. As I also use MobileOrg to access these org files from my iPad, a checksums.dat file must be kept up-to-date when any changes are made. This can be done by running md5sum *.org > checksums.dat.

The problem is that there are three different commands for md5sum command: md5sum.exe in Cygwin, md5 in Mac OS X, and md5sum in Debian. The most ideal situation is a makefile, stored in a Dropbox foder, detects which command is available in the current machine and runs that command to do the md5 checksum operation.

@Kevin That sounds like an overkill. I don't want to install a program in three distinct systems. In each system, a Dropbox folder is accessible as a local directory. When I run make checksum to generate checksum data under a Dropbox folder, I want the makefile uses an appropriate command after detecting which checksum command is available in the current machine.
–
Federico MagallanezJan 13 '12 at 4:44

If you are going to do a significant amount of scripting, and the project is small, I'd suggest scons.
–
Faheem MithaJan 13 '12 at 8:58

4 Answers
4

Possible commands to generate a checksum

Unfortunately, there's no standard utility to generate a cryptographic checksum. There is a standard utility to generate a CRC: cksum; this may be sufficient for your purpose of detecting changes in a non-hostile environment.

I would recommend using SHA1 rather than MD5. There aren't many systems that have an MD5 utility but no SHA1, and if you're going to use cryptographic checksums, you might as well use an algorithm with no known method to find collisions (assuming you also check the size).

One tool that's not standard but common and can calculate digests is OpenSSL. It's available for Cygwin, Debian and OSX, but unfortunately not part of the default installation on OSX.

openssl dgst -sha1

On OSX 10.6, there is a shasum utility, which is also present on Debian (it's part of the perl package) and I believe on Cygwin too. This is a Perl script. Most unix systems have Perl installed, so you could bundle that script alongside your makefile if you're worried about this script not being available everywhere.

Portable (POSIX) make

You can only run shell commands in rule, so each rule that computes a checksum has to contain the lookup code. You can put the snippet in a variable. Remember that separate lines in rules are evaluated independently. Also remember that $ signs that are to be passed to the shell need to be escaped to $$.

I had to use the -P option of the type command in order to get the "GNU make" method described above to work properly, so I ended up with sum := $(shell { type -P sha1sum || type -P sha1 || type -P shasum; } 2>/dev/null).
–
SeanMar 27 at 5:08

@Sean Thanks for the bug report. type -P only works if your shell is bash, not if it's e.g. ksh or dash (which is the case on Ubuntu among others). You can use command -v for portability.
–
GillesMar 27 at 8:32

It appears you've switched from using type to using command. Should the other methods be updated as well?
–
SeanMar 27 at 16:30

@Sean The other methods are fine: type is a correct, portable way to test the presence of a command, the problem with it is that it produces output like sha1sum is /usr/bin/sha1sum rather than just the program name. This was the only place where I used the output of type rather than just its return value.
–
GillesMar 27 at 17:12

which exit values are not portable. Use type instead (and you probably want to use if; then; elif to avoid having problems in there are multiple executables). which and type only look for executables anyway, so your test with -x is pointless.
–
Chris DownJan 13 '12 at 7:00

GNU autotools are the more traditional approach, but (and perhaps speaking only for myself) I don't think people are really too excited at the prospect of initially setting a project up using them. Well, it's a learning curve, in any case. CMake is the newer kid on the block, and perhaps less common (and still has a learning curve). It has its own syntax, and it generates makefiles for your platform. CMake does has the distinct advantage of being not so un*x-centric; it works reliably on unix, windows and mac as well (e.g., it can generate msvc projects, for instance, instead of makefiles.)

Edit: and of course since 'makefiles' were suggested, this answer goes along with that presumption. But perhaps just a python script or (gasp) a simple Java program would be more appropriate for this task; the scripting/programming language would do this the same across platforms.