catkin

prepare_ldif_for_gnokii.awk

Jun 26th, 2011
176
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/awk -f
  2.  
  3. # Prepares a .ldif file in preparation for gnokii to load to a Nokia Series 40 phone
  4.  
  5. # Change history
  6. # * Version 0.0 17may11 Charles
  7. #   First published version, developed and tested using Thunderbird export
  8. # * Version 0.1 23may11 Charles
  9. #   Changed tabs to spaces
  10. # * Version 0.2 23jun11 Charles
  11. #   Fixed base64 wrapping lines by adding --wrap=0 option
  12. #   Added ^mobile: to telephone numbers to have non-numerics stripped
  13.  
  14. BEGIN {
  15.     FS = ":"
  16.     max_description_len = 116 # gnokii limit at 12may11
  17. }
  18.  
  19. # Note cn for use in any log message
  20. /^cn:/ {
  21.     if ( $0 ~ /^cn::/ )
  22.     {
  23.         "echo " substr( $0, 6 ) " | base64 --decode" | getline cn
  24.     }
  25.     else
  26.     {
  27.         cn = substr( $0, 5 )
  28.     }
  29. }
  30.  
  31. # Remove non-numerics from phone numbers, keeping any +
  32. /^facsimiletelephonenumber: / || /^homePhone: / || /^mobile: / || /^telephoneNumber: / {
  33.     $2 = substr( $2, 2 ) # Remove leading space
  34.     if ( substr( $2, 1, 1 ) == "+" )
  35.     {
  36.         plus = "+"
  37.         $2 = substr( $2, 2 )
  38.     }
  39.     else plus = ""
  40.     gsub( /[^0-9]/, "", $2 )
  41.     print $1 ": " plus $2
  42.     next
  43. }
  44.  
  45. # Truncate any long "description"s
  46. /^description:/ {
  47.     base64 = 0
  48.     truncated = 0
  49.     if ( $0 ~ /^description::/ )
  50.     {
  51.         #print "Found base-64 description. $0: " $0 > "/dev/stderr"
  52.         base64 = 1
  53.         "echo " substr( $0, 14 ) " | base64 --decode" | getline value
  54.         if ( value ~ /^base64: invalid input/ )
  55.         {
  56.             print "WARNING: " cn ": description/note is invalid base-64; deleted (input line " NR ")" > "/dev/stderr"
  57.             next
  58.         }
  59.     }
  60.     else value = $2
  61.     if ( length( value ) > max_description_len )
  62.     {
  63.         truncated = 1
  64.         print "WARNING: " cn ": description/note longer than " max_description_len "; truncated (input line " NR ")" > "/dev/stderr"
  65.         value = substr( value, 1, max_description_len )
  66.     }
  67.     if ( base64 )
  68.     {
  69.         gsub( "'", "'\"'\"'", value )
  70.         "echo '" value "' | base64 --wrap=0" | getline value
  71.         print "description:: " value
  72.     }
  73.     else
  74.     {
  75.         print "description: " value
  76.     }
  77.     next
  78. }
  79.  
  80. { print }
RAW Paste Data