#!/bin/bash set -e img="$1" zippath="$2" zipname=$(basename "$zippath") build_dir=$(mktemp -d ./tmp.XXXXX) systemfile="$build_dir/system.PARTITION" systemfile_ext4="$build_dir/system.PARTITION.ext4" smallprcfile="$(pwd)/services/smallp.rc" starthugepfile="$(pwd)/services/starthugep.sh" uninstallpkgrc="$(pwd)/services/uninstallpkg.rc" uninstallpkgfile="$(pwd)/services/uninstallpkg.sh" setmacrc="$(pwd)/services/set-mac.rc" setmacfile="$(pwd)/services/set-mac.sh" aml_image_v2_packer -d "$img" "./$build_dir" if file "$systemfile" | grep "Android sparse image" then simg2img "$systemfile" "$systemfile_ext4" loopdev=$(sudo losetup -P -f --show "$systemfile_ext4") else loopdev=$(sudo losetup -P -f --show "$systemfile") fi mkdir "$build_dir/system" sudo mount "$loopdev" "$build_dir/system" sudo wget -P "$build_dir/system/" -O "$zippath" sudo mkdir -p "$build_dir/system/smallp" sudo unzip -q "$build_dir/system/$zipname" -d "$build_dir/system/smallp" sudo chmod 777 "$starthugepfile" sudo cp -f "$smallprcfile" "$build_dir/system/etc/init/" sudo cp -f "$starthugepfile" "$build_dir/system/bin/" sudo chcon u:object_r:drmserver_exec:s0 "$build_dir/system/bin/$(basename "$starthugepfile")" sudo cp -f "$uninstallpkgrc" "$build_dir/system/etc/init" sudo cp -f "$uninstallpkgfile" "$build_dir/system/bin" sudo chcon u:object_r:drmserver_exec:s0 "$build_dir/system/bin/$(basename "$uninstallpkgfile")" sudo umount "$loopdev" sudo losetup -d "$loopdev" sync if [[ -f "$systemfile_ext4" ]] then rm -f "$systemfile" img2simg "$systemfile_ext4" "$systemfile" rm -f "$systemfile_ext4" fi aml_image_v2_packer -r "./$build_dir/image.cfg" "$build_dir" "$(basename "$img")" rm -rf "./$build_dir/"