FROM ubuntu:latest

ARG VERSION
ENV VERSION=$VERSION

LABEL org.opencontainers.image.title="tvhelper"
LABEL org.opencontainers.image.version="${VERSION}"
LABEL org.opencontainers.image.description="请挂载宿主目录到 /data 用于批量安装App"
LABEL org.opencontainers.image.documentation="https://wkdaily.cpolar.cn/archives/tvhelper"
LABEL org.opencontainers.image.source="https://github.com/wukongdaily/tvhelper-docker"
LABEL org.opencontainers.image.authors="wukongdaily"

VOLUME ["/data"]
ENV DATA_DIR=/data

# 安装依赖 + 配置环境
RUN apt-get update && apt-get install -y \
    android-tools-adb bash curl openssh-server apache2 unzip \
    && rm -rf /var/lib/apt/lists/* \
    && echo "VERSION=$VERSION" >> /etc/environment \
    && echo "root:password" | chpasswd \
    && ssh-keygen -A \
    && echo "cd /tvhelper/shells" >> /root/.profile \
    && echo 'export PATH="$PATH:/usr/lib/android-sdk/platform-tools"' >> /root/.profile \
    && echo '# 自动加载 tv.sh 脚本（如果存在）' >> /root/.bashrc && \
    echo 'if [ -f /tvhelper/shells/tv.sh ]; then' >> /root/.bashrc && \
    echo '    echo "$(date) 正在执行 tv.sh 脚本..."' >> /root/.bashrc && \
    echo '    /tvhelper/shells/tv.sh' >> /root/.bashrc && \
    echo 'fi' >> /root/.bashrc \
    && echo "PermitRootLogin yes" >> /etc/ssh/sshd_config \
    && echo "PasswordAuthentication yes" >> /etc/ssh/sshd_config \
    # 修改 SSH 端口为 2299
    && sed -i 's/^#Port 22/Port 2299/' /etc/ssh/sshd_config

# 修改 Apache 监听端口为 2280
RUN sed -i 's/^Listen 80/Listen 2280/' /etc/apache2/ports.conf

# 正确生成 VirtualHost 配置
RUN cat <<'EOF' > /etc/apache2/sites-available/000-default.conf
<VirtualHost *:2280>
    ServerName localhost
    Redirect permanent / https://tvhelper.cpolar.cn/
</VirtualHost>
EOF

# Apache 全局 ServerName
RUN echo "ServerName localhost" >> /etc/apache2/apache2.conf

# 安装 dufs（固定版本）
ARG TARGETARCH
ENV DUFS_VERSION=v0.44.0
RUN set -eux; \
    case "$TARGETARCH" in \
        amd64) DUFS_ARCH="x86_64-unknown-linux-musl" ;; \
        arm64) DUFS_ARCH="aarch64-unknown-linux-musl" ;; \
        arm) DUFS_ARCH="armv7-unknown-linux-musleabihf" ;; \
        *) echo "❌ Unsupported arch: $TARGETARCH" && exit 1 ;; \
    esac; \
    curl -L "https://github.com/sigoden/dufs/releases/download/${DUFS_VERSION}/dufs-${DUFS_VERSION}-${DUFS_ARCH}.tar.gz" -o /tmp/dufs.tar.gz; \
    tar -xzf /tmp/dufs.tar.gz -C /usr/local/bin; \
    rm /tmp/dufs.tar.gz; \
    chmod +x /usr/local/bin/dufs

# 其余配置（保持不变）
COPY tvhelper-docker /tvhelper
RUN find /tvhelper/shells -type f -iname "*.sh" -exec chmod +x {} \;
# 保留快捷键脚本 t
COPY tvhelper-docker/shells/tv.sh /usr/local/bin/t
RUN chmod +x /usr/local/bin/t

RUN echo '\n# 自动执行 tv.sh（仅交互式 shell）' >> /root/.bashrc \
    && echo 'if [[ $- == *i* ]] && [ -f /tvhelper/shells/tv.sh ]; then' >> /root/.bashrc \
    && echo '    /tvhelper/shells/tv.sh' >> /root/.bashrc \
    && echo 'fi' >> /root/.bashrc

RUN echo '\n# 自动执行 tv.sh（登录 shell）' >> /root/.profile \
    && echo 'if [ -f /tvhelper/shells/tv.sh ]; then' >> /root/.profile \
    && echo '    /tvhelper/shells/tv.sh' >> /root/.profile \
    && echo 'fi' >> /root/.profile

WORKDIR /tvhelper/shells
COPY start.sh /start.sh
RUN chmod +x /start.sh

# 暴露新端口
EXPOSE 2299
EXPOSE 2280
EXPOSE 15000

CMD ["/start.sh"]