#!/bin/sh
# url: http://pank.org/scripts/
# description: newcj3cin install script for scim
# comment: csh bash
# platform: linux

TABLE=NewCJ3.txt.in
NO_DOT_COMMA=1
NO_PTT=1
CIN_URL=http://cle.linux.org.tw/candyz/NewCJ3/004/NewCJ3.cin

if [ ! -f NewCJ3.cin ] ; then
    echo NewCJ3.cin not found, try to get cle.linux.org.twe
    echo
    wget $CIN_URL
fi

if [ ! -f /usr/share/scim/icons/newcj3.png ] ; then
    wget -P /usr/share/scim/icons http://pank.org/scripts/newcj3.png
fi

if [ -f NewCJ3.cin ] ; then
    [ $NO_DOT_COMMA = 1 ] && sed -i -re "/\.|,/d" NewCJ3.cin
    [ $NO_PTT = 1 ] && sed -i "/^ptt;/d" NewCJ3.cin
    cat<<EOF>$TABLE
### File header must not be modified
### This file must be encoded into UTF-8.
### This file comes from http://www.chinesecj.com
SCIM_Generic_Table_Phrase_Library_TEXT
VERSION_1_0

### Begin Table definition.
BEGIN_DEFINITION

### An unique id to distinguish this table among others.
### Use uuidgen to generate this kind of id.
UUID = 1da9d4ff-ccdd-45af-b1a5-7bd4ac0aeb5f

### A unique number indicates the version of this file.
### For example the last modified date of this file.
### This number must be less than 2^32.
SERIAL_NUMBER = `date +%Y%m%d`01

ICON = /usr/share/scim/icons/newcj3.png

### The default name of this table
NAME = NewCJ3

### The local names of this table 
NAME.zh_CN = 乱仓打鸟
NAME.zh_TW = 亂倉打鳥
NAME.zh_HK = 亂倉打鳥

### The author of this table
AUTHOR = Roy Hiu-yeung Chan <roy.h.y.chan@gmail.com>

### Supported languages of this table
LANGUAGES = zh_TW,zh_HK,zh_CN,zh_SG

### Prompt string to be displayed in the status area.
STATUS_PROMPT = 中

### The Keyboard Layout used by this table. Unset or set to "Unknown" to accept any kind of layouts.
KEYBOARD_LAYOUT = US_Default

### If true then the first candidate phrase
### will be selected automatically during inputing.
AUTO_SELECT = TRUE

### If true then a multi wildcard will be appended
### at the end of inputing string automatically.
AUTO_WILDCARD = FALSE

### If true then the result string will be committed to client automatically.
### This should be used with AUTO_SELECT = TRUE.
AUTO_COMMIT = FALSE

### If true then the key will be splitted during inputing automatically.
AUTO_SPLIT = TRUE

### If true then the phrases' frequencies will be adjusted dynamically.
DYNAMIC_ADJUST = FALSE 

### If true then the preedit area will be filled up by the current candidate phrase automatically.
AUTO_FILL = FALSE

### If true then the lookup table will always be shown if there is any candidate phrase.
### Otherwise the lookup table won't be shown unless the user requires it by moving the preedit caret left.
ALWAYS_SHOW_LOOKUP = TRUE

### Use full width punctuation by default
DEF_FULL_WIDTH_PUNCT = FALSE

### Use full width letter by default
DEF_FULL_WIDTH_LETTER = FALSE

### The maxmium length of a key.
MAX_KEY_LENGTH = 5

### Show key prompt if true
SHOW_KEY_PROMPT = TRUE

### Discard invalude key if true
DISCARD_INVALID_KEY = FALSE

### Valid input chars.
EOF
     if [ $NO_DOT_COMMA = 1 ] ; then
         echo "VALID_INPUT_CHARS = abcdefghijklmnopqrstuvwxyz';[]" >> $TABLE
     else
         echo "VALID_INPUT_CHARS = abcdefghijklmnopqrstuvwxyz',.;[]" >> $TABLE
     fi
     cat<<EOF>>$TABLE
### Single wildcard char, can have multiple chars.
### SINGLE_WILDCARD_CHAR = ?

### Multi wildcard char.
MULTI_WILDCARD_CHAR = *

### The key strokes to split inputted string.
### SPLIT_KEYS = quoteright

### The key strokes to commit the convert result to client.
COMMIT_KEYS = space

### The keys to forward the inputted string to client.
### FORWARD_KEYS = Return

### The key strokes to select phrases
SELECT_KEYS = 1,2,3,4,5,6,7,8,9

### The key strokes to page up the lookup table.
PAGE_UP_KEYS = Shift+less,Page_Up,comma,minus

### The key strokes to page down.
PAGE_DOWN_KEYS = Shift+greater,Page_Down,period,equal,space

### Define the prompts of each valid input char.
BEGIN_CHAR_PROMPTS_DEFINITION
a 日
b 月
c 金
d 木
e 水
f 火
g 土
h 竹
i 戈
j 十
k 大
l 中
m 一
n 弓
o 人
p 心
q 手
r 口
s 尸
t 廿
u 山
v 女
w 田
x 難
y 卜
z 重
END_CHAR_PROMPTS_DEFINITION
END_DEFINITION

### Begin Table data.
BEGIN_TABLE
EOF
    N=`grep -n 'chardef begin' NewCJ3.cin | cut -d: -f1`
    tail +$[N+1] NewCJ3.cin | sed '/chardef end/d' >> $TABLE
    echo END_TABLE >> $TABLE
    scim-make-table $TABLE -b -o /usr/share/scim/tables/NewCJ3.bin
    [ -f /usr/share/scim/tables/NewCJ3.bin ] && echo OK
else
    echo NewCJ3.bin not found
fi