#!/bin/sh

#
# Screen capture of Mir devices
#

# Copyright (C) 2013 Canonical
#
# Authors: Jean-Baptiste Lallement <jean-baptiste.lallement@canonical.com>
#
# This program is free software; you can redistribute it and/or modify it #
# under the terms of the GNU General Public License as published by the Free #
# Software Foundation; version 3.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# # details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51 Franklin
# Street, Fifth Floor, Boston, MA 02110-1301 USA

# TODO:
#   - detect that it is running Mir and fallback to screencap if it is SF

set -eu

PICDIR=$(mktemp -d /tmp/$(basename $0).XXXXXX)
FBDEV=fb0
ADBOPTS=""
CONVERTOPTS="-alpha off -resize 50%"
SUPPORTED="mako manta grouper"
REQUIREDPKGS="android-tools-adb imagemagick"

cleanup() {
    #
    # Cleanup temporary files
    #
    [ -d "$PICDIR" ] && rm -Rf "$PICDIR"
}
trap cleanup EXIT INT QUIT ABRT PIPE TERM

usage() {
    #
    # Print usage and exit
    #
    cat <<EOF
Usage: $(basename $0) [OPTIONS...] <FILENAME>
Dump /dev/$FBDEV from a touch device and write it to FILENAME

Arguments:
    FILENAME    Name of the output image, the output format must be supported by
                convert from the package imagemagick.
Options
    -d          Enable debug
    -h          This help
    -s SERIAL   Serial number of the device

EOF
    exit 1
}

check_prerequisites() {
    # Check list of required packages
    okay=1
    pkgs="$REQUIREDPKGS"
    for pkg in $pkgs; do
        if ! dpkg-query -W -f '${Status}\t${Package}\n' $pkg 2>/dev/null| grep "ok installed" >/dev/null 2>&1; then
            okay=0
            echo "E: Package '$pkg' is required and is not installed."
        fi
    done

    if [ $okay -ne 1 ]; then
        echo "E: pre-requisites are not met. Exiting!"
        exit 1
    fi
}

get_device() {
    # Get device name
    device=$(adb $ADBOPTS shell getprop ro.cm.device)
    echo "$device"|tr -d '\r'
}

is_supported() {
    # Returns 0 is a device is supported
    if ! echo "$SUPPORTED"|grep -qw $1 >/dev/null 2>&1; then
        return 1
    else
        return 0
    fi
}
get_mode() {
    # Find graphics mode
    size=$(adb $ADBOPTS shell head -1 /sys/devices/virtual/graphics/$FBDEV/modes| cut -d: -f2|cut -d 'p' -f1)

    # Fallback to hardcoded values
    #if ! echo "$size"|grep -qE '^\[0-9\]+x\[0-9\]+$' >/dev/null 2>&1; then
    if ! echo "$size"|grep -qE '^[0-9]+x[0-9]+$' >/dev/null 2>&1; then
        device="$(get_device)"
        case "$device" in
            maguro)
                size=720x1280
                ;;
            mako)
                size=768x1280
                ;;
            manta)
                size=1600x2560
                ;;
            grouper)
                size=800x1280
                ;;
            *)
                size=""
                echo "E: Device '$device' is not supported. Supported devices are: $SUPPORTED"
                ;;
        esac
    fi
    echo $size
}

SHORTOPTS="hds:"
LONGOPTS="help,debug,serial:"

TEMP=$(getopt -o $SHORTOPTS --long $LONGOPTS -- "$@")
eval set -- "$TEMP"

while true ; do
    case "$1" in
        -h|--help)
            usage;;
        -d|--debug)
            set -x
            shift;;
        -s|--serial)
            ADBOPTS="$ADBOPTS -s $2"
            shift 2;; 
        --) shift;
            break;;
        *) usage;;
    esac
done

[ $# -eq 0 ] && usage
DST=$1

check_prerequisites

# Set resolution and depth for device
depth=8
size="$(get_mode)"
device="$(get_device)"
if [ -z "$size" ] || ! is_supported "$device"; then
    echo "E: Device '$device' is not supported. Supported devices are: $SUPPORTED"
    exit 1
fi
CONVERTOPTS="$CONVERTOPTS -depth $depth -size $size"

echo "I: Dumping $FBDEV ..."
adb $ADBOPTS shell "kill -SIGSTOP \$(pidof unity8)"
adb $ADBOPTS pull /dev/$FBDEV ${PICDIR}/fb
adb $ADBOPTS shell "kill -SIGCONT \$(pidof unity8)"

convert $CONVERTOPTS rgba:${PICDIR}/fb[0] $DST
echo "I: Done"
