+case "$(GET action)" in
+ newid)
+ NEWID="$(genid)"
+
+ { git init "users/${NEWID}" || mkdir -p "users/${NEWID}"; } >&-
+
+ printf '%s 303 See Other\r\n' "$SERVER_PROTOCOL"
+ printf 'Location: %s\r\n' "${SCRIPT_NAME}?id=${NEWID}"
+ SET_COOKIE +8640000 "id=${NEWID}"
+ printf '\r\n'
+ exit 0
+ ;;
+ newfolder)
+ name="$(POST name |head -n1)"
+ fid="$(timeid)"
+ order="$(
+ head -qn1 "${BDB}"/????????????.bm \
+ | cut -f3 \
+ | sort -n \
+ | tail -n1 \
+ || printf 1
+ )"
+ order="$(((order + 1000) / 1000 * 1000))"
+ if [ -n "$name" -a -d "${BDB}" ]; then
+ printf '%s\t%s\t%s\n' "$fid" "$(HTML "$name")" "$order" >"${BDB}/${fid}.bm"
+ fi
+ REDIRECT "${SCRIPT_NAME}#${fid}"
+ ;;
+ modfolder)
+ name="$(POST name |head -n1)"
+ fid="$(POST fid | checkid)"
+ file="${BDB}/${fid}.bm"
+ if [ "$(POST control)" = confirm -a -n "$name" -a -f "$file" ]; then
+ order="$(head -n1 "$file" |cut -f3 || printf 1000)"
+ printf '%s\t%s\t%s\n' "$fid" "$(HTML "$name")" "$order" >"${file%.bm}.tmp"
+ tail -n+2 "$file" >>"${file%.bm}.tmp"
+ mv "${file%.bm}.tmp" "$file"
+ fi
+ REDIRECT "${SCRIPT_NAME}#${fid}"
+ ;;
+ delfolder)
+ fid="$(POST fid | checkid)"
+ target="$(POST target | checkid)"
+ file="${BDB}/${fid}.bm"
+ tfile="${BDB}/${target}.bm"
+ if [ "$(POST control)" = confirm -a -f "$file" ]; then
+ if [ "$target" = "____________" ] || tail -n+2 "$file" >>"$tfile"; then
+ rm -f "$file"
+ fi
+ fi
+ REDIRECT "${SCRIPT_NAME}#${target}"
+ ;;
+ movefolder)
+ fid="$(POST fid | checkid)"
+ target="$(POST target | checkid)"
+ file="${BDB}/${fid}.bm"
+ tfile="${BDB}/${target}.bm"
+ if [ "$target" = "____________" -a -f "$file" -a "$(POST control)" = confirm ]; then
+ read nan1 nan2 last nan3 <"${BDB}/$(list_folders |tail -n1 |cut -f1).bm"
+ sed -ri "1s;^(([^\t]+\t){2})[^\t]+(.*)$;\1$((${last:-0} + 1000))\3;;" "$file"
+ elif [ -f "$tfile" -a -f "$file" -a "$(POST control)" = confirm ]; then
+ read nan1 nan2 tid nan3 <"$tfile"
+ sed -ri "1s;^(([^\t]+\t){2})[^\t]+(.*)$;\1$((${tid:-1} -1))\2;;" "$file"
+ order_files
+ fi
+ REDIRECT "${SCRIPT_NAME}#${fid}"
+ ;;
+ newbookmark)
+ fid="$(POST fid | checkid)"
+ name="$(POST name |head -n1)"
+ url="$(POST url |head -n1)"
+ file="${BDB}/${fid}.bm"
+ bid="$(timeid)"
+ if [ -n "$name" -a -f "${file}" ]; then
+ printf '%s\t%s\t%s\n' "$bid" "$(HTML "$name")" "$(HTML "$url")" >>"${file}"
+ fi
+ getFavicon "$url" "$bid"
+ REDIRECT "${SCRIPT_NAME}#${fid}"
+ ;;
+ modbookmark)
+ bid="$(POST bid | checkid)"
+ name="$(POST name |head -n1)"
+ url="$(POST url |head -n1)"
+ file="$(grep -lE "^${bid}" "${BDB}"/????????????.bm)"
+ if [ -w "$file" -a -n "$name" -a -n "$url" ]; then
+ bm="$(printf '%s\t%s\t%s' "$bid" "$(HTML "$name")" "$(HTML "$url")" |sed -r 's;[\&\;];\\&;g;')"
+ sed -ri "s;^${bid}\t.*$;${bm};" "$file"
+ fi
+ getFavicon "$url" "$bid"
+ REDIRECT "${SCRIPT_NAME}#${fid}"
+ ;;
+esac
+
+list_bookmarks(){
+ fid="$1"
+ bmodify="$(GET bmodify |checkid)"
+
+ tail -n+2 "${BDB}/${fid}.bm" \
+ | while read bid name url; do
+ if [ "${bid}" = "$bmodify" ]; then
+ cat <<-EOF
+ <form class="modbookmark" method="POST" action="${SCRIPT_NAME}?action=modbookmark">
+ <input type="hidden" name="bid" value="${bid}" />
+ <input type="text" name="name" value="${name}")" placeholder="Name" />
+ <input type="text" name="url" value="${url}")" placeholder="URL" />
+ <button type="submit">Modify</button>
+ </form>
+ EOF
+ else
+ cat <<-EOF
+ <div class="bookmark">
+ <a class="modify" href="${SCRIPT_NAME}?bmodify=${bid}">Modify</a>
+ <a class="link" href="${url}")"><img src="${BDB}/favicons/${bid}.ico"/>${name}</a>
+ </div>
+ EOF
+ fi
+ done
+}
+
+foldermod(){
+ fmodify="$(GET fmodify |checkid )"
+ fdelete="$(GET fdelete |checkid )"
+ fmove="$(GET fmove |checkid )"
+
+ if [ -n "$fmodify" ]; then
+ read fid fname order <"${BDB}/${fmodify}.bm"
+ cat <<-EOF
+ <form class="modfolder rename" method="POST" action="${SCRIPT_NAME}?action=modfolder">
+ <h1>Rename Folder: ${fname}</h1>
+ <input type="hidden" name="fid" value="${fid}" />
+ <label class="tab">Rename</label>
+ <a class="tab"
+ href="${SCRIPT_NAME}?fdelete=${fid}">Delete</a>
+ <a class="tab"
+ href="${SCRIPT_NAME}?fmove=${fid}">Move</a>
+ <input type="text" name="name" value="${fname}" />
+ <button type="submit" name="control" value="confirm">OK</button>
+ <button type="submit" name="control" value="cancel">Cancel</button>
+ </form>
+ EOF
+ elif [ -n "$fdelete" ]; then
+ read fid fname order <"${BDB}/${fdelete}.bm"
+ cat <<-EOF
+ <form class="modfolder delete" method="POST" action="${SCRIPT_NAME}?action=delfolder">
+ <h1>Delete Folder: ${fname}</h1>
+ <input type="hidden" name="fid" value="${fid}" />
+ <a class="tab" href="${SCRIPT_NAME}?fmodify=${fid}">Rename</a>
+ <label class="tab">Delete</label>
+ <a class="tab"
+ href="${SCRIPT_NAME}?fmove=${fid}">Move</a>
+ <label>Pass Bookmarks on to:</label>
+ <select name="target">
+ $(printf '<option value="%s">%s</option>' $(list_folders |grep -v "^${fid}"))
+ <option value="____________">(discard)</option>
+ </select>
+ <button type="submit" name="control" value="confirm">OK</button>
+ <button type="submit" name="control" value="cancel">Cancel</button>
+ </form>
+ EOF
+ elif [ -n "$fmove" ]; then
+ read fid fname order <"${BDB}/${fmove}.bm"
+ cat <<-EOF
+ <form class="modfolder move" method="POST" action="${SCRIPT_NAME}?action=movefolder">
+ <h1>Move Folder: ${fname}</h1>
+ <input type="hidden" name="fid" value="${fid}" />
+ <a class="tab" href="${SCRIPT_NAME}?fmodify=${fid}">Rename</a>
+ <a class="tab" href="${SCRIPT_NAME}?fdelete=${fid}">Delete</a>
+ <label class="tab">Move</label>
+ <label>Move before folder:</label>
+ <select name="target">
+ $(printf '<option value="%s">%s</option>' $(list_folders |grep -v "^${fid}"))
+ <option value="____________">(last)</option>
+ </select>
+ <button type="submit" name="control" value="confirm">OK</button>
+ <button type="submit" name="control" value="cancel">Cancel</button>
+ </form>
+ EOF
+ fi
+}
+
+show_folders(){
+ list_folders \
+ | while read fid fname order; do
+ cat <<-EOF
+ <section class="folder" id="${fid}">
+ <h1>${fname}</h1>
+ <a class="modify" href="${SCRIPT_NAME}?fmodify=${fid}">Modify</a>
+ $(list_bookmarks "$fid")
+ <form class="newbookmark" method="POST" action="${SCRIPT_NAME}?action=newbookmark">
+ <input type="hidden" name="fid" value="${fid}" />
+ <input type="text" name="name" value="" placeholder="Name" />
+ <input type="text" name="url" value="" placeholder="URL" />
+ <button type="submit">New Bookmark</button>
+ </form>
+ </section>
+ EOF
+ done
+}
+
+SET_COOKIE +8640000 "id=${COKID}" # Refresh Cookie
+printf 'Content-Type: text/html; charset=utf-8\r\n\r\n'
+
+cat <<EOF
+<!DOCTYPE HTML>
+<HTML><head>
+ <title>Bookman - Your Collection</title>
+ <link rel="stylesheet" type="text/css" href="bookmarks.css" />
+</head><body id="collection">
+ $(foldermod)
+ $(show_folders)
+ <form class="newfolder" method="POST" action="${SCRIPT_NAME}?action=newfolder">
+ <input type="text" name="name" value="" placeholder="New Folder" />
+ <button type="submit">New</button>
+ </form>
+</body></HTML>
+EOF
+
+#set filetype=sh