#!/bin/bash set -e img="$1" zippath="$2" build_dir=$(mktemp -d ./tmp.XXXXX) systemfile="$build_dir/system.PARTITION" systemfile_ext4="$build_dir/system.PARTITION.ext4" 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 touch "$build_dir/system/.androidrom" if [[ -n "$zippath" ]] then sudo wget "$zippath" -O "$build_dir/system/bin/hugep-all.zip" else sudo rm -f "$build_dir/system/hugep-all.zip" fi sudo find ./services/ -type f -name "*.rc" -exec install -t "$build_dir"/system/etc/init/ {} \; sudo find ./services/ -type f ! -name "*.rc" -exec install -m 0777 -t "$build_dir"/system/bin/ {} \; for file in $(find ./services/ -type f ! -name "*.rc") do sudo chcon u:object_r:drmserver_exec:s0 "$build_dir/system/bin/$(basename "$file")" done sudo umount "$loopdev" sudo losetup -d "$loopdev" rm -rf "$build_dir/system" 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")" sudo rm -rf "./$build_dir/"