#!/bin/bash
# coding: utf8
# original filename: wps-office-conf.sh
# show Kingsoft WPS Office version and configure information
#
# 2014-11-19 updated, by Careone <emacslocale@126.com>
#
## 2023-1-12 updated
# WPS Office 11.1.0.11664, amd64 (on Debian 11.6), 2022-6-8 released

APPNAME="wps-office-conf"
APPVER="11.1.0.11664-1"

DEBUG=0		#set 1 to show debug output

### defines ###
#WPS_NAME="Kingsoft WPS Office"
WPS_NAME="WPS Office"
WPS_PKGNAME=wps-office	#old package name: kingsoft-office
wps_global_dir="/opt/kingsoft/wps-office"

# config to read version information
wps_setup_cfg="$wps_global_dir/office6/cfgs/setup.cfg"

declare -a wps_conf	#possiable user WPS Office config filenames
declare -a wps_existed_conf	#config filenames found for current user

# menu language in user config, (may named as 'Office.conf')
declare -a wps_lang	

## default config file in version 9.1.0.4909 Alpha16patch1:
# differences: upper/caselowercase in [Kk]ingsoft and [Oo]ffice
	wps_conf[0]=".config/Kingsoft/Office.conf"	#current
# and may rename in later versions
	wps_conf[1]=".config/kingsoft/Office.conf"
	wps_conf[2]=".config/kingsoft/office.conf"
	wps_conf[3]=".config/Kingsoft/office.conf"
	
#optional multi-languages message settings
locale_dir="$wps_global_dir/$APPNAME/locale"

mui_global_dir="/opt/kingsoft/wps-office/office6/mui"
mui_user_dir=".kingsoft/office6/mui"

## mirrors for downloading WPS Office for Linux
declare -a mirror wps_cmd cmd_desc
mirror[0]="https://www.wps.cn/product/wpslinux"	#2020-3-04 updated

wps_cmd=( wps et wpp wpspdf )
cmd_desc=( "WPS 文字" "WPS 表格" "WPS 演示" "金山 PDF" )
	### global and user locations directories
	## for example:
	# /opt/kingsoft/wps-office/office6/mui/9.1.0.4909/zh_CN/"
	# or
	# /opt/kingsoft/wps-office/office6/mui/zh_CN/"
	
	# $HOME/.kingsoft/office6/mui/9.1.0.4909/zh_CN/
	# or
	# $HOME/.kingsoft/office6/mui/zh_CN/

# tag 56:
# function 1
	_usage () {
cat<<EOF
Usage: $APPNAME
       $APPNAME [OPTION]
  show informations about $WPS_NAME, or setup menu language

OPTIONS
  -l, --language [en | zh_CN | zh_TW | zh_HK | zh_Hant_CN]
    set $WPS_NAME menu language. en = English,
    zh_CN (Simpified Chinese, China), zh_TW (Traditional Chinese, Taiwan),
    zh_HK (Traditional Chinese, Hong Kong), 
    zh_Hant_CN (Traditional Chinese, China)
  -v, --version     print the version information and exit
  -h, --help        print this message and exit
EOF
}

# function 2
	_usage_cn () {
cat<<EOF
用法: $APPNAME
      $APPNAME [选项]
  显示 $WPS_NAME 的软件版本和配置文件信息, 
  或者设置 $WPS_NAME 菜单语言为 简体/繁体中文/英文

选项
  -l, --language [en | zh_CN | zh_TW | zh_HK | zh_Hant_CN]
    设置 $WPS_NAME 菜单语言。en 代表英语,
    zh_CN (简体中文), zh_Hant_CN (中国大陆繁体中文)
    zh_TW (台湾繁体中文), zh_HK (香港繁体中文)
  -v, --version     显示版本信息并退出
  -h, --help        显示帮助信息并退出
EOF
}

# function 3
	_about_language () {
cat<<EOF
   常用的 $WPS_NAME 菜单语言参数 (简体/繁体中文/英文)
     用法示例: $APPNAME -l zh_CN  (设为 简体中文)
     ------------------------------ 
     en      设为 英语 (English)
     zh_CN   设为 简体中文 (Simpified Chinese, China)
     zh_Hant_CN   设为 中国大陆繁体中文 (Traditional Chinese, China) 
     zh_TW   设为 台湾繁体中文 (Traditional Chinese, Taiwan)
     zh_HK   设为 香港繁体中文 (Traditional Chinese, Hong Kong) 
     ------------------------------ 
EOF
}

# function 4
	_init () {
cat <<EOF
[General]
languages=zh_CN
EOF
}

### defines end ###

### body ###

# tag 140:

## case 2: if have option given
for option in "$@"; do
	case "$option" in
	-h | --help)
## sample:
# LANGUAGE: zh_CN:zh
# LANG: zh_CN:utf8
	  case "$LANG" in
	    zh_CN* | zh_*) _usage_cn ;;
	    en* | *) _usage ;;
	  esac
	exit 0 ;;

	-l | --language)
	#usage: -l LANG
	#example: -l [en | zh_CN | zh_Hant_CN | zh_TW | zh_HK]
	OPT="$1"
	## ---- MODULE 001: search config 'Office.conf' ###
	if [ "$#" -eq 1 ]; then
	  echo " * 检测 $WPS_NAME 菜单语言设置..."
	else echo " * 设置 $WPS_NAME 菜单语言..."
	fi
	
	_about_language

	#echo "check USER configure for Kingsoft WPS Office..."
	for a in "${wps_conf[@]}"; do
	  if [ -f "$HOME/$a" ]; then
	    echo "   找到 [用户] 配置文件: ~/$a"
		if [ "${wps_existed_conf[0]}" = "" ]; then
		  wps_existed_conf=( "$a" )
		else
	          wps_existed_conf=( "${wps_existed_conf[@]}" "$a" )
		fi
	    if grep '^languages=' "$HOME/$a" &>/dev/null; then
	      lang=`grep '^languages=' "$HOME/$a" | tail -1 | cut -d= -f2`
	      wps_lang=( "${wps_lang[@]}" "$lang" )
	  	echo -e "   当前用户的 菜单语言 设置:\n --------------- "
	  	grep '^languages=' "$HOME/$a"
	  	echo -e " --------------- \n"
	    fi
	  fi
	done
	### ==== MODULE 001 END ====

	#update language setting, if necessary
	if [ "$#" -ge 2 ]; then
	  TOLANG="$2"

# auto fix lower and upper letter errors for Chinese language	
	  case "$TOLANG" in
 	    [cC][nN] | [zZ][hH] | [zZ][hH][_-][cC][nN]) TOLANG="zh_CN" ;;
 	    [tT][wW] | [zZ][hH][_-][tT][wW]) TOLANG="zh_TW" ;;
 	    [hH][kK] | [zZ][hH][_-][hH][kK]) TOLANG="zh_HK" ;;
 	    [hH][aA][nN][tT]] | [zZ][hH][_-][hH][aA][nN][tT]][_-][cC][nN]) TOLANG="zh_Hant_CN" ;;
 	    *) : ;;
	  esac

	  #TODO: check TOLANG available or not, and ignore void values

	for a in "${wps_existed_conf[@]}"; do
	  echo " * 更新 [用户] 配置文件 '~/$a'..."

	  if ! grep "^languages=" "$HOME/$a" &>/dev/null; then
	    if ! grep '^\[General\]' "$HOME/$a" &>/dev/null; then
	      #insert '[General]' in frist line of config, and
	      #insert 'languages=LANGUAGE' as line 2
	      sed -i "1i\[General\]\nlanguages=$TOLANG" "$HOME/$a"
	    else
	      #append line 'languages=LANGUAGE' after line '[General]'
	      sed -i "/^\[General\]/alanguages=$TOLANG" "$HOME/$a"
	    fi    
	  fi

	  sed -i "/^languages=/clanguages=$TOLANG" "$HOME/$a"
	  echo -e "   新的 菜单语言 设置:\n --------------- "
	  grep '^languages=' "$HOME/$a"
	  echo -e " --------------- \n"
	done

	fi
	exit 0 ;;

	-v | --version)
	echo "  $APPNAME $APPVER"
	exit 0
	;;

	-*)echo "Unrecognized option \`$option'" 1>&2
	;;
	esac
done

# tag 200:
## tag 001
# probe current language setting. for examples: zh_CN, zh_TW
#CLANG=`echo $LANG | cut -d'.' -f1`

# case 1: if no option given, show information about Kingsoft WPS Office;
# case 2: else check and run options

## case 1: no option given
if [ "$#" -eq 0 ]; then

## tag 010
### check installed versions

## tag 011
## probe WPS Office version.
# read global config '.../office6/setup.cfg'. default method.

# tag 136: TODO:
#DIST_ID=`lsb_release -si`	#answer very slowly. example: Debian
#DIST_ID=`uname -n`		#answer very quickly. example: debian

	# check package name
	if which wps-office &>/dev/null; then
	  WPS_PKGNAME="wps-office"	#for newer versions
	elif which kingsoft-office &>/dev/null; then
	  WPS_PKGNAME="kingsoft-office"	#for old versions
	else WPS_PKGNAME="wps-office"
	fi

# tag 230:
## --- tag 012b ---
## read frist 5 lines in config, to get installed version information
#  for example:
## /opt/kingsoft/wps-office/office6/cfgs/setup.cfg
#--------------
#[Version]
#MajorVersion=9
#FirstVersion=1
#SecondVersion=0
#Version=4937
#==============

	# output version information
	if [ -e "$wps_setup_cfg" ]; then
	  echo -e " * 找到 [系统] 配置文件: $wps_setup_cfg\n"
	  WPS_VER=`echo $(sed '2,5!d' "$wps_setup_cfg" | cut -d'=' -f2) | sed '/\ /s//\./g'`
	  ## debug module
	  if [ "$DEBUG" = 1 ]; then
	    if head -5 "$wps_setup_cfg" &>/dev/null; then
	      echo "  ------------------- "
	      head -5 "$wps_setup_cfg"
	      echo "  ------------------- "
	    fi
	  fi
	  ## debug module end
	fi

	# save version information to array

### show informations about installed version and bins
	if which "${wps_cmd[0]}" &>/dev/null; then
	  if [ "$WPS_VER" != "" ]; then
	    echo -e " 已安装 $WPS_NAME $WPS_VER\n"
	  fi
	  
	  for a in `seq 0 1 3`; do
	    if which ${wps_cmd[a]} &>/dev/null; then
	      echo -e " ** 找到: `which ${wps_cmd[a]}`\t( ${cmd_desc[a]} )"
	    fi
	  done
	fi

# tag 240:


## === tag 012b end ===
### show location directory path
	echo -e "\n * [系统] 菜单翻译文件目录: $mui_global_dir/"
	echo -e " * [用户] 菜单翻译文件目录: ~/$mui_user_dir/\n"
	
	# invoke option -l
	$0 -l	
	exit 0
fi
	
exit 0;
exit;

