Slackware の setup スクリプトを改変してキーボード再設定ツールを書いたらライセンス的に微妙だった話

インストール環境内、/usr/lib/setup/SeTkeymap を元ネタにキーボード再設定ツールを書いてみたが Use and redistribution covered by the same terms as the "setup" script.とあるので、ライセンス的に微妙。一応目的は再 “セットアップ” なので same terms と言えなくもないが。

元ネタから用途に照らして要らない部分をコメントアウトし適切な場所に以下のようなコードを放り込んでやれば O.K. ではある。

```
cat << EOF > /etc/rc.d/rc.keymap
#!/bin/sh
# Load the keyboard map.  More maps are in /usr/share/kbd/keymaps.
if [ -x /usr/bin/loadkeys ]; then
 /usr/bin/loadkeys $MAPNAME
fi
EOF
```
↑注意：これはほぼ /etc/rc.d/rc.keymap そのまんまなので、私の著作物ではありません。

/usr/share/kbd/keymaps/i386 以下から include を除いてリストを自動生成させる方向で新規に書いてみようかな。

# CC-BY かつワンライナー的な試作品

ってことで、/usr/lib/setup/SeTkeymap から離れてワンライナーっぽく書いてみた試作品。

```sh:試作品その1
mkfifo keymappipe && KEYMAP=$(basename $(cat keymappipe)) & dialog --menu keymaps 30 50 20 $(find /usr/share/kbd/keymaps/i386 -name "*.map.gz" | sort | grep --invert-match include | sed "s/\/usr\/share\/kbd\/keymaps\/i386\///g" | tr "\n" " " | sed "s/ / found /g") 2>keymappipe && printf '#!/bin/sh'"\n"'# Load the keyboard map.  More maps are in /usr/share/kbd/keymaps.'"\n"'if [ -x /usr/bin/loadkeys ]; then'"\n /usr/bin/loadkeys $KEYMAP\nfi" > rc.keymap && rm keymappipe || rm keymappipe
```

試作品その2。

```sh:試作品その2
mkfifo keymappipe && printf '#!/bin/sh'"\n"'# Load the keyboard map.  More maps are in /usr/share/kbd/keymaps.'"\n"'if [ -x /usr/bin/loadkeys ]; then'"\n /usr/bin/loadkeys $(basename $(cat keymappipe))\nfi" > rc.keymap & dialog --menu keymaps 30 50 20 $(find /usr/share/kbd/keymaps/i386 -name "*map*" | sort | grep --invert-match include | sed "s/\/usr\/share\/kbd\/keymaps\/i386\///g" | tr "\n" " " | sed "s/ / found /g") 2>keymappipe && rm keymappipe || rm keymappipe
```

試作品その3。これをベースに出来そう。

```sh:試作品その3
mkfifo keymappipe && printf '#!/bin/sh'"\n"'# Load the keyboard map.  More maps are in /usr/share/kbd/keymaps.'"\n"'if [ -x /usr/bin/loadkeys ]; then'"\n /usr/bin/loadkeys $(basename $(cat keymappipe | sed "s/.gz//g"))\nfi" > rc.keymap & dialog --menu "Select a keymap suitable for your keyboard." 30 50 20 $(find /usr/share/kbd/keymaps/i386 -name "*map*" | sort | grep --invert-match include | sed "s/\/usr\/share\/kbd\/keymaps\/i386\///g" | tr "\n" " " | sed "s/ / found /g") 2>keymappipe && rm keymappipe || rm keymappipe
```

試作品その４。このコードであればライセンス的にも問題ないだろうし、シェルスクリプトとして書き直せばリリース出来るかな。リリース版は [MIT ライセンス](https://ja.osdn.net/projects/opensource/wiki/licenses%2FMIT_license)にする予定。このページに乗っけてあるコードについては [CC-BY-4.0](https://creativecommons.org/licenses/by/4.0/deed.ja) でご自由にご利用下さい。

```sh:試作品その4
rm -f /tmp/keyboard-reconfig.pipe && mkfifo /tmp/keyboard-reconfig.pipe && printf '#!/bin/sh'"\n"'# Load the keyboard map.  More maps are in /usr/share/kbd/keymaps.'"\n"'if [ -x /usr/bin/loadkeys ]; then'"\n /usr/bin/loadkeys $(basename $(cat /tmp/keyboard-reconfig.pipe) || cat /etc/rc.d/rc.keymap | grep -v "#" | grep map | sed "s/ \/usr\/bin\/loadkeys //g")\nfi" > /etc/rc.d/rc.keymap & dialog --title "Console Keymap Selector" --menu "Select a keymap suitable for your keyboard." 22 50 15 $(find /usr/share/kbd/keymaps/i386 -name "*map*" | sort | grep --invert-match include | sed -e "s/\/usr\/share\/kbd\/keymaps\/i386\///g" -e "s/.gz//g" | tr "\n" " " | sed "s/ / found /g") 2>/tmp/keyboard-reconfig.pipe && rm /tmp/keyboard-reconfig.pipe || rm /tmp/keyboard-reconfig.pipe
```

![スクリーンショット_2019-07-11_10-32-50.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/410379/4adaecb5-fe45-a2c7-d315-186d48584d2d.png)

# MIT ライセンスのリリース候補版。

```sh:agete-keyboardreconfig
#!/bin/sh
# Copyright 2019 EarthSimilarityIndex, the 3rd Planet, Solar System
#
# Released under the MIT license
#
# 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, 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 AUTHORS OR 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.

rm -f /tmp/keyboardreconfig_rc.keymap.new
rm -f /tmp/keyboardreconfig.message
rm -f /tmp/keyboardreconfig.selection

while true; do
current_keymap=$(cat /etc/rc.d/rc.keymap | grep -v "#" |
	grep map | sed "s/ \/usr\/bin\/loadkeys //g")

dialog --msgbox "Current keymap is $current_keymap" 6 36

dialog --title "Console Keymap Selector"\
	--menu "Select a keymap suitable for your keyboard."\
	18 48 11 $(find /usr/share/kbd/keymaps/i386 -name "*map*" |
	sort | grep --invert-match include |
	sed -e "s/\/usr\/share\/kbd\/keymaps\/i386\///g" -e "s/.gz//g" |
	tr "\n" " " | sed "s/ / found /g") 2>/tmp/keyboardreconfig.selection
if [ $? = 1 ]; then
	loadkeys $current_keymap
	unset current_keymap
	unset selected_keymap
	unset result
	rm -f /tmp/keyboardreconfig.message
	rm -f /tmp/keyboardreconfig.selection
	rm -f /tmp/keyboardreconfig_rc.keymap.new
	exit
fi

cat << EOS > /tmp/keyboardreconfig_rc.keymap.new
#!/bin/sh
# Load the keyboard map.  More maps are in /usr/share/kbd/keymaps.
if [ -x /usr/bin/loadkeys ]; then
 /usr/bin/loadkeys $(basename $(cat /tmp/keyboardreconfig.selection) ||
 printf $current_keymap)
fi
EOS

selected_keymap=$(cat /tmp/keyboardreconfig_rc.keymap.new | grep -v "#" |
	grep map | sed "s/ \/usr\/bin\/loadkeys //g")

loadkeys $selected_keymap

cat << EOS > /tmp/keyboardreconfig.message
Try the keymap selected here.
After that, use "Tab" key
to choose accept or deny.
EOS

dialog --title "$selected_keymap is selected"\
	--ok-label "Accept" --cancel-label "Deny"\
	--editbox /tmp/keyboardreconfig.message 12 36 2>/dev/null
if [ $? = 0 ]; then
	cat /tmp/keyboardreconfig_rc.keymap.new > /etc/rc.d/rc.keymap
	unset current_keymap
	unset selected_keymap
	unset result
	rm /tmp/keyboardreconfig.message
	rm /tmp/keyboardreconfig.selection
	rm /tmp/keyboardreconfig_rc.keymap.new
	exit
fi

done
```

（下）ネタ的な意味では WTFPL が魅力的なんだけど、今回は無難に MIT 。

何度か見直して GitLab にぽーいの予定。これと init の on/off ツールと /etc/profile.d の on/off ツールと setup で選択し忘れたパッケージグループの追加インストールツールとで「Agate」シリーズとしてリリースします。日本語環境関連は「Menou」シリーズを準備中。

余談ですが、瑪瑙いいよね。あとラピスラズリ。あーいう不純なやつら。綺麗はきたない、きたないは綺麗。純粋なモノは危うくて嫌い。

「いざ、お供つかまつらん、ラピスラズリの鉱脈を探す旅に」