{% extends "base.html" %}
{% block title %}Cave re-labelling from unofficial identifier to official Kataster number
<!-- cave_kataster.html - this text visible because this template has been included -->
{% endblock %}

{% block content %}



<h2>Cave re-labelling from unofficial identifier to official Kataster number</h2>
<h3>Cave <a href="/{{cave.url|safe}}">{{cave.slug|safe}}</a> '{{cave.official_name|safe}}'</h3>


 <form action="{% url 'kataster' cave.slug %}" method="post">
<p>
{% if cave.kataster_number %}
<b>This cave has already been "katastered". area:{{cave.areacode |safe}}  number:{{cave.kataster_number |safe}} 
</b>
{% else %}
This cave needs to be "katastered". If you have the new number issued by the Austrians, we can do it now.

{% csrf_token %}
{{form.as_p}}
	<button  class="fancybutton"  style="padding: 0.5em 25px; font-size: 100%;" type = "submit" >
		See what the edits look like &rarr;
	</button>	

{% endif %}
<div style="color:red; font-weight: bold; font-size: large">
{{warning|safe}}<p>
{{error|safe}}</div>

<h3>Rename the .html files in <var>expoweb</var></h3>
<div style="font-family: monospace; font-weight: bold;">
{{cave_data|safe}}<br />
&nbsp;&nbsp;&nbsp;<span style="color:grey">&lt;kataster_number&gt;</span><span style="color:grey">&lt;/kataster_number&gt;</span> 
<br />
&nbsp;&nbsp;&nbsp;
&rarr;
<span style="color:grey">&lt;kataster_number&gt;</span><span style="color:blue">{{knum}}</span><span style="color:grey">&lt;/kataster_number&gt;</span> 

<br />
&nbsp;&nbsp;&nbsp;<span style="color:grey">&lt;survex_file&gt;</span>{{cave.survex_file}}
<span style="color:grey">&lt;/survex_file&gt;</span> 
<br />
&nbsp;&nbsp;&nbsp;
&rarr;
<span style="color:grey">&lt;survex_file&gt;</span><span style="color:blue">caves-{{cave.areacode}}/{{knum}}/{{knum}}.svx</span>
<span style="color:grey">&lt;/survex_file&gt;</span> 


<p>
{% for e in entrance_data %}
{{e|safe}}</br />
{% endfor %}
</div>

<ul style="list-style: disc">

<li> Edit the '<samp>kataster'number</samp>' field inside the <samp>cave_data</samp> file.
<li> Edit the '<samp>survex_file</samp>' field inside the <samp>cave_data</samp> file.
<li> Edit the '<samp>entranceslug</samp>' field inside each '<samp>entrance</samp>' field in the <samp>cave_data</samp> file.
<li> Rename all the files listed above - always do this <b>last</b> otherwise it is hard to automatically discover which edits have not been done.
</ul>

<h3>Rename the cave description directory in <var>expoweb</var></h3>
<p>
<ul style="list-style: disc">
<li> Edit all the '<samp>href=</samp>' and '<samp>src=</samp>' <small>URLS</small> (if they exist) inside all the <samp>cave_data</samp> and <samp>entrance_data</samp> files descriptive text to refer to the new directory
<li> Rename the directory (if it exists) inside the areacode directory, e.g. rename <samp>/{{cave.areacode}}/{{cave.unofficial_number}}/</samp> as <samp>/{{cave.areacode}}/<span style="color:blue">{{knum}}</span>/</samp> (if <span style="color:blue">{{knum}}</span> is the correct new kataster number). Do this last.
</ul>

<p><b><samp>
{% if misplaced %}
<samp><b>1623/l/ :</samp></b><br />
{% endif %}
{{misplaced|safe}}
</samp></b><br />

<p>

<h3>Rename the survex directory in <var>loser</var></h3>
<div style="font-family: monospace; font-weight: bold;">
{{loser_name|safe}}/
<p>
{% for f in loser_data %}
&nbsp;&nbsp;&nbsp;{{f|safe}}</br />
{% endfor %}
</div>
<ul style="list-style: disc">
<li>Beware that the capitalisation of the directory name in the <var>loser</var> repo may be different from the cave name in the <var>expoweb</var> repo.
<li> Find the survex files for this cave and edit the <samp>*include</samp> inside the survex files to use the new kataster number
<li> find and edit the <samp>'*_station'</samp> tags in each <samp>entrance_data</samp> file and in the fixed points files in the loser repo.
<li> Rename the survex files
<li> Run <samp>'cavern caves-{{cave.areacode |safe}}/<span style="color:blue">{{knum}}/{{knum}}.svx</span>'</samp> and check it all works as you hope
<li> Run <samp>'cavern caves-{{cave.areacode |safe}}/caves.svx' </samp>  and check it all works as you hope
</ul>
Here are all the survex files which are involved with this cave, the first one on this list should be the same as the one shown above. Any others will need fixing manually:<br />
<samp>{% for svx in cave.survexfile_set.all %}
{{svx}}.svx<br />
{%endfor %}
</samp>


<h3>Sort out the *fix point(s) in both <var>loser</var> and in <var>expoweb</var></h3>
<ul style="list-style: disc">
<li> Find the survex station point for each entrance (one or more in each <samp>entrance_data</samp> file) in <var>expoweb</var>. There are two fields on an Entrance where there might be a location: <samp>'tag_station'</samp>, or <samp>'other_station'</samp>. These refer to a survex survey point, which might be *fix elsewhere or one of the survey points in one of the cave survex files.
<li> Change the name of each survex point in the relevant survex file in  <var>loser</var> (usually done by renaming the <samp>*begin/*end</samp> block
and patching any <samp>*equate</samp> statements)
<li> Make the same name change(s) in each entrance (i.e. in each <samp>entrance_data</samp> file) in <var>expoweb</var>
</ul>
<p>Without fully parsing the entire survex *include tree (which this code does not do), it is impossible to find the *fix declaration file from the <samp>'station'</samp> fields in the entrance_data files. However we conventionally store the *fix in a "known" place, so we try.
<p>Edit these Entrance files:<br />
<p>
{% for e in entrances %}
<b><samp>
entrance_data/{{e}}.html{% if e.tag_station%}</samp><br />&nbsp;&nbsp;&nbsp;</b>tag_station:<b> <samp>{{e.tag_station}}{% endif %}
{% if e.other_station%}</samp><br />&nbsp;&nbsp;&nbsp;</b>other_station:<b> <samp>{{e.other_station}}{% endif %}<br /><samp></b>
{% endfor %}


<h3>Check the wallet links to survex files in <var>drawings</var></h3>
<p>
Edit these Wallet files <br><b><samp>
<ul style="list-style: disc">
{% for w in cave.wallets.all %}
<li><a href="{{w.get_url}}">{{w.walletname}}</a> : </b>survex files<b> {{w.survexfiles}}
{%endfor %}
</ul>
</samp></b>

and if any of them are any of these survex files:<br />
<samp>
{% for svx in cave.survexfile_set.all %}
{{svx}}.svx<br />
{%endfor %}
</samp>
then the wallet needs editing: do this online  (click on the wallet id link above) <em>before</em> you run any of the katastering script. Doing it online will also do all the git commit stuff for you.

<p>If doing this manually, it is <b>much</b> easier to do this using the online wallet edit form than to mess with the raw format of the JSON files, which we never touch and so will be unfamiliar to everyone.
<!--
<div style="margin-left: 5em;">Explanation: wallets store their data in JSON files in <samp><var>drawings</var>/walletsjson/&lt;year&gt;/&lt;walletid&gt;/contents.json</samp>, like this for wallet 2024#15:<code><pre>"survex file": [
  "caves-1623/2015-dl-01/2015-dl-01",
  "caves-1623/2024-jc-01/2024-jc-01"
 ],
</pre></code>
</div>
-->
<h3>Set the historic alias forwarder  in <var>expoweb</var></h3>
<ul style="list-style: disc">
<li> Add the line <br />
<b><samp>("{{cave.slug|safe}}", "<span style="color:blue">{{cave.areacode |safe}}-{{knum}}</span>"),</samp></b></br />
 to the end 
of the file <var><a href="/cave_data/cavealiases.txt_edit">cave_data/cavealiases.txt</a></var> (don't forget the  comma at the end of the line)
</ul>
<p>This will cause lots of errors until the cave <samp>"<span style="color:blue">{{cave.areacode |safe}}-{{knum}}</span>"</samp> has been created by a full database reset OR we can do an online rename of the "Cave" object in the database [code yet to be written] by pressing this button [button not yet implemented].
<!--or by editing & saving the cave description page <a href="/{{cave.areacode |safe}}/{{knum}}">/{{cave.areacode |safe}}/{{knum}}</a> after renaming. (Editing a cave description page regenerates some of the internal indices.)
-->

<h3>Finally</h3>
<ul style="list-style: disc">
<li><samp>cd loser<br />
git add *<br />
git commit -m 'Katastering  {{cave.slug|safe}} to <span style="color:blue">{{cave.areacode |safe}}-{{knum}}</span>'<br />
git pull<br />
git push<br />
cd ../expoweb<br />
git add *<br />
git commit -m 'Katastering  {{cave.slug|safe}} to <span style="color:blue">{{cave.areacode |safe}}-{{knum}}</span>'<br />
git pull<br />
git push<br />
</samp>
<li>It is then vital to do a complete databaseReset as troggle has internally indexed all those {{cave.slug}} files, the indexes are now out of date and horrible things will happen when people try to use troggle with any cave that has been altered:<br />
<samp>uv run databaseReset reset <span style="color:blue">K{{knum}}</span></samp><br />
OR we can do an online rename of the "Cave" object in the database [code yet to be written] by pressing the button above [button not yet implemented].
<li>Look in the reset import warnings and errors report to see if anything has gone wrong:
<var><a href="http://expo.survex.com/dataissues">expo.survex.com/dataissues</a></var><br />
(in fact, look at this before you do any of this, so you can see what chnages.)
</ul>

<h3>Ideal scenario</h3>
<p>
<p>Ideally you do this renumbering on a standalone instance of troggle running on a <var> &#9874; <a href="/handbook/troggle/troglaptop.html">troggle development laptop</a></var> and you don't push any of the git commits to the server until you have got all the niggles out of the conversion, i.e. the databaseReset runs locally without any warnings or errors introduced by your renaming <em>and</em> 'cavern' runs without any new survex errors.

<h3>Copy this script and edit it to match the directory names on your PC</h3>
<textarea id="script" name="script" rows="{{rows}}" cols="110" style="font-size:small; font-weight: bold; background-color: LemonChiffon">
{{script}}
</textarea>

<br />
<!-- duplicate button at bottom of long page -->
	<button  class="fancybutton"  style="padding: 0.5em 25px; font-size: 100%;" type = "submit" >
		See what the edits look like &rarr;
	</button>	
</form>
<div style="color:red; font-weight: bold; font-size: large">
{{warning|safe}}<p>
{{error|safe}}</div>
{% endblock %}
