#! @@AWK@@ -f
BEGIN { 

ID    =    "dclpsrot 1.1"
DATE  =                               "1995/03/19(numaguti)\n \
              2008/04/25(kagimoto)"

EX    =    "rotate dcl.ps file"

USAGE =  "% dclpsrot <file>"

#----------------------------------------------------------------------------
# NO OPTION

    if (ARGV[1] == "-help" || ARGV[1] == "help") { 
        print "  " ID,DATE
        print " --- " EX
        print " usage : " USAGE
        exit
    }
}

/^%%Orientation: Landscape/ {
    print "%%Orientation: Portrait"
    next
}

/^%%Orientation: Portrait/ {
    print "%%Orientation: Landscape"
    next
}

/^%%BoundingBox/ {
    print "%%BoundingBox: -1 -1 751 531"
    next
}

/concat$/ {
    print "[ 0.1 0 0 0.1 0 0 ] concat"
    next
}
/translate/ {
    print "% " $0
    next
}
/rotate/ {
    rotate_anticlockwise()
    next
}
/^%%Begin SZTX / {
    $4 += 90
    print
    next
}
/^\/P1/	{
    $1=""; print "/P3" $0
    next
}
/^\/P2/	{
    $1=""; print "/P4" $0
    next
}
/^\/P3/	{
    $1=""; print "/P1" $0
    next
}
/^\/P4/	{
    $1=""; print "/P2" $0
    next
}

{
    print
}

function rotate_anticlockwise () {
  statement = ""
  for (i = 1; i <= NF; i++) {
    statement = statement " " $i
    if ($i == "rotate") {
      angle = $++i
      statement = statement " " angle+90
    }
  }
  printf("%s\n", statement)
}
