|
#!/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 |
I think you're going in the wrong direction. I think you should just make sure the darn thing is compatible with the Bourne shell, And not go off the deep end with the BASH stuff. It's not that hard to just write it to a file and then you'll won't mess up the error code detection. Then you can always read from the file later on.
Maybe even ID=$(curl ....) would be OK.