utilities: FreeBSD compatibility.
[sliver-openvswitch.git] / utilities / ovs-pki.in
index 0255251..2dc4060 100755 (executable)
@@ -1,6 +1,6 @@
 #! /bin/sh
 
-# Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks, Inc.
+# Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
 #
 # Licensed under the Apache License, Version 2.0 (the "License");
 # you may not use this file except in compliance with the License.
@@ -24,6 +24,38 @@ batch=no
 log='@LOGDIR@/ovs-pki.log'
 keytype=rsa
 bits=2048
+
+# OS-specific compatibility routines
+case $(uname -s) in
+FreeBSD)
+    file_mod_epoch()
+    {
+        stat -r "$1" | awk '{print $10}'
+    }
+
+    file_mod_date()
+    {
+        stat -f '%Sm' "$1"
+    }
+
+    sha1sum()
+    {
+        sha1 "$@"
+    }
+    ;;
+*)
+    file_mod_epoch()
+    {
+        date -r "$1" +%s
+    }
+
+    file_mod_date()
+    {
+        date -r "$1"
+    }
+    ;;
+esac
+
 for option; do
     # This option-parsing mechanism borrowed from a Autoconf-generated
     # configure script under the following license:
@@ -93,9 +125,14 @@ Options that apply to any command:
   -f, --force          Continue even if file or directory already exists
   -l, --log=FILE       Log openssl output to FILE (default: ovs-log.log)
   -h, --help           Print this usage message.
+  -V, --version        Display version information.
 EOF
             exit 0
             ;;
+        -V|--version)
+            echo "ovs-pki (Open vSwitch) @VERSION@"
+            exit 0
+            ;;
         --di*=*)
             pkidir=$optarg
             ;;
@@ -173,7 +210,7 @@ if test -z "$dsaparam"; then
 fi
 case $log in
     /*) ;;
-    *) log="$PWD/$log" ;;
+    *) log=`pwd`/$log ;;
 esac
 
 logdir=$(dirname "$log")
@@ -208,7 +245,7 @@ if test "$command" = "init"; then
     # Create the CAs.
     for ca in controllerca switchca; do
         echo "Creating $ca..." >&2
-        oldpwd=$PWD
+        oldpwd=`pwd`
         mkdir -p $ca
         cd $ca
 
@@ -360,7 +397,7 @@ make_tmpdir() {
 fingerprint() {
     file=$1
     name=${1-$2}
-    date=$(date -r $file)
+    date=$(file_mod_date "$file")
     if grep -e '-BEGIN CERTIFICATE-' "$file" > /dev/null; then
         fingerprint=$(openssl x509 -noout -in "$file" -fingerprint |
                       sed 's/SHA1 Fingerprint=//' | tr -d ':')
@@ -604,7 +641,7 @@ elif test "$command" = expire; then
     for type in switch controller; do
         cd "$pkidir/${type}ca/incoming" || exit 1
         for file in $(glob "*"); do
-            time=$(date -r "$file" +%s)
+            time=$(file_mod_epoch "$file")
             if test "$time" -lt "$cutoff"; then
                 rm -f "$file"
             fi