|
#!/bin/sh |
|
|
|
set -e |
|
|
|
OSM_SERVER=https://api.openstreetmap.org |
|
OSM_TEST_SERVER=https://master.apis.dev.openstreetmap.org |
|
|
|
# Commit to test server unless --serious is given |
|
if echo "$@" | grep -q '\--serious' ; then |
|
SERVER=$OSM_SERVER |
|
else |
|
SERVER=$OSM_TEST_SERVER |
|
fi |
|
|
|
OSM_API=${SERVER}/api/0.6 |
|
FILE="$(echo "$@" | sed s/--serious//)" |
|
|
|
# Fail if no OSC file is given |
|
if [ -z "$FILE" ]; then |
|
echo No OSC file is given >&2 |
|
exit 1 |
|
fi |
|
|
|
# Prompt for comment and User:Password |
|
if [ ! -t 0 ]; then |
|
comment=$(cat) |
|
else |
|
echo -n 'Type comment: ' |
|
read -r comment </dev/tty |
|
fi |
|
if [ -z "${OSM_USER_PASSWD}" ]; then |
|
echo -n 'Type USER:PASSWD: ' |
|
read -r OSM_USER_PASSWD </dev/tty |
|
fi |
|
|
|
# API call for changeset create |
|
# If env SOURCE is set, add it as part of changeset: |
|
# <tag k='source' v='$SOURCE'/> |
|
create_changeset() { |
|
SOURCE_TAG="${SOURCE:+$(printf "<tag k='source' v='%s'/>" $SOURCE)}" |
|
|
|
# Create changeset with given information |
|
response="$(curl ${OSM_API}/changeset/create \ |
|
--fail-with-body \ |
|
--user "$OSM_USER_PASSWD" \ |
|
--upload-file - \ |
|
--silent \ |
|
<<' EOF' |
|
<osm> |
|
<changeset> |
|
${SOURCE_TAG} |
|
<tag k='comment' v='${comment}'/> |
|
<tag k='created_by' v='bash script'/> |
|
<tag k='bot' v='yes'/> |
|
</changeset> |
|
</osm> |
|
EOF |
|
)" || local result=fail |
|
|
|
# If return code >200, or the response is not a sequence number |
|
# Exit with return code 1 |
|
if [ "$result" = fail ] || ! echo "$response" | grep -qE '^[[:digit:]]+$' ; then |
|
echo |
|
echo Fail to create a new changeset: >&2 |
|
echo "$response" >&2 |
|
return 1 |
|
else |
|
changeset_id=$response |
|
fi |
|
} |
|
|
|
# API call for uploading OSC file |
|
uploade_file_to_changeset() { |
|
response="$(curl -X POST $OSM_API/changeset/$1/upload \ |
|
--fail-with-body \ |
|
--user "$OSM_USER_PASSWD" \ |
|
--upload-file - \ |
|
--silent |
|
)" || local result=fail |
|
|
|
if [ "$result" = fail ]; then |
|
echo |
|
echo Fail to upload OSC file: >&2 |
|
echo "$response" >&2 |
|
return 1 |
|
fi |
|
} |
|
|
|
# API call for closing changeset |
|
close_changeset() { |
|
response="$(curl -X PUT ${OSM_API}/changeset/$1/close \ |
|
--fail-with-body \ |
|
--user "$OSM_USER_PASSWD" \ |
|
--silent |
|
)" || local result=fail |
|
|
|
if [ "$result" = fail ]; then |
|
echo |
|
echo Fail to close changeset: >&2 |
|
echo "$response" >&2 |
|
return 1 |
|
fi |
|
} |
|
|
|
# Create changeset when CHANGESET is not set |
|
if [ -z $CHANGESET ]; then |
|
echo 'CHANGESET is not set, create a new one' |
|
create_changeset && \ |
|
echo "Changeset created, check ${SERVER}/changeset/${changeset_id}" || \ |
|
exit 1 |
|
else |
|
echo "CHANGESET is set, use ${CHANGESET} as changeset ID" |
|
changeset_id=$CHANGESET |
|
fi |
|
|
|
# Upload OSC file to Changeset |
|
sed -Ee "/<(node|way|relation)/ s/>/ changeset=\"${changeset_id}\">/" $FILE |\ |
|
uploade_file_to_changeset ${changeset_id} && \ |
|
echo Upload file $FILE to changeset ${changeset_id} || \ |
|
exit 1 |
|
|
|
# Close Changeset |
|
close_changeset ${changeset_id} && \ |
|
echo Changeset ${changeset_id} closed || \ |
|
exit 1 |
Okay. Then never mind the 'checkbashisms' script I was going to tell you about. Also curl has some --fail options you might take advantage of instead of examining the code manually.
As to the the /dev/tty stuff and -t ... just more asking for trouble.
case ... [0123456789]*[0123456789] esac could even check for valid changeset numbers... I'll leave the challenge of how to even check for one digit changeset numbers .... Anyway you would have the valid matches separated by a pipe and then you fail if it doesn't match any of them. I'm sorry I'm writing this on a cell phone otherwise I'd go in to more detail. Anyway even a shell from the '70s could work with that. I'm just saying like you see in the other user's comment... You never know what kind of shell simebody might have... it might even be a zsh. So there's no point making the whole script break. It's (or was) just such a short script. It should be just like one of those Debian system scripts that don't take any chances with the kid stuff.