#!/bin/sh

global T := $1

for h in [\
	*.txt *.html \
	howto/*.txt howto/*.html \
	technical/*.txt technical/*.html \
	RelNotes/*.txt *.css]
{
	if test ! -f $h
	{
		: did not match
	} elif test -f "$T/$h" &&
		$DIFF -u -I'^Last updated ' "$T/$h" $h
	{
		:; # up to date
	} else {
		echo >&2 "# install $h $T/$h> !2 "# install $h $T/$h"
		rm -f "$T/$h"
		mkdir -p $[dirname "$T/$h]
		cp $h "$T/$h"
	}
}
global strip_leading := $[echo "$T/" | sed -e 's|.|.|g]
for th in [\
	"$T"/*.html "$T"/*.txt \
	"$T"/howto/*.txt "$T"/howto/*.html \
	"$T"/technical/*.txt "$T"/technical/*.html]
{
	global h := $[expr $th : "$strip_leading"'\(.*\)]
	matchstr $h {
	RelNotes-*.txt | index.html { continue }
	}
	test -f $h && continue
	echo >&2 "# rm -f $th> !2 "# rm -f $th"
	rm -f $th
}
ln -sf git.html "$T/index.html"
(CommandList
  children: [
    (Assignment
      keyword: Assign_None
      pairs: [(assign_pair lhs:(LhsName name:T) op:Equal rhs:{(DQ ($ VSub_Number "$1"))} spids:[4])]
      spids: [4]
    )
    (ForEach
      iter_name: h
      iter_words: [
        {(Lit_Other "*") (.txt)}
        {(Lit_Other "*") (.html)}
        {(howto/) (Lit_Other "*") (.txt)}
        {(howto/) (Lit_Other "*") (.html)}
        {(technical/) (Lit_Other "*") (.txt)}
        {(technical/) (Lit_Other "*") (.html)}
        {(RelNotes/) (Lit_Other "*") (.txt)}
        {(Lit_Other "*") (.css)}
      ]
      do_arg_iter: False
      body: 
        (DoGroup
          children: [
            (If
              arms: [
                (if_arm
                  cond: [(C {(test)} {(KW_Bang "!")} {(-f)} {(DQ ($ VSub_Name "$h"))})]
                  action: [(C {(Lit_Other ":")} {(did)} {(not)} {(match)})]
                  spids: [-1 69]
                )
                (if_arm
                  cond: [
                    (AndOr
                      children: [
                        (C {(test)} {(-f)} {(DQ ($ VSub_Name "$T") (/) ($ VSub_Name "$h"))})
                        (C {($ VSub_Name "$DIFF")} {(-u)} {(-I) (SQ <"^Last updated ">)} 
                          {(DQ ($ VSub_Name "$T") (/) ($ VSub_Name "$h"))} {(DQ ($ VSub_Name "$h"))}
                        )
                      ]
                      op_id: Op_DAmp
                    )
                  ]
                  action: [(Sentence child:(C {(Lit_Other ":")}) terminator:<Op_Semi ";">)]
                  spids: [81 116]
                )
              ]
              else_action: [
                (SimpleCommand
                  words: [
                    {(echo)}
                    {
                      (DQ ("# install ") ($ VSub_Name "$h") (" ") ($ VSub_Name "$T") (/) 
                        ($ VSub_Name "$h")
                      )
                    }
                  ]
                  redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[131])]
                )
                (C {(rm)} {(-f)} {(DQ ($ VSub_Name "$T") (/) ($ VSub_Name "$h"))})
                (C {(mkdir)} {(-p)} 
                  {
                    (CommandSubPart
                      command_list: 
                        (CommandList
                          children: [(C {(dirname)} {(DQ ($ VSub_Name "$T") (/) ($ VSub_Name "$h"))})]
                        )
                      left_token: <Left_CommandSub "$(">
                      spids: [159 167]
                    )
                  }
                )
                (C {(cp)} {(DQ ($ VSub_Name "$h"))} {(DQ ($ VSub_Name "$T") (/) ($ VSub_Name "$h"))})
              ]
              spids: [126 183]
            )
          ]
          spids: [53 185]
        )
      spids: [15 -1]
    )
    (Assignment
      keyword: Assign_None
      pairs: [
        (assign_pair
          lhs: (LhsName name:strip_leading)
          op: Equal
          rhs: 
            {
              (CommandSubPart
                command_list: 
                  (CommandList
                    children: [
                      (Pipeline
                        children: [
                          (C {(echo)} {(DQ ($ VSub_Name "$T") (/))})
                          (C {(sed)} {(-e)} {(SQ <"s|.|.|g">)})
                        ]
                        negated: False
                      )
                    ]
                  )
                left_token: <Left_CommandSub "$(">
                spids: [188 205]
              )
            }
          spids: [187]
        )
      ]
      spids: [187]
    )
    (ForEach
      iter_name: th
      iter_words: [
        {(DQ ($ VSub_Name "$T")) (/) (Lit_Other "*") (.html)}
        {(DQ ($ VSub_Name "$T")) (/) (Lit_Other "*") (.txt)}
        {(DQ ($ VSub_Name "$T")) (/howto/) (Lit_Other "*") (.txt)}
        {(DQ ($ VSub_Name "$T")) (/howto/) (Lit_Other "*") (.html)}
        {(DQ ($ VSub_Name "$T")) (/technical/) (Lit_Other "*") (.txt)}
        {(DQ ($ VSub_Name "$T")) (/technical/) (Lit_Other "*") (.html)}
      ]
      do_arg_iter: False
      body: 
        (DoGroup
          children: [
            (Assignment
              keyword: Assign_None
              pairs: [
                (assign_pair
                  lhs: (LhsName name:h)
                  op: Equal
                  rhs: 
                    {
                      (CommandSubPart
                        command_list: 
                          (CommandList
                            children: [
                              (C {(expr)} {(DQ ($ VSub_Name "$th"))} {(Lit_Other ":")} 
                                {(DQ ($ VSub_Name "$strip_leading")) (SQ <"\\(.*\\)">)}
                              )
                            ]
                          )
                        left_token: <Left_CommandSub "$(">
                        spids: [265 280]
                      )
                    }
                  spids: [264]
                )
              ]
              spids: [264]
            )
            (Case
              to_match: {(DQ ($ VSub_Name "$h"))}
              arms: [
                (case_arm
                  pat_list: [{(RelNotes-) (Lit_Other "*") (.txt)} {(index.html)}]
                  action: [(ControlFlow token:<ControlFlow_Continue continue>)]
                  spids: [292 299 303 -1]
                )
              ]
              spids: [283 289 306]
            )
            (AndOr
              children: [
                (C {(test)} {(-f)} {(DQ ($ VSub_Name "$h"))})
                (ControlFlow token:<ControlFlow_Continue continue>)
              ]
              op_id: Op_DAmp
            )
            (SimpleCommand
              words: [{(echo)} {(DQ ("# rm -f ") ($ VSub_Name "$th"))}]
              redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[324])]
            )
            (C {(rm)} {(-f)} {(DQ ($ VSub_Name "$th"))})
          ]
          spids: [261 341]
        )
      spids: [212 -1]
    )
    (C {(ln)} {(-sf)} {(git.html)} {(DQ ($ VSub_Name "$T") (/index.html))})
  ]
)