+# parse command line -----------------------------------------------
+
+$finderinfo = 0; # 0: unknown 1: file 2: directory
+while ($arg = shift @ARGV)
+{
+ if ($arg =~ /^(-h|-help|--help)$/ ) {
+ printf ("usage: %s [-a] FILE|DIR\n" ,basename($0));
+ printf (" or: %s -f FILE\n" ,basename($0));
+ printf (" or: %s -d FILE\n" ,basename($0));
+ printf (" or: %s -h|-help|--help\n" ,basename($0));
+ printf ("Dump AppleSingle/AppleDouble format file.\n");
+ printf ("\n");
+ printf (" -a (default) Dump a AppleSingle/AppleDouble file for FILE or DIR\n");
+ printf (" automatically.\n");
+ printf (" Extrapolate FinderInfo type from absolute path.\n");
+ printf (" If FILE is not AppleSingle/AppleDouble format,\n");
+ printf (" look for '.AppleDouble/FILE' and '._FILE'.\n");
+ printf (" If DIR, look for 'DIR/.AppleDouble/.Parent' and '._DIR'.\n");
+ printf (" -f Dump FILE. Assume FinderInfo to be FileInfo.\n");
+ printf (" -d Dump FILE. Assume FinderInfo to be DirInfo.\n");
+ printf (" -h,-help,--help Display this help and exit\n");
+ printf ("\n");
+ printf ("There is no way to detect whether FinderInfo is FileInfo or DirInfo.\n");
+ printf ("By default, %s examins whether file or directory,\n" ,basename($0));
+ printf ("a parent directory is .AppleDouble, filename is ._*, filename is .Parent,\n");
+ printf ("and so on.\n");
+ printf ("If setting option -f or -d, %s assume FinderInfo and doesn't look for\n");
+ printf ("another file.\n");
+ exit 0;
+ } elsif ($arg eq "-a") {
+ $finderinfo = 0;
+ } elsif ($arg eq "-f") {
+ $finderinfo = 1;
+ } elsif ($arg eq "-d") {
+ $finderinfo = 2;
+ } elsif ($arg =~ /^-/) {
+ printf (STDERR "%s: invalid option %s\n", basename($0), $arg);
+ printf (STDERR "Try \`%s\ -h' for more information.\n", basename($0));
+ exit 1;
+ } else {
+ $afile = $arg;
+ }
+}
+
+if (!($afile)) {
+ printf (STDERR "missing file operand.\n");
+ exit 1;
+}
+if (!( -e $afile)) {
+ printf (STDERR "\"%s\" not found.\n", $afile);
+ exit 1;
+}
+
+# detect FinderInfo, and search AppleSingle/AppleDouble file --------------
+
+$abspath = File::Spec->rel2abs($afile);
+($basename, $path, $ext) = fileparse($abspath);
+
+if ( $finderinfo != 0 ) {
+ ;
+} elsif ( -f $afile ) {
+ if ( $basename eq ".Parent") {
+ $finderinfo = 2;
+ } elsif ( $path =~ /\/.AppleDouble\/$/ ) {
+ $finderinfo =1;
+ } elsif ( $basename =~ /^._/ ) {
+ if ( -f $path.substr($basename, 2) ) {
+ $finderinfo =1;
+ } elsif ( -d $path.substr($basename, 2) ) {
+ $finderinfo =2;
+ }
+ }
+ if (!open(INFILE, "<$afile")) {
+ printf (STDERR "cannot open %s\n", $afile);
+ exit 1;
+ }
+ read(INFILE,$buf,4);
+ $val = unpack("N", $buf );
+ close(INFILE);
+ if ($val == 0x00051600 || $val == 0x00051607) {
+ ;
+ } else {
+ printf ("\"%s\" is not AppleSingle/AppleDouble format.\n", $afile);
+ $finderinfo = 1;
+ $netatalkfile = $path.".AppleDouble/".$basename;
+ $osxfile = $path."._".$basename;
+ if ( -e $netatalkfile ) {
+ $afile = $netatalkfile;
+ } elsif ( -e $osxfile ) {
+ printf ("\"%s\" not found.\n", $netatalkfile);
+ $afile = $osxfile;
+ } else {
+ printf ("\"%s\" not found.\n", $netatalkfile);
+ printf ("\"%s\" not found.\n", $osxfile);
+ exit 1;
+ }
+ }
+} elsif ( -d $afile) {
+ printf ("\"%s\" is directory.\n", $afile);
+ $finderinfo =2;
+ $netatalkfile = $path.$basename."/.AppleDouble/.Parent";
+ $osxfile = $path."._".$basename;
+ if ( -e $netatalkfile ) {
+ $afile = $netatalkfile;
+ } elsif ( -e $osxfile ) {
+ printf ("\"%s\" not found.\n", $netatalkfile);
+ $afile = $osxfile;
+ } else {
+ printf ("\"%s\" not found.\n", $netatalkfile);
+ printf ("\"%s\" not found.\n", $osxfile);
+ exit 1;
+ }
+} else {
+ printf (STDERR "unknown error: %s\n", $afile);
+ exit 1;
+}