Skip to content

Instantly share code, notes, and snippets.

@zeph1e
Created Mar 8, 2017
Embed
What would you like to do?
bashrc_id.sh
#!/bin/bash
export CRED=root@id
SSH=$(whereis ssh | awk '{print $2}')
JQ=$(whereis jq | awk '{print $2}')
SCP=$(whereis scp | awk '{print $2}')
if [ -z "$SSH" ]; then
echo "Unable to find ssh" 1>&2
return 1
fi
if [ -z "$SCP" ]; then
echo "Unable to find scp" 1>&2
return 1
fi
if [ -z "$JQ" ]; then
echo "Unable to find jq" 1>&2
return 1
fi
function runCmd() {
ssh -x $CRED "$@"
}
function setDevmode() {
runCmd "mkdir -p /media/cryptofs/apps/usr/palm/services/com.palmdts.devmode.service && \
touch /media/cryptofs/apps/usr/palm/services/com.palmdts.devmode.service/start-devmode.sh && \
touch /var/luna/preferences/devmode_enabled && \
touch /var/luna/preferences/debug_system_apps && \
touch /var/luna/preferences/dc0 && \
touch /var/luna/preferences/dc1"
echo -n "reboot[Y/n]? "
read ANS
if [ x$ANS = xy -o x$ANS = xY -o x$ANS = x ]; then
runCmd "/sbin/reboot"
fi
}
function checkDevmode() {
runCmd "test -d /media/cryptofs/apps/usr/palm/services/com.palmdts.devmode.service && \
test -f /media/cryptofs/apps/usr/palm/services/com.palmdts.devmode.service/start-devmode.sh && \
test -f /var/luna/preferences/devmode_enabled && \
touch /var/luna/preferences/debug_system_apps && \
test -f /var/luna/preferences/dc0 && \
test -f /var/luna/preferences/dc1"
return $?
}
function doCapture() {
CAPTURED_FILE=$1
if [ -z "$CAPTURED_FILE" ]; then
echo "No filename given; save it as ~/capture.jpg"
CAPTURED_FILE=capture.jpg
fi
TMPFILE=$(tempfile)
rm $TMPFILE
ssh -x $CRED "luna-send -f -n 1 luna://com.webos.service.tv.capture/executeOneShot '{\"path\":\"$TMPFILE\", \"method\":\"SCREEN\", \"width\":1920, \"height\":1080, \"format\":\"JPEG\"}'" > /dev/null
scp $CRED:$TMPFILE $CAPTURED_FILE
runCmd rm -f $TMPFILE
}
function listRunningApps() {
ssh -x $CRED "luna-send -n 1 luna://com.palm.webappmanager/listRunningApps '{}'"
}
function launchApp() {
if [ -n "$2" ]; then
PARAM=",\"params\":$2"
fi
ssh -x $CRED "luna-send -n 1 luna://com.webos.applicationManager/launch '{\"id\":\"$1\" $PARAM}'"
}
function closeApp() {
ssh -x $CRED "luna-send -n 1 luna://com.palm.applicationManager/closeByAppId '{\"id\":\"$1\"}'"
}
function closeAllApps() {
ssh -x $CRED "luna-send -n 1 luna://com.palm.webappmanager/closeAllApps '{}'"
}
function foregroundAppInfo() {
ssh -x $CRED "luna-send -n 1 luna://com.webos.applicationManager/getForegroundAppInfo '{}'"
}
function sendExitKey() {
ssh -x $CRED "luna-send -n 1 luna://com.webos.service.tv.legacyinput/createKeyEvent '{\"code\":\"0x00AE\",\"type\":\"single\"}'"
}
function launchDSMP() {
TARGETDIR=$1
if [ -z "$TARGETDIR" ]; then
TARGETDIR="/tmp/usb/sda/sda/AutoPlay"
fi
DIREXISTS=$(runCmd "if test -d $TARGETDIR; then echo t; fi")
if [ -z "$DIREXISTS" ]; then
echo "No such directory : $TARGETDIR" 1>&2
fi
CTSFILE=$(runCmd ls $TARGETDIR | grep -E "\.cts$" | awk '{ print $1 }')
CPLSFILE=$(runCmd ls $TARGETDIR | grep -E "\.cpls$" | awk '{ print $1 }')
if [ -z "$CTSFILE" -a -z "$CPLSFILE" ]; then
echo "no cts or cpls file is in : $TARGETDIR" 1>&2
return 1
fi
if [ -n "$CPLSFILE" ]; then
TARGETFILE=$CPLSFILE
echo $CPLSFILE
else
TARGETFILE=$CTSFILE
fi
REALPATH=$(runCmd realpath $TARGETDIR/$TARGETFILE 2> /dev/null)
PARAM="'{\\\"filePath\\\":\\\"$REALPATH\\\",\\\"isNew\\\":false}'"
launchApp "com.webos.app.dsmp" "$PARAM"
}
function closeDSMP() {
closeApp "com.webos.app.dsmp"
}
function installDSMP() {
FILE=$1
TARGETDIR=$2
if [ -z "$FILE" -o ! -f "$FILE" ]; then
echo "[Usage] $0 <content.zip> [install-dir]" 1>&2
return
fi
if [ -z "$TARGETDIR" ]; then
TARGETDIR="~/dsmp"
fi
TEMPDIR=$(runCmd mktemp -d)
scp $FILE $CRED:$TEMPDIR/ 2> /dev/null
# refresh directory
runCmd rm -rf $TARGETDIR 2> /dev/null
runCmd mkdir -p $TARGETDIR 2> /dev/null
runCmd unzip -x $TEMPDIR/$FILE -d $TARGETDIR 2> /dev/null
runCmd rm -rf $TEMPDIR 2> /dev/null
}
PREV_DSMP_TEMP=
function runDSMP() {
FILE=$1
if [ -z "$FILE" -o ! -f "$FILE" ]; then
echo "[Usage] $0 <content.zip> [install-dir]" 1>&2
return
fi
TARGETDIR=$(runCmd mktemp -d)
installDSMP $FILE $TARGETDIR
if [ -n "$PREV_DSMP_TEMP" ]; then
runCmd rm -rf $PREV_DSMP_TEMP 2> /dev/null
fi
RUNNING=$(listRunningApps | grep "com.webos.app.dsmp")
if [ -n "$RUNNING" ]; then
closeDSMP
fi
launchDSMP $TARGETDIR
PREV_DSMP_TEMP=$TARGETDIR
}
function launchScap() {
TARGETDIR=$1
if [ -z "$TARGETDIR" ]; then
TARGETDIR="~/scap"
fi
REALPATH=$(runCmd realpath $TARGETDIR/index.html 2> /dev/null)
if [ -z "$REALPATH" ]; then
echo "index.html file is not in : $TARGETDIR" 1>&2
return
fi
PARAM="'{\\\"path\\\":\\\"$REALPATH\\\"}'"
launchApp "commercial.signage.signageapplauncher" "$PARAM"
}
function closeScap() {
closeApp "commercial.signage.signageapplauncher"
}
function installScap() {
FILE=$1
TARGETDIR=$2
if [ -z "$FILE" ]; then
FILE="scap_app.zip"
fi
if [ -z "$TARGETDIR" ]; then
TARGETDIR="~/scap"
fi
if [ ! -f $FILE ]; then
echo "no such file : $FILE" 1>&2
return
fi
TEMPDIR=$(runCmd mktemp -d)
scp $FILE $CRED:$TEMPDIR/ 2> /dev/null
# refresh directory
runCmd "rm -rf $TARGETDIR" 2> /dev/null
runCmd "mkdir -p $TARGETDIR" 2> /dev/null
runCmd "unzip $TEMPDIR/$FILE -d $TARGETDIR" 2> /dev/null
runCmd "rm -rf $TEMPDIR" 2> /dev/null
}
# install to temp & run
PREV_SCAP_TEMP=
function runScap() {
FILE=$1
if [ -z "$FILE" ]; then
FILE="scap_app.zip"
fi
TARGETDIR=$(runCmd mktemp -d)
installScap $FILE $TARGETDIR
if [ -n "$PREV_SCAP_TEMP" ]; then
runCmd rm -rf $PREV_SCAP_TEMP 2> /dev/null
fi
RUNNING=$(listRunningApps | grep "commercial.signage.signageapplauncher")
if [ -n "$RUNNING" ]; then
closeScap
fi
launchScap $TARGETDIR
PREV_SCAP_TEMP=$TARGETDIR
}
IPKAPPID="com.lg.app.signage"
function launchIpkApp() {
launchApp $IPKAPPID
}
function closeIpkApp() {
closeApp $IPKAPPID
}
function installIpkApp() {
FILE=$1
INSTALLING_IPKAPPID=$IPKAPPID
if [ -z "$FILE" ]; then
echo 1>&2 "filename is not given as an argument"
return 1
fi
if [ "x$2" != "x" ]; then
INSTALLING_IPKAPPID=$2
fi
echo $INSTALLING_IPKAPPID
BASENAME=$(basename $FILE)
RETURN_VALUE=0
TEMPDIR=$(runCmd mktemp -d 2> /dev/null)
TEMPFILE=`tempfile`
$SCP $FILE $CRED:$TEMPDIR/$BASENAME 2> /dev/null
REMOTEFILE=$TEMPDIR/$BASENAME
OLDIFS=$IFS
IFS=$'\n'$'\t'$' '
if checkDevmode; then
JOBS_CURRENT=$(jobs | sed -E 's/(\[|\])/ /g' | awk '{ print $1; }')
# runCmd "luna-send -i luna://com.webos.appInstallService/dev/install \
# '{\"id\":\"$INSTALLING_IPKAPPID\", \"ipkUrl\":\"$REMOTEFILE\", \"subscribe\":true, \
# \"target\":{\"deviceId\":\"INTERNAL_STORAGE_SIGNAGE\", \
# \"deviceUri\":\"/mnt/lg/appstore/signage\", \
# \"deviceType\":\"internal signage\", \
# \"deviceName\":\"SIGNAGE_INTERNAL STORAGE\"}}'" > $TEMPFILE 2> /dev/null &
runCmd "luna-send -i luna://com.webos.appInstallService/dev/install \
'{\"id\":\"$INSTALLING_IPKAPPID\", \"ipkUrl\":\"$REMOTEFILE\", \"subscribe\":true}'" > $TEMPFILE 2> /dev/null &
sleep 1
JOBS_AFTER=`jobs | sed -E 's/(\[|\])/ /g' | awk '{ print $1; }'`
JOBS_DIFF=
for var in $JOBS_AFTER; do
CONTAINS=
for cur in $JOBS_CURRENT; do
if [ "$var" = "$cur" ]; then
CONTAINS=1
break
fi
done
if [ -z "$CONTAINS" ]; then
JOBS_DIFF=$var $JOBS_DIFF
fi
done
# echo "jobs diff=$JOBS_DIFF"
RETRY_MAX=10
COMPLETED=
STATE="CHECK_RETVAL"
SLEEP="1"
while [ $RETRY_MAX -gt 0 ]; do
# echo $STATE
case $STATE in
CHECK_RETVAL)
HEAD=$(head -n 1 $TEMPFILE)
# echo "HEAD: $HEAD"
if [ -n "$HEAD" ]; then
for var in $(head -n 1 $TEMPFILE | $JQ '.[]'); do
# echo $var:
if [ "$var" != "true" ]; then
STATE="FAILED"
break
fi
done
if [ "$STATE" = "CHECK_RETVAL" ]; then
STATE="WAIT_COMPLETE"
echo "Waiting to be completed..."
continue
fi
fi
;;
WAIT_COMPLETE)
if [ "$(tail -n 1 $TEMPFILE | $JQ '.statusValue')" = "17" -a \
"$(tail -n 1 $TEMPFILE | $JQ '.details.progress')" = "100" ]; then
STATE="COMPLETED"
continue
fi
;;
COMPLETED)
RETRY_MAX=0
echo "Completed!"
SLEEP=
;;
FAILED)
echo "Failed to install : $FILE" 1>&2
echo ; echo "luna-bus log:"
cat $TEMPFILE
RETURN_VALUE=1
RETRY_MAX=0
SLEEP=
;;
*)
echo "Invalid state : $STATE" 1>&2
STATE="FAILED"
;;
esac
RETRY_MAX=$(expr $RETRY_MAX \- 1)
if [ -n "$SLEEP" ]; then
sleep 1
fi
done
# cleanup
for var in $JOBS_DIFF; do
JOB='%'$var
kill $JOB 2> /dev/null
done
runCmd "rm -rf $TEMPDIR" 2> /dev/null
rm -f $TEMPFILE
IFS=$OLDIFS
else
echo "Devmode is not enabled! run setDevmode first" 2>&1
return 1
fi
return $RETURN_VALUE
}
function removeIpkApp() {
REMOVING_IPKAPPID=$IPKAPPID
if [ "x$1" != "x" ]; then
REMOVING_IPKAPPID=$1
fi
RETURN_VALUE=0
OLDIFS=$IFS
IFS=$'\n'$'\t'$' '
if checkDevmode; then
JOBS_CURRENT=$(jobs | sed -E 's/(\[|\])/ /g' | awk '{ print $1; }')
runCmd "luna-send -i luna://com.webos.appInstallService/dev/remove \
'{\"id\":\"$REMOVING_IPKAPPID\", \"subscribe\":true}'" > $TEMPFILE 2> /dev/null &
sleep 1
JOBS_AFTER=`jobs | sed -E 's/(\[|\])/ /g' | awk '{ print $1; }'`
JOBS_DIFF=
for var in $JOBS_AFTER; do
CONTAINS=
for cur in $JOBS_CURRENT; do
if [ "$var" = "$cur" ]; then
CONTAINS=1
break
fi
done
if [ -z "$CONTAINS" ]; then
JOBS_DIFF=$var $JOBS_DIFF
fi
done
# echo "jobs diff=$JOBS_DIFF"
RETRY_MAX=10
COMPLETED=
STATE="CHECK_RETVAL"
SLEEP="1"
while [ $RETRY_MAX -gt 0 ]; do
# echo $STATE
case $STATE in
CHECK_RETVAL)
HEAD=$(head -n 1 $TEMPFILE)
# echo "HEAD: $HEAD"
if [ -n "$HEAD" ]; then
for var in $(head -n 1 $TEMPFILE | $JQ '.[]'); do
# echo $var:
if [ "$var" != "true" ]; then
STATE="FAILED"
break
fi
done
if [ "$STATE" = "CHECK_RETVAL" ]; then
STATE="WAIT_COMPLETE"
echo "Waiting to be completed..."
continue
fi
fi
;;
WAIT_COMPLETE)
if [ "$(tail -n 1 $TEMPFILE | $JQ '.statusValue')" = "31" -a \
"$(tail -n 1 $TEMPFILE | $JQ '.details.state')" = "\"removed\"" ]; then
STATE="COMPLETED"
continue
elif [ "$(tail -n 1 $TEMPFILE | $JQ '.details.state')" = "\"remove failed\"" ]; then
STATE="FAILED"
continue
fi
;;
COMPLETED)
RETRY_MAX=0
echo "Completed!"
SLEEP=
;;
FAILED)
echo "Failed to remove" 1>&2
echo ; echo "luna-bus log:"
cat $TEMPFILE
RETURN_VALUE=1
RETRY_MAX=0
SLEEP=
;;
*)
echo "Invalid state : $STATE" 1>&2
STATE="FAILED"
;;
esac
RETRY_MAX=$(expr $RETRY_MAX \- 1)
if [ -n "$SLEEP" ]; then
sleep 1
fi
done
# cleanup
for var in $JOBS_DIFF; do
JOB='%'$var
kill $JOB 2> /dev/null
done
IFS=$OLDIFS
else
echo "Devmode is not enabled! run setDevmode first" 2>&1
return 1
fi
return $RETURN_VALUE
}
function inodeMon() {
TARGETDIR=$1
if [ -z "$TARGETDIR" ]; then
TARGETDIR="/var/lib/webappmanager3"
fi
COUNT=
echo "#### monitoring $TARGETDIR #################################################"
runCmd "(cd $TARGETDIR && find . | cut -d/ -f-2 | sort | uniq -c | sort -rn)" 2> /dev/null
echo
SPACES=" "
while [ 1 ]; do
unset SUBDIRS
unset KEYS
unset RESULT
declare -A SUBDIRS
declare -a KEYS
declare -a RESULT
DATE="[$(date)]"
INODE=$(runCmd "cd /var/lib/webappmanager3 && find . | cut -d/ -f-2 | sort | uniq -c | sort -rn" \
2> /dev/null| sed -E 's/\.(.+)( )+(.+)/.\1_\3/g')
INDEX=0
for var in $INODE; do
if [ -z "$COUNT" ]; then
COUNT=$var
continue
fi
SUBDIRS["$var"]=$COUNT
KEYS[$INDEX]="$var"
INDEX=$(expr $INDEX \+ 1)
COUNT=
done
# first run
if [ -z "$(echo ${!PREV_SUBDIRS[@]} | awk '{print $1}')" ]; then
unset PREV_SUBDIRS
declare -A PREV_SUBDIRS
for key in ${!SUBDIRS[@]}; do
PREV_SUBDIRS[$key]=${SUBDIRS[$key]}
done
continue
fi
OFFSET=$(expr $(expr ${#SPACES} \- ${#DATE}) \- 1)
PREPEND=${SPACES:$OFFSET}
INDEX=0
# see if there're changes
for i in ${!KEYS[@]}; do
key=${KEYS[$i]}
if [ "${PREV_SUBDIRS[$key]}" != "${SUBDIRS[$key]}" ]; then
CUR=$(if [ -z "${SUBDIRS[$key]}" ]; then echo 0; else echo ${SUBDIRS[$key]}; fi)
PREV=$(if [ -z "${PREV_SUBDIRS[$key]}" ]; then echo 0; else echo ${PREV_SUBDIRS[$key]}; fi)
DELTA=$(expr $CUR \- $PREV)
if [ "$DELTA" -gt 0 ]; then
DELTA="+$DELTA"
fi
RESULT[$INDEX]="$key: $DELTA(${SUBDIRS[$key]})"
INDEX=$(expr $INDEX \+ 1)
fi
done
# need to check removed slot
for key in ${!PREV_SUBDIRS[@]}; do
if [ -z "${SUBDIRS[$key]}" ]; then
DELTA="-"${PREV_SUBDIRS[$key]}
RESULT[$INDEX]="$key: $DELTA(gone)"
INDEX=$(expr $INDEX \+ 1)
fi
done
# if [ -n "$RESULT" ]; then
for i in ${!RESULT[@]}; do
if [ "$i" -eq "0" ]; then
echo -n "$DATE "
else
echo -n "$PREPEND "
fi
echo ${RESULT[i]}
done
unset PREV_SUBDIRS
declare -A PREV_SUBDIRS
for key in ${!SUBDIRS[@]}; do
# echo ">>> $key: ${SUBDIRS[$key]}"
PREV_SUBDIRS[$key]=${SUBDIRS[$key]}
done
# echo "++++++"
# for key in ${!PREV_SUBDIRS[@]}; do
# echo "$key: ${PREV_SUBDIRS[$key]}"
# done
sleep 0.1
done
}
function runBrowser() {
runCmd "cd /mnt/otncabi/usr/palm/applications/com.webos.app.browser && ./run_webbrowser"
}
function killBrowser() {
runCmd "cd /mnt/otncabi/usr/palm/applications/com.webos.app.browser && ./kill_webbrowser"
}
function showFB() {
runCmd "echo \"0 1\" > /proc/lg/fbdev/viewctrl"
}
function hideFB() {
runCmd "echo \"0 0\" > /proc/lg/fbdev/viewctrl"
}
function redirectScap() {
REDIRECTURL=$1
if [ "x$REDIRECTURL" = "x" ]; then
echo 1>&2 "Need URL to redirect"
return 1
fi
TEMPDIR=$(mktemp -d)
cat <<EOF > $TEMPDIR/index.html
<html>
<meta http-equiv="refresh" content="0; url=$REDIRECTURL" />
</html>
EOF
(cd $TEMPDIR && zip -r scap_app.zip . && runScap)
rm -rf $TEMPDIR
}
function clearCache() {
runCmd "luna-send -n 1 luna://com.palm.webappmanager/clearBrowsingData '{}'"
}
function doInstop() {
echo -n "Really do instop?[y/N] "
read ANS
if [ x$ANS = xY -o x$ANS = xy ]; then
runCmd "luna-send -n 1 -f luna://com.webos.service.tv.systemproperty/doFactoryDefault '{}'"
fi
}
function arBrowser() {
URL=$1
runCmd luna-send -n 1 -f luna://com.webos.service.tv.hcap/launchPreloadedApplication "{\"id\": \"201604201053010001\", \"parameters\": \"{\\\"target\\\":\\\"$URL\\\"}\"}"
}
function resetDate() {
CURDATE=$(date -u '+%Y%m%d%H%M.%S')
runCmd "date -u -s $CURDATE"
}
function screenRotation() {
DEGREE=$1
if [ -z "$DEGREE" -o $(expr "$DEGREE" \% 90) -ne 0 -o "$DEGREE" -gt 270 ]; then
echo 1>&2 "No valid degree to rotate is being given (one of 0, 90, 180, 270)"
return 1;
fi
if [ $DEGREE -eq 0 ]; then
DEGREE="off"
fi
runCmd "luna-send -n 1 luna://com.webos.settingsservice/setSystemSettings '{\"settings\":{\"screenRotation\":\"$DEGREE\"},\"category\":\"option\"}'"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment