+#!/bin/sh
+##############################################################################
+# Copyright (c) 2016 Free Software Foundation, Inc. #
+# #
+# Permission is hereby granted, free of charge, to any person obtaining a #
+# copy of this software and associated documentation files (the "Software"), #
+# to deal in the Software without restriction, including without limitation #
+# the rights to use, copy, modify, merge, publish, distribute, distribute #
+# with modifications, sublicense, and/or sell copies of the Software, and to #
+# permit persons to whom the Software is furnished to do so, subject to the #
+# following conditions: #
+# #
+# The above copyright notice and this permission notice shall be included in #
+# all copies or substantial portions of the Software. #
+# #
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL #
+# THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING #
+# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER #
+# DEALINGS IN THE SOFTWARE. #
+# #
+# Except as contained in this notice, the name(s) of the above copyright #
+# holders shall not be used in advertising or otherwise to promote the sale, #
+# use or other dealings in this Software without prior written #
+# authorization. #
+##############################################################################
+# $Id: tput-initc,v 1.1 2016/12/10 23:35:46 tom Exp $
+# Some of the ".dat" files in ncurses' test-directory give r/g/b numbers for
+# default palettes of xterm and Linux console. This script reads the numbers
+# and (assuming the same or compatible terminal) uses tput to (re)initialize
+# the palette using those numbers.
+
+failed() {
+ printf "?? $*\n" >&2
+ exit 1
+}
+
+if [ $# = 1 ]
+then
+ file=$1
+elif [ $# = 0 ]
+then
+ file=$TERM.dat
+else
+ failed "expected one parameter or none"
+fi
+
+if [ ! -f "$file" ]
+then
+ if [ -f "$file.dat" ]
+ then
+ file="$file.dat"
+ else
+ failed "no such file: $file"
+ fi
+fi
+
+myterm=${file%%.dat}
+colors=$(tput -T $myterm colors 2>/dev/null)
+if [ ${colors:-0} -le 0 ]
+then
+ myterm=${myterm%%-color}
+ colors=$(tput -T $myterm colors 2>/dev/null)
+fi
+if [ ${colors:-0} -le 0 ]
+then
+ failed "terminal $myterm does not support color"
+fi
+
+cat $file |\
+awk -v myterm=$myterm '
+BEGIN { limit=1000; }
+function scaled(n) {
+ return (n * 1000)/limit;
+}
+
+/^scale:[0-9]+/{
+ sub("^scale:","",$0);
+ limit = $0;
+}
+
+/^[0-9]+:/{
+ sub(":","",$1);
+ printf "tput -T%s initc %d %d %d %d\n", myterm, $1,scaled($2),scaled($3),scaled($4);
+}' |sh -