#!/usr/bin/perl -w
# -*- C -*-

use strict;
use autodie;

my $settings = <<'END';

main_dia = 71.2 + 0.50;
top_thick = 4;

main_cnr = 4.0;

rivet_posn = 6.0;
rivet_thick = 1.67;
rivet_width = 4.15 + 1.0;
rivet_tall = 5.51 + 0.49;

over_rivet_gap = 0.5;

bayo_interf = 0.75;
bayo_behind = 3.5;

seal_thick = 0.1;
seal_recess = [ 2.0, 2.0 ]; // recess depth, recess height

side_taper = 1.0;

bayo_above_gap = 1.0;

// calculated

side_height = rivet_posn + bayo_behind;
side_thick = rivet_tall + over_rivet_gap;

top_z = top_thick + seal_thick;

bayo_entry = bayo_behind / 3.0;
bayo_top_z = bayo_behind + rivet_thick + over_rivet_gap;

$fs=0.5;
$fa=5;

// bayonet definition

bayo_polygon = [[ -bayo_entry,        -5             ],
		[ -bayo_entry,        0              ],
		[ 0,                  bayo_entry     ],
		[ bayo_entry,         bayo_entry     ]];

END

my $mainbody = <<'END';

module MainProfile(){
  main_cnr_pos = [ side_thick, top_z ] - [1,1]*main_cnr;
  translate(main_cnr_pos){
    intersection(){
      difference(){
	circle(r = main_cnr);
	circle(r = main_cnr * 0.5);
      }
      square([10,10]);
    }
  }
  polygon([[ -1,                    seal_thick        ],
	   [ -1,                    top_z             ],
	   [ main_cnr_pos[0],       top_z             ],
	   [ side_thick,            main_cnr_pos[1]   ],
	   [ side_thick,            -side_height      ],
	   [ side_taper,            -side_height      ],
	   [ 0,                     -rivet_posn       ],
	   [ 0,                     -(seal_recess[0] + seal_recess[1]) ],
	   [ seal_recess[0],        -(seal_recess[0]) ],
	   [ seal_recess[0],        seal_thick        ],
	   ]);
}

translate([-20,0]) MainProfile();

polygon(bayo_polygon);

END

print $settings, $mainbody or die $!;

