#!/usr/bin/awk -f

# Convert X Pixmaps (XPM) into Pascal-code (new awk)
# (works for GNU-Pascal and FreePascal, maybe not for others)
#
# Example-Usage:
# ./xpm2pas image1.xpm image2.xpm > images.inc
#
# Copyright (c) 2009 Andreas K. Foerster
# Copying and distribution of this file, with or without modification,
# are permitted in any medium without royalty provided the copyright
# notice and this notice are preserved.  This file is offered as-is,
# without any warranty.

/\/\* XPM \*\// { print "{ converted form XPM with xpm2pas }"; next }

# preserve other comments
/\/\*/, /\*\// { gsub (/\/\*/, "{"); gsub (/\*\//, "}"); print }

/^static char \* ?.*\[\] = \{/ {
  picname = ($4 == "=") ? $3 : $4
  sub(/\[\]/, "", picname)
  sub(/\*/, "", picname)
  sub(/_xpm/, "", picname)
  zl = 0
  }

zl == 0 && /"[0-9]+ [0-9]+ [0-9]+ [0-9]+/ {
  print ""
  print "const " picname " : array[0.." ($2 + $3) "] of PChar = ("
  # one less for the 0, but then again one more for the value-line
  }

/".*"/ {
  zl++
  gsub (/'/, "\\")  # \ is no problem in Pascal, only in C
  gsub (/"/, "'")
  sub (/'};/, "');\n")
  print
  }

/^\};$/ { print ");\n" }
