#!/bin/sh
# $FreeWRT$

if ! grep /bin/mksh ${IPKG_INSTROOT}/etc/shells >/dev/null 2>&1; then
	echo /bin/mksh >>${IPKG_INSTROOT}/etc/shells
fi

if ! grep KSH_VERSION ${IPKG_INSTROOT}/etc/profile >/dev/null 2>&1; then
	echo 'if test -n "$KSH_VERSION"; then' >>${IPKG_INSTROOT}/etc/profile
	echo '	: ${HOSTNAME:=$(</proc/sys/kernel/hostname)}' >>${IPKG_INSTROOT}/etc/profile
	echo '	PS1='\''$(((rv=$?)) && print $rv\|)${USER:=$(id -un)}@${HOSTNAME:=nil}:$(' >>${IPKG_INSTROOT}/etc/profile
	echo '		local pfx=~ wd=${PWD:-$(pwd)}' >>${IPKG_INSTROOT}/etc/profile
	echo '		[[ ${wd#$pfx} = $wd || $pfx = ?(/) ]] || wd=\~${wd#$pfx}' >>${IPKG_INSTROOT}/etc/profile
	echo '		pfx=; let n="( (n = ${COLUMNS:-80} / 3) < 7 ? 7 : n)"' >>${IPKG_INSTROOT}/etc/profile
	echo '		while (( (${#pfx} + ${#wd}) > n )); do' >>${IPKG_INSTROOT}/etc/profile
	echo '			if [[ $wd = */* ]]; then' >>${IPKG_INSTROOT}/etc/profile
	echo '				pfx=.../' >>${IPKG_INSTROOT}/etc/profile
	echo '				wd="${wd#*/}"' >>${IPKG_INSTROOT}/etc/profile
	echo '			else' >>${IPKG_INSTROOT}/etc/profile
	echo '				pfx=...' >>${IPKG_INSTROOT}/etc/profile
	echo '				wd="${wd#?????}"' >>${IPKG_INSTROOT}/etc/profile
	echo '			fi' >>${IPKG_INSTROOT}/etc/profile
	echo '		done' >>${IPKG_INSTROOT}/etc/profile
	echo '		print -r -- "$pfx$wd")'\''" $(if (( $(id -u) )); then' >>${IPKG_INSTROOT}/etc/profile
	echo '			print \$; else print \#; fi) "' >>${IPKG_INSTROOT}/etc/profile
	echo '	export HOSTNAME PS1 USER' >>${IPKG_INSTROOT}/etc/profile
	echo 'fi' >>${IPKG_INSTROOT}/etc/profile
fi
