[tex-live] TeXLive (Windows) bash script -> exe

Norbert Preining preining at logic.at
Tue May 20 01:51:58 CEST 2014


Hi

On Mon, 19 May 2014, Josef Kleber wrote:
> i wrote a bash script osmimage

Besides all the other answers, ... here my comments:
* it would have been nice to actually attach the script, as it is
  not very long. I do it now. (attachment "osmimage")
* reading through it I guess it takes me with practically no
  knowledge of lua at most 1h to rewrite it in lua, if you ask
  one lua expert here niccely, they will come up with code in 10min.
  Actually, it took me *exactely* 1h ... of course, it does not
  do error handling on Windows, which is a pain ..
  attachment "osmimage.lua"

* it also need alt_getopt.lua which is in TeX Live. I am not sure
  how this can be automatically found, lua experts, please


Norbert

------------------------------------------------------------------------
PREINING, Norbert                               http://www.preining.info
JAIST, Japan                                 TeX Live & Debian Developer
GPG: 0x860CDC13   fp: F7D8 A928 26E3 16A1 9FA0  ACF0 6CAC A448 860C DC13
------------------------------------------------------------------------
-------------- next part --------------
#!/bin/bash
#
# osmimage [options]
#
# downloads an OpenStreetMap map specified by [options]
# by using http://developer.mapquest.com/ web service
#
# License: LPPL
#
pversion()
{
  echo "`basename $0` $VERSION"
  echo "(C) Josef Kleber   License: LPPL"
  exit 0;
}
#
function phelp()
{
  echo -e \
    "\n  `basename $0` [options]\n\n"\
    " downloads an OpenStreetMap map specified by [options]\n"\
    " by using http://developer.mapquest.com/ web service\n\n"\
    " Options:\n\n"\
    " -k  key registered at http://developer.mapquest.com/\n"\
    "     default (Example key from web site)!\n"\
    "     Please register and use your own key!\n\n"\
    " -l  specify a location\n"\
    "     e.g. 'Bergheimer Stra?e 110A, 69115 Heidelberg, Germany'\n\n"\
    " -x  specify a xsize (800)\n\n"\
    " -y  specify a ysize (400)\n\n"\
    " -S  short form to specify a size, e.g. 800,400\n\n"\
    " -s  specify a scale factor in the range 1692-221871572 (3385 = -z 17)\n"\
	"     see: http://open.mapquestapi.com/staticmap/zoomToScale.html\n\n"\
    " -z  specify a zoom in the range 1-18 (zoom overrides scale!)\n\n"\
    " -t  specify map type {map|sat|hyb} (map)\n\n"\
    " -i  specify image type {jpeg|jpg|gif|png} (png)\n\n"\
    " -c  specify icon color (yellow_1)\n"\
    "     see: http://open.mapquestapi.com/staticmap/icons.html\n\n"\
    " -n  specify the icon number (1)\n\n"\
    " -o  specify output basename without file extension (osmimage.IMAGETYPE)\n\n"\
    " -q  quiet; no output!\n\n"\
    " -v  prints version information\n\n"\
    " -h  prints help information\n\n"
  pversion
}
#
osmimage_error()
{
  local exitcode="$1"
  local errortext="$2"
  echo "Error ($exitcode): $errortext" >&2
  exit $exitcode
}
#
osmimage_warning()
{
  local warningtext="$1"
  echo -e "WARNING: $warningtext\n" >&2
}
#
check_prog()
{
  local prog="$1"
  hash $prog 2>/dev/null || osmimage_error 1 "$prog not installed! Aborting."
}
#
check_number()
{
  local var="$1"
  local varname="$2"
  local number='^[0-9]+$'
  if ! [[ $var =~ $number ]]
  then
     osmimage_error 2 "$varname can't be $var! Not a number!"
  fi
}
#
check_range()
{
  local var="$1"
  local min="$2"
  local max="$3"
  local exitcode="$4"
  local varname="$5"
  check_number $var $varname
  if [ $var -lt $min ]
  then
    osmimage_error $exitcode "$varname = $var; must be in the range of $min-$max"
  fi
  if [ $var -gt $max ]
  then
    osmimage_error $exitcode "$varname = $var; must be in the range of $min-$max"

  fi
}
#
check_prog wget
#
URL="http://open.mapquestapi.com/staticmap/v4/getplacemap"
KEY=""
LOCATION=""
XSIZE=""
YSIZE=""
SCALE=""
ZOOM=""
TYPE=""
IMAGETYPE=""
COLOR=""
NUMBER=""
OFILE="osmimage"
QUIET="false"
VERSION="v1.1 (19/05/2014)"
#
while getopts "k:l:x:y:S:s:z:t:i:c:n:o:qvh" flag
do
  case "$flag" in
    k) KEY="$OPTARG";;
    l) LOCATION="$OPTARG";;
    x) XSIZE="$OPTARG";;
    y) YSIZE="$OPTARG";;
    S) SIZE="$OPTARG";;
	s) SCALE="$OPTARG";;
    z) ZOOM="$OPTARG";;
    t) TYPE="$OPTARG";;
    i) IMAGETYPE="$OPTARG";;
    c) COLOR="$OPTARG";;
    n) NUMBER="$OPTARG";;
    o) OFILE="$OPTARG";;
    q) QUIET="true";;
    v) pversion;;
    h) phelp;;
  esac
done
#
if [ "$QUIET" = "true" ]
then
  exec 1>/dev/null
  exec 2>/dev/null
fi
#
if [ -z $KEY ]
then
  KEY="Kmjtd%7Cluu7n162n1%2C22%3Do5-h61wh"
  osmimage_warning "KEY not specified; using mapquest example key as default!"
fi
#
if [ -z $LOCATION ]
then
  LOCATION="Bergheimer Stra?e 110A, 69115 Heidelberg, Germany"
  osmimage_warning "LOCATION not specified; using Dante e.V. Office as default!"
fi
#
if [ -z $XSIZE ]
then
  XSIZE=800
  osmimage_warning "XSIZE not specified; using XSIZE=800 as default!"
fi
check_range $XSIZE 1 3840 11 XSIZE
#
if [ -z $YSIZE ]
then
  YSIZE=400
  osmimage_warning "YSIZE not specified; using YSIZE=400 as default!"
fi
check_range $YSIZE 1 3840 12 YSIZE
#
if [ -z $SIZE ]
then
  SIZE="$XSIZE,$YSIZE"
fi
#
if [ -z $SCALE ]
then
  if [ -z $ZOOM ]
  then
    SCALE=3385
    osmimage_warning "SCALE not specified, using SCALE=3385 as default!"
  fi
else
  check_range $SCALE 1692 221871572 13 SCALE
fi
#
if [ -z $TYPE ]
then
  TYPE="map"
  osmimage_warning "TYPE not specified; using map as default!"
fi
#
if [ -z $IMAGETYPE ]
then
  IMAGETYPE="png"
  osmimage_warning "IMAGETYPE not specified; using png as default!"
fi
#
if [ -z $COLOR ]
then
  COLOR="yellow_1"
  osmimage_warning "COLOR not specified; using yellow_1 as default!"
fi
if [ -z $NUMBER ]
then
  NUMBER=1
  osmimage_warning "NUMBER not specified; using 1 as default!"
fi
check_number $NUMBER NUMBER
#
ULOCATION="&location=$LOCATION"
USIZE="&size=$SIZE"
if [ -z $ZOOM ]
then
  USCALEZOOM="&scale=$SCALE"
else
  check_range $ZOOM 1 18 14 ZOOM
  USCALEZOOM="&zoom=$ZOOM"
fi
UTYPE="&type=$TYPE"
UIMAGETYPE="&imagetype=$IMAGETYPE"
USHOWICON="&showicon=${COLOR}-${NUMBER}"
UOFILE="${OFILE}.${IMAGETYPE}"
#
IMGURL="${URL}?key=${KEY}${ULOCATION}${USIZE}${USCALEZOOM}${UTYPE}${UIMAGETYPE}${USHOWICON}"
#
wget "$IMGURL" -O "$UOFILE"
exit 0
-------------- next part --------------
#!/usr/bin/env texlua
--
-- osmimage [options]
--
-- downloads an OpenStreetMap map specified by [options]
-- by using http://developer.mapquest.com/ web service
--
-- License: LPPL
--

local URL = "http://open.mapquestapi.com/staticmap/v4/getplacemap"
local KEY = ""
local LOCATION = ""
local XSIZE = ""
local YSIZE = ""
local SIZE = ""
local SCALE = ""
local ZOOM = ""
local TYPE = ""
local IMAGETYPE = ""
local COLOR = ""
local NUMBER = ""
local OFILE = "osmimage"
local QUIET = "false"
local VERSION = "v1.1 (19/05/2014)"

function pversion()
  print("osmimage " .. VERSION)
  print("(C) Josef Kleber   License: LPPL")
  os.exit(0)
end

function phelp()
  print([[
osmimage [options]

 downloads an OpenStreetMap map specified by [options]
 by using http://developer.mapquest.com/ web service

 Options:

 -k  key registered at http://developer.mapquest.com/
     default (Example key from web site)!
     Please register and use your own key!

 -l  specify a location
     e.g. 'Bergheimer Stra???e 110A, 69115 Heidelberg, Germany'

 -x  specify a xsize (800)

 -y  specify a ysize (400)

 -S  short form to specify a size, e.g. 800,400

 -s  specify a scale factor in the range 1692-221871572 (3385 = -z 17)
     see: http://open.mapquestapi.com/staticmap/zoomToScale.html

 -z  specify a zoom in the range 1-18 (zoom overrides scale!)

 -t  specify map type {map|sat|hyb} (map)

 -i  specify image type {jpeg|jpg|gif|png} (png)

 -c  specify icon color (yellow_1)
     see: http://open.mapquestapi.com/staticmap/icons.html

 -n  specify the icon number (1)

 -o  specify output basename without file extension (osmimage.IMAGETYPE)

 -q  quiet; no output!

 -v  prints version information

 -h  prints help information

]])
  pversion()
end

function osmimage_error(exitcode, errortext)
  io.stderr:write ("Error (" .. exitcode .. "): " .. errortext .. "\n")
  os.exit(exitcode)
end 

function osmimage_warning(warningtext)
  io.stderr:write("WARNING: " .. warningtext .. "\n")
end

-- checks if command exists on the path and returns its full path
-- copied from runscript.tlu
local function search_path(fname, PATH, PATHEXT)
  if string.find(fname, '[/\\]') then 
    return nil, "directory part not allowed for PATH search: "..fname 
  end
  PATH = PATH or os.getenv('PATH')
  PATHEXT = PATHEXT or '\0' -- '\0' for no extension
  for dir in string.gmatch(PATH, '[^;]+') do
    local dirsep = (string.find(dir, '\\') and '\\' or '/')
    for ext in string.gmatch(PATHEXT, '[^;]+') do
      local f = dir..dirsep..fname..ext
      if lfs.isfile(f) then return f, ext end
    end
  end
  return nil, "file or program not on PATH: "..fname
end

-- adapted from runscript.tlu
function check_prog(prog)
  local cmdext = prog..(string.find(prog, '%.[^\\/.]*$') and '' or '.exe')
  local fullcmd = search_path(cmdext)
  if fullcmd then 
    return fullcmd, cmd
  end
  osmimage_error(1, prog .. "not installed! Aborting.")
end

function check_number(var, varname)
  local number='^[0-9]+$'
  if not(string.match(var, '^[0-9]+$')) then
    osmimage_error(2, varname .. " can't be " .. var .. "! Not a number!")
  end
end

function check_range(var,min,max,exitcode,varname)
  check_number(var,varname)
  if (var < min or var > max) then
    osmimage_error(exitcode, varname .. " = " .. var .. "; must be in the range of " .. min .. "-" .. max)
  end
end

-- check_prog("wget")

require "alt_getopt"
local long_opts = {}
local ret
local optarg
local optind
opts,optind,optarg = alt_getopt.get_ordered_opts(arg, "k:l:x:y:S:s:z:t:i:c:n:o:qvh", long_opts)
for i,v in ipairs (opts) do
  if v == "k" then
    KEY = optarg[i]
  elseif v == "l" then
    LOCATION = optarg[i]
  elseif v == "x" then
    XSIZE = optarg[i]
  elseif v == "y" then
    YSIZE = optarg[i]
  elseif v == "S" then
    SIZE = optarg[i]
  elseif v == "s" then
    SCALE = optarg[i]
  elseif v == "z" then
    ZOOM = optarg[i]
  elseif v == "t" then
    TYPE = optarg[i]
  elseif v == "i" then
    IMAGETYPE = optarg[i]
  elseif v == "c" then
    COLOR = optarg[i]
  elseif v == "n" then
    NUMBER = optarg[i]
  elseif v == "o" then
    OFILE = optarg[i]
  elseif v == "q" then
    QUIET = 1
  elseif v == "v" then
    pversion()
  elseif v == "h" then
    phelp()
  end
end

--
-- TO BE DONE TODO TODO TODO
-- if [ "$QUIET" = "true" ]
-- then
--   exec 1>/dev/null
--   exec 2>/dev/null
-- fi

if KEY == "" then
  KEY="Kmjtd%7Cluu7n162n1%2C22%3Do5-h61wh"
  osmimage_warning("KEY not specified; using mapquest example key as default!")
end

if LOCATION == "" then
  LOCATION = "Bergheimer Stra???e 110A, 69115 Heidelberg, Germany"
  osmimage_warning("LOCATION not specified; using Dante e.V. Office as default!")
end

if XSIZE == "" then
  XSIZE=800
  osmimage_warning("XSIZE not specified; using XSIZE=800 as default!")
end

check_range(XSIZE,1,3840,11,"XSIZE")

if YSIZE == "" then
  YSIZE=400
  osmimage_warning("YSIZE not specified; using YSIZE=400 as default!")
end
check_range(YSIZE,1,3840,12,"YSIZE")

if SIZE == "" then
  SIZE = XSIZE .. "," .. YSIZE
end

if SCALE == "" then
  if ZOOM == "" then
    SCALE=3385
    osmimage_warning("SCALE not specified, using SCALE=3385 as default!")
  end
else
  check_range(SCALE,1692,221871572,13,"SCALE")
end

if TYPE == "" then
  TYPE = "map"
  osmimage_warning("TYPE not specified; using map as default!")
end

if IMAGETYPE == "" then
  IMAGETYPE="png"
  osmimage_warning("IMAGETYPE not specified; using png as default!")
end

if COLOR == "" then
  COLOR="yellow_1"
  osmimage_warning("COLOR not specified; using yellow_1 as default!")
end

if NUMBER == "" then
  NUMBER=1
  osmimage_warning("NUMBER not specified; using 1 as default!")
end
check_number(NUMBER,"NUMBER")

ULOCATION = "&location=" .. LOCATION
USIZE = "&size=" .. SIZE
if ZOOM == "" then
  USCALEZOOM = "&scale=" .. SCALE
else
  check_range(ZOOM,1,18,14,"ZOOM")
  USCALEZOOM = "&zoom=" .. ZOOM
end
UTYPE = "&type=" .. TYPE
UIMAGETYPE = "&imagetype=" .. IMAGETYPE
USHOWICON = "&showicon=" .. COLOR .. "-" .. NUMBER
UOFILE = OFILE .. "." .. IMAGETYPE

IMGURL = URL .. "?key=" .. KEY .. ULOCATION .. USIZE .. USCALEZOOM .. UTYPE .. UIMAGETYPE .. USHOWICON

PROGLINE = "wget '" .. IMGURL .. "' -O " .. UOFILE

print(PROGLINE)
os.spawn(PROGLINE)



More information about the tex-live mailing list