#!/bin/bash

export RESTAPI="http://localhost:8080/geoserver/rest"
export LOGIN="admin26:gm2711" # Change YOUR_OWN_PASSWORD_VALUE to your own password value. #
export WORKSPACE="osm_tw_11d"
export STORE="OpenStreetMap_11d"
export SLD_DIR="$HOME/my_labs/lab_11d/osm_tw_11d_sld"

export ALL_SLD_FILES="$(find $HOME/my_labs/lab_11d/osm_tw_11d_sld -maxdepth 1 -type f -name '*.sld' -printf '%f\n')"

for file in $ALL_SLD_FILES
do
export SLD_FILE_WITH_EXT="$file"
export LAYER="${SLD_FILE_WITH_EXT%.*}"
export LOGFILE="$HOME/my_labs/logs/$LAYER.log"
export STYLE_NAME_11D="11d_$LAYER"

echo "Processing layer $LAYER ..."

printf "\n======================================\n=== " > $LOGFILE
echo $LAYER >> $LOGFILE
printf "=== " >> $LOGFILE
date +"%Y-%m-%d %H:%M:%S %Z" >> $LOGFILE
printf "======================================\n" >> $LOGFILE


# Long options (style: building)
printf "\n\n--------------\n--- curl 1 ---\n--------------\n" >> $LOGFILE
curl --verbose --user $LOGIN --header "Content-type: text/xml" --data "<featureType><name>$LAYER</name></featureType>" $RESTAPI/workspaces/$WORKSPACE/datastores/$STORE/featuretypes?recalculate=nativebbox,latlonbbox &>> $LOGFILE

printf "\n\n--------------\n--- curl 2 ---\n--------------\n" >> $LOGFILE
curl --verbose --user $LOGIN --header "Content-type: text/xml" --data "<style><name>$STYLE_NAME_11D</name><filename>$SLD_FILE_WITH_EXT</filename></style>" $RESTAPI/workspaces/$WORKSPACE/styles &>> $LOGFILE

printf "\n\n--------------\n--- curl 3 ---\n--------------\n" >> $LOGFILE
curl --verbose --user $LOGIN -XPUT --header "Content-type: application/vnd.ogc.sld+xml" --data @$SLD_DIR/$SLD_FILE_WITH_EXT $RESTAPI/workspaces/$WORKSPACE/styles/$STYLE_NAME_11D &>> $LOGFILE

printf "\n\n--------------\n--- curl 4 ---\n--------------\n" >> $LOGFILE
curl --verbose --user $LOGIN -XPUT --header "Content-type: text/xml" --data "<layer><enabled>true</enabled><defaultStyle><name>$STYLE_NAME_11D</name><workspace>$WORKSPACE</workspace></defaultStyle></layer>" $RESTAPI/layers/$WORKSPACE:$LAYER &>> $LOGFILE

printf "\n\n" >> $LOGFILE
done

echo "Completed"

