#
# cgi functions
#
cgi_form() {
  # $1=stage
  # $METHOD := method, defaults to "POST"
  : ${myname:?'Sure to set $myname to this script name'}
  cont=`cat`
  cat<<EOF
<form action="$myname" method="${METHOD:-POST}" enctype="multipart/form-data">
$cont
<input type="hidden" name="stage" value="$1">
<input type="submit" value="送信" title="COMMIT">
<input type="reset" value="リセット" title="Reset">
</form>
EOF
}
cgi_submit() {
  cat<<EOF
<input type="submit" value="$1">
EOF
}
cgi_reset() {
  cat<<EOF
<input type="reset" value="$1">
EOF
}
cgi_radio() {
  echo "<input type=\"radio\" name=\"$1\" ${2:+value=\"$2\"} $3>"
}
cgi_checkbox() {
  echo "<input type=\"checkbox\" name=\"$1\" ${2:+value=\"$2\"} $3>"
}
cgi_hidden() {
  echo "<input type=\"hidden\" name=\"$1\" value=\"$2\" $3>"
}
cgi_passwd() {
  cat<<EOF
<table class="pswd">
 <tr><td>現パスワード</td><td><input type="password" name="pswd"></td></tr>
 <tr><td>新パスワード</td><td><input type="password" name="pswd1"></td></tr>
 <tr><td>新パスワード(確認)</td><td><input type="password" name="pswd2"></td></tr>
</table>
EOF
}
cgi_text() {
  _v=`echo "$2"|htmlescape`
  echo "<input type=\"text\" name=\"$1\" value=\"$_v\" $3>"
}
cgi_textarea() {
  _v=`echo "$2"|htmlescape`
  cat<<EOF
<textarea name="$1" $3>$_v</textarea>
EOF
}
cgi_file() (			# In a subshell
  # $1=name $2=val(as filename) $3=args(if any)
  ## err cgi_file: \$1=$1 \$2=$2 \$3="[$3]"
  # Using global variable $dir
  case "$3" in
    *multiple*)
      onemore='<button type="button" id="morefile" title="More attachment file">添付追加</button>'
      multiple="$nl$onemore"
      ;;
  esac
  if [ -n "$2" -a -s "$dir/$2" ]; then
    file=$dir/$2
    bn=${file##*/}
    ct=`file --mime-type - < "$dir/$2" | cut -d' ' -f2`
    data=`percenthex "$file"`
    icon="<img src=\"data:$ct,$data\">"
  fi
  cat<<EOF
 ${icon}
<input type="file" name="$1" value="$bn" $3>$multiple
EOF
)
cgi_multi() (
  # $1=name $2=dir $3=func $4=args...
  # `dir' should contain $name.count and $name.N where N is 1 upto N
  i=1 name=$1 dir=$2 func=$3
  argnomulti=`echo "$4"|sed 's/multiple//'` # No multiple for Replaced entres
  n=`cat $dir/$name.count`
  case "$func" in
    *file) addrename=yes ;;
  esac
  echo '<table class="text">'
  while [ $i -le $n ]; do
    file=$name.$i
    vname=$file.`cat "$dir/$file.rowid"`
    val="`cat $dir/$file`"
    if [ -n "$addrename" ]; then
      # ${var:+..} cannot be used for inside-quotes incompatibility of shells
      ren1="<input class=\"action\" type=\"radio\" name=\"action.$vname\"
	id=\"mv.$vname\" value=\"mv\"><label for=\"mv.$vname\"
	title=\"Rename\">名前変更</label>"
      ren2="<input class=\"mv\" type=\"text\" name=\"mv.$vname\" value=\"$val\">"
    fi
    cat<<EOF
 <tr><td>($i)</td><td>
<input class="action" type="radio" name="action.$vname" id="keep.$vname"
 value="keep"><label for="keep.$vname" title="Keep">温存</label>
<input class="action" type="radio" name="action.$vname" id="edit.$vname"
 value="edit"><label for="edit.$vname" title="Replace">修正</label>
$ren1
<input class="action" type="radio" name="action.$vname" id="rm.$vname"
 value="rm"><label for="rm.$vname" title="Remove">削除</label>
<label class="confirm" title="OK?">本当に消します<input class="confirm" type="checkbox"
 name="confirm.$vname" value="yes">はい</label><br>
`$func $vname "$val" "$argnomulti"`<span>$val</span>
$ren2
</td></tr>
EOF
    i=$((i+1))
  done
  cat<<EOF
 <tr><td>(新規)</td><td>`$func $name "" "$4"`</td></tr>
</table>
EOF
)
# In these functions, $2 should be quoted because it can be null
cgi_multi_text() {
  cgi_multi $1 "$2" cgi_text "$3 class=\"edit\""
}
cgi_multi_textarea() {
  cgi_multi $1 "$2" cgi_textarea "$3 class=\"edit\""
}
cgi_multi_file() {
  # $1=name $2=val(filename)
  cgi_multi $1 "$2" cgi_file "$3 class=\"edit\""
}
cgi_datalist_h() {
  # $1=id $2...=hexed-args
  echo "<datalist id=\"$1\">"
  shift
  for i; do
    echo "<option value=\"`echo "$i"|unhexize|htmlescape`\"></option>"
  done
  echo "</datalist>"
}
cgi_select_h() {
  # $1=name $2...=hexed-args
  echo "<select name=\"$1\"${CGI_SELECT_MULTI:+ multiple} id=\"$1\">"
  shift
  for i; do
    echo "<option>`echo \"$i\"|unhexize|htmlescape`</option>"
  done
  echo "</select>"
}

html() {
  echo "<$*>"
  cat
  echo "</$1>"
}
