#!/bin/sh # echo without interpretation of backslash escapes and without # adding newline at the end - just the string as it is rawprint() { printf '%s' "$@" } encode_dword() { printf '\x%02x' \ `expr $1 / 16777216 % 256` \ `expr $1 / 65536 % 256` \ `expr $1 / 256 % 256` \ `expr $1 % 256` } encode_string() { # turn string into UTF-16 and hexdump it hex_utf16=`rawprint "$1" | iconv -t UTF-16BE | od -t x1 -A n | tr -d -c '[:xdigit:]'`; encode_dword `rawprint "$hex_utf16" | wc -m | xargs -I {} expr {} / 2` # length = num hex digits / 2 rawprint "$hex_utf16" | sed 's/../\\x&/g' # put '\x' prefix before every pair of digits } encode_single_define() { encode_string `rawprint "$1" | grep -o '^[^=]*'` # everything before '=' rawprint '\x00\x00\x00\n\x00' encode_string `rawprint "$1" | sed 's/^[^=]*=\?//'` # everything after '=' } # $1 - newline-separated list of defines encode_defines() { # add some fixed defines and discard empty lines from the tail defines=`printf 'va_list\nva_args\n%s' "$1"` # count lines (no newline at the end so add one) encode_dword `printf '%s\n' "$defines" | wc -l` while [ -n "$defines" ]; do encode_single_define `rawprint "$defines" | head -n 1` defines=`rawprint "$defines" | tail -n +2` done } encode_includes() { encode_dword 3 # number of custom includes encode_string "$1/src/stdafx.h" encode_string "$1/objs/lang" encode_string "$1/objs/setting" } # escape with a backslash (\) characters special to the sed replace string: \ & # also escape our custom filed separator that we will be using in sed: @ escape_sed_special() { sed -e 's/[\&@]/\\&/g' } PROJECT_DIR=`pwd` DIR_NAME=`pwd | xargs -0 basename` USAGE_TEXT='Usage: kdev-gen [PROJECT_NAME|-h|--help] PROJECT_NAME is the name of the project that will be displayed in KDevelop. Before executing, cd into OpenTTD folder and run ./configure script. -h, --help print help and exit' case "$# $1" in '1 -h' | '1 --help') printf 'Generate OpenTTD project files for KDevelop 4.\n\n%s\n' "$USAGE_TEXT"; exit 0;; 1*) PROJECT_NAME="$1";; 0*) PROJECT_NAME="$DIR_NAME";; *) printf 'Wrong arguments given. %s\n' "$USAGE_TEXT" >&2; exit 1;; esac CFLAGS=`grep '^using CFLAGS\.\.\.' config.log 2>/dev/null` if [ -z "$CFLAGS" ]; then echo "OpenTTD config.log not found" >&2 echo "cd into OpenTTD first and run 'configure'" >&2 exit 1 fi DEFINES=`eval "printf '%s\n' $CFLAGS" | grep '^\-D' | cut -c3-` PROJECT_NAME_SED=s@!!PROJECT_NAME!!@`rawprint "$PROJECT_NAME" | escape_sed_special`@g PROJECT_DIR_SED=s@!!PROJECT_DIR!!@`rawprint "$PROJECT_DIR" | escape_sed_special`@g CUSTOM_DEFINES_SED=s@!!CUSTOM_DEFINES!!@`encode_defines "$DEFINES" | escape_sed_special`@g CUSTOM_INCLUDES_SED=s@!!CUSTOM_INCLUDES!!@`encode_includes "$PROJECT_DIR" | escape_sed_special`@g mkdir -p .kdev4 sed -e "$PROJECT_NAME_SED" \ >"$PROJECT_DIR/$DIR_NAME.kdev4" \ << "EOF" [Project] Manager=KDevCustomMakeManager Name=!!PROJECT_NAME!! EOF sed -e "$PROJECT_DIR_SED" -e "$CUSTOM_DEFINES_SED" -e "$CUSTOM_INCLUDES_SED" \ >"$PROJECT_DIR/.kdev4/$DIR_NAME.kdev4" \ << "EOF" [CustomDefinesAndIncludes][ProjectPath0] Defines=!!CUSTOM_DEFINES!! Includes=!!CUSTOM_INCLUDES!! Path=. [Defines And Includes][Compiler] Name=GCC Path=gcc Type=GCC [Filters] size=29 [Filters][0] inclusive=0 pattern=.* targets=3 [Filters][1] inclusive=0 pattern=.git targets=2 [Filters][10] inclusive=0 pattern=*.o targets=1 [Filters][11] inclusive=0 pattern=*.a targets=1 [Filters][12] inclusive=0 pattern=*.so targets=1 [Filters][13] inclusive=0 pattern=*.so.* targets=1 [Filters][14] inclusive=0 pattern=moc_*.cpp targets=1 [Filters][15] inclusive=0 pattern=*.moc targets=1 [Filters][16] inclusive=0 pattern=ui_*.h targets=1 [Filters][17] inclusive=0 pattern=qrc_*.cpp targets=1 [Filters][18] inclusive=0 pattern=*~ targets=1 [Filters][19] inclusive=0 pattern=*.orig targets=1 [Filters][2] inclusive=0 pattern=CVS targets=2 [Filters][20] inclusive=0 pattern=.*.kate-swp targets=1 [Filters][21] inclusive=0 pattern=.*.swp targets=1 [Filters][22] inclusive=0 pattern=*.pyc targets=1 [Filters][23] inclusive=0 pattern=*.pyo targets=1 [Filters][24] inclusive=0 pattern=/objs targets=2 [Filters][25] inclusive=0 pattern=*.rej targets=1 [Filters][26] inclusive=0 pattern=*.orig targets=1 [Filters][27] inclusive=0 pattern=*.porig targets=1 [Filters][28] inclusive=0 pattern=*.ansi targets=3 [Filters][3] inclusive=0 pattern=.svn targets=2 [Filters][4] inclusive=0 pattern=_svn targets=2 [Filters][5] inclusive=0 pattern=SCCS targets=2 [Filters][6] inclusive=0 pattern=_darcs targets=2 [Filters][7] inclusive=0 pattern=.hg targets=2 [Filters][8] inclusive=0 pattern=.bzr targets=2 [Filters][9] inclusive=0 pattern=__pycache__ targets=2 [Launch] Launch Configurations=Launch Configuration 0 [Launch][Launch Configuration 0] Configured Launch Modes=execute Configured Launchers=nativeAppLauncher Name=Launch OpenTTD\s Type=Native Application [Launch][Launch Configuration 0][Data] Arguments=-d 1 Dependencies=@Variant(\x00\x00\x00\t\x00\x00\x00\x00\x00) Dependency Action=Nothing EnvironmentGroup= Executable=file://!!PROJECT_DIR!!/bin/openttd External Terminal=konsole --noclose --workdir %workdir -e %exe Project Target= Use External Terminal=false Working Directory=file://!!PROJECT_DIR!!/bin isExecutable=true EOF